Skip to content

Commit

Permalink
feat: add official plugin repo
Browse files Browse the repository at this point in the history
  • Loading branch information
pylixonly committed Aug 14, 2024
1 parent 61286d8 commit 60a95ab
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/core/ui/components/AddonPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface AddonPageProps<T extends object> {
card: ComponentType<CardWrapper<T>>;
searchKeywords: SearchKeywords;
onFabPress?: () => void;
ListHeaderComponent?: ComponentType<any>;
ListFooterComponent?: ComponentType<any>;
}

Expand All @@ -45,6 +46,7 @@ export default function AddonPage<T extends object>({ card: CardComponent, ...pr

const headerElement = (
<View style={{ paddingBottom: 8 }}>
{props.ListHeaderComponent && <props.ListHeaderComponent />}
{settings.safeMode?.enabled && <View style={{ marginBottom: 10 }}>
<HelpMessage messageType={0}>{props.safeModeMessage}</HelpMessage>
{props.safeModeExtras}
Expand Down
19 changes: 10 additions & 9 deletions src/core/ui/settings/pages/PluginBrowser/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { installPlugin, isPluginInstalled, uninstallPlugin } from "@lib/plugins"
import { BunnyPluginManifest } from "@lib/plugins/types";
import { showToast } from "@lib/ui/toasts";
import { safeFetch } from "@lib/utils";
import { OFFICIAL_PLUGINS_REPO_URL } from "@lib/utils/constants";
import { Button, Card, FlashList, IconButton, Stack, Text } from "@metro/common/components";
import { QueryClient, QueryClientProvider, useMutation, useQuery } from "@tanstack/react-query";
import { chunk } from "es-toolkit";
Expand All @@ -17,21 +18,21 @@ async function arrayFromAsync<T>(iterableOrArrayLike: AsyncIterable<T>): Promise
return arr;
}

async function fetchManifest(id: string) {
const url = `https://bn-plugins.github.io/dist/plugins/${id}/manifest.json`;
async function fetchManifest(repoURL: string, id: string) {
const url = new URL(`plugins/${id}/manifest.json`, repoURL);
const data = await safeFetch(url).then(d => d.json());

queryClient.setQueryData(["plugin-manifest-dist", { id }], data);

return data as BunnyPluginManifest;
}

async function* getManifests() {
const rawResponse = await safeFetch("https://bn-plugins.github.io/dist/repo.json");
async function* getManifests(repoUrl: string) {
const rawResponse = await safeFetch(repoUrl);
const pluginIds = Object.keys(await rawResponse.json());

for (const idChunks of chunk(pluginIds, 5)) {
const manifests = idChunks.map(fetchManifest);
const manifests = idChunks.map(id => fetchManifest(OFFICIAL_PLUGINS_REPO_URL, id));
for (const manifest of manifests) {
yield await manifest;
}
Expand Down Expand Up @@ -74,10 +75,10 @@ function TrailingButtons(props: { id: string }) {
</Stack>;
}

function PluginCard(props: { id: string, manifest: BunnyPluginManifest; }) {
function PluginCard(props: { repoUrl: string, id: string, manifest: BunnyPluginManifest; }) {
const { isPending, error, data: plugin } = useQuery({
queryKey: ["plugin-manifest-dist", { id: props.id }],
queryFn: () => fetchManifest(props.id)
queryFn: () => fetchManifest(props.repoUrl, props.id)
});

return (
Expand Down Expand Up @@ -113,7 +114,7 @@ function PluginCard(props: { id: string, manifest: BunnyPluginManifest; }) {
function BrowserPage() {
const { data, error, isPending, refetch } = useQuery({
queryKey: ["plugins-repo-fetch"],
queryFn: () => arrayFromAsync(getManifests())
queryFn: () => arrayFromAsync(getManifests(OFFICIAL_PLUGINS_REPO_URL))
});

if (error) {
Expand Down Expand Up @@ -148,7 +149,7 @@ function BrowserPage() {
contentContainerStyle={{ paddingBottom: 90, paddingHorizontal: 5 }}
renderItem={({ item: manifest }: any) => (
<View style={{ paddingVertical: 6, paddingHorizontal: 8 }}>
<PluginCard id={manifest.id} manifest={manifest} />
<PluginCard repoUrl={OFFICIAL_PLUGINS_REPO_URL} id={manifest.id} manifest={manifest} />
</View>
)}
/>;
Expand Down
9 changes: 8 additions & 1 deletion src/core/ui/settings/pages/Plugins/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useProxy as useNewProxy } from "@lib/api/storage/new";
import { isPluginInstalled, pluginSettings, registeredPlugins } from "@lib/plugins";
import { Author } from "@lib/utils/types";
import { NavigationNative } from "@metro/common";
import { Button, SegmentedControl, SegmentedControlPages, Text, useSegmentedControlState } from "@metro/common/components";
import { Button, HelpMessage, SegmentedControl, SegmentedControlPages, Text, useSegmentedControlState } from "@metro/common/components";
import { ComponentProps } from "react";
import { useWindowDimensions, View } from "react-native";

Expand Down Expand Up @@ -87,6 +87,13 @@ export default function Plugins() {
<PluginPage
useItems={() => (useNewProxy(pluginSettings), [...registeredPlugins.values()].filter(p => isPluginInstalled(p.id)))}
resolveItem={unifyBunnyPlugin}
ListHeaderComponent={() => (
<View style={{ marginBottom: 10 }}>
<HelpMessage messageType={0}>
Bunny plugin system is in no way ready, try not getting yourself burnt ⚠️
</HelpMessage>
</View>
)}
ListFooterComponent={() => (
<View style={{ alignItems: "center", justifyContent: "center", paddingTop: 16, gap: 12 }}>
<Text variant="heading-lg/bold">{"Looking for more?"}</Text>
Expand Down
8 changes: 6 additions & 2 deletions src/lib/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getCorePlugins } from "@core/plugins";
import { readFile, removeFile, writeFile } from "@lib/api/native/fs";
import { awaitStorage, createStorage, getPreloadedStorage, preloadStorageIfExists, updateStorageAsync } from "@lib/api/storage/new";
import { safeFetch } from "@lib/utils";
import { OFFICIAL_PLUGINS_REPO_URL } from "@lib/utils/constants";
import { semver } from "@metro/common";

import { createBunnyPluginAPI } from "./api";
Expand Down Expand Up @@ -191,7 +192,7 @@ export async function updateRepository(repoUrl: string) {
* Deletes a repository from registrations and uninstalls ALL plugins under this repository
*/
export async function deleteRepository(repoUrl: string) {
// Once when we have our "official repository", make sure to make it non-deleteable
assert(repoUrl !== OFFICIAL_PLUGINS_REPO_URL, repoUrl, "delete the official repository");
assert(pluginRepositories[repoUrl], repoUrl, "delete a non-registered repository");

const promQueues = [] as Promise<unknown>[];
Expand Down Expand Up @@ -366,8 +367,11 @@ export async function checkAndRegisterUpdates() {
corePluginInstances.set(id, instance);
}

await updateRepository(OFFICIAL_PLUGINS_REPO_URL);
await Promise.allSettled(Object.keys(pluginRepositories).map(async repo => {
await updateRepository(repo);
if (repo !== OFFICIAL_PLUGINS_REPO_URL) {
await updateRepository(repo);
}
}));
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const VD_PROXY_PREFIX = "https://vd-plugins.github.io/proxy";
export const BUNNY_PROXY_PREFIX = "https://bn-plugins.github.io/vd-proxy";
export const OLD_BUNNY_PROXY_PREFIX = "https://bunny-mod.github.io/plugins-proxy";

export const OFFICIAL_PLUGINS_REPO_URL = "https://bn-plugins.github.io/dist/repo.json";

// These are still Vendetta's
export const DISCORD_SERVER_ID = "1015931589865246730";
export const PLUGINS_CHANNEL_ID = "1091880384561684561";
Expand Down

0 comments on commit 60a95ab

Please sign in to comment.