feat(ui/plugins): show error message

This commit is contained in:
amsyarasyiq 2024-05-23 21:35:53 +08:00
commit d6d0801731
2 changed files with 34 additions and 0 deletions

View file

@ -2,12 +2,16 @@ import { CardWrapper } from "@core/ui/components/AddonCard";
import { requireAssetIndex } from "@lib/api/assets"; import { requireAssetIndex } from "@lib/api/assets";
import { useProxy } from "@lib/api/storage"; import { useProxy } from "@lib/api/storage";
import { BunnyPlugin, startPlugin, stopPlugin } from "@lib/managers/plugins"; import { BunnyPlugin, startPlugin, stopPlugin } from "@lib/managers/plugins";
import { lazyDestructure } from "@lib/utils/lazy";
import { findByProps } from "@metro";
import { tokens } from "@metro/common";
import { Card, IconButton, Stack, TableSwitch, Text } from "@metro/common/components"; import { Card, IconButton, Stack, TableSwitch, Text } from "@metro/common/components";
import { createContext, useContext } from "react"; import { createContext, useContext } from "react";
import { Image, View } from "react-native"; import { Image, View } from "react-native";
import { usePluginCardStyles } from "./usePluginCardStyles"; import { usePluginCardStyles } from "./usePluginCardStyles";
const { useToken } = lazyDestructure(() => findByProps("useToken"));
const PluginContext = createContext<BunnyPlugin>(null!); const PluginContext = createContext<BunnyPlugin>(null!);
const usePlugin = () => useContext(PluginContext); const usePlugin = () => useContext(PluginContext);
@ -50,6 +54,30 @@ function Title() {
</Text>; </Text>;
} }
// TODO: Allow glacing at the error's stack
function Status() {
const plugin = usePlugin();
const TEXT_NORMAL = useToken(tokens.colors.TEXT_NORMAL);
if (!plugin.error) return null;
return <View style={{ flexDirection: "row", alignItems: "center", gap: 8 }}>
<Image
tintColor={TEXT_NORMAL}
source={{
...Image.resolveAssetSource(
requireAssetIndex("WarningIcon")
),
height: 18,
width: 18
}}
/>
<Text variant="text-sm/semibold">
There was an error while attempting to start this plugin.
</Text>
</View>;
}
export default function PluginCard({ item: plugin }: CardWrapper<BunnyPlugin>) { export default function PluginCard({ item: plugin }: CardWrapper<BunnyPlugin>) {
useProxy(plugin); useProxy(plugin);
@ -57,6 +85,7 @@ export default function PluginCard({ item: plugin }: CardWrapper<BunnyPlugin>) {
<PluginContext.Provider value={plugin}> <PluginContext.Provider value={plugin}>
<Card> <Card>
<Stack spacing={16}> <Stack spacing={16}>
<Status />
<View style={{ flexDirection: "row", alignItems: "center" }}> <View style={{ flexDirection: "row", alignItems: "center" }}>
<Stack spacing={0}> <Stack spacing={0}>
<Title /> <Title />

View file

@ -31,6 +31,7 @@ export interface BunnyPlugin {
manifest: PluginManifest; manifest: PluginManifest;
enabled: boolean; enabled: boolean;
update: boolean; update: boolean;
error?: string;
js: string; js: string;
} }
@ -74,6 +75,7 @@ export async function fetchPlugin(id: string) {
enabled: existingPlugin?.enabled ?? false, enabled: existingPlugin?.enabled ?? false,
update: existingPlugin?.update ?? true, update: existingPlugin?.update ?? true,
js: pluginJs ?? existingPlugin.js, js: pluginJs ?? existingPlugin.js,
error: existingPlugin?.error
}; };
} }
@ -107,9 +109,12 @@ export async function startPlugin(id: string) {
loadedPlugins[id] = pluginRet; loadedPlugins[id] = pluginRet;
pluginRet.onLoad?.(); pluginRet.onLoad?.();
} }
delete plugin.error;
plugin.enabled = true; plugin.enabled = true;
} catch (e) { } catch (e) {
logger.error(`Plugin ${plugin.id} errored whilst loading, and will be unloaded`, e); logger.error(`Plugin ${plugin.id} errored whilst loading, and will be unloaded`, e);
plugin.error = e instanceof Error ? e.stack : String(e);
try { try {
loadedPlugins[plugin.id]?.onUnload?.(); loadedPlugins[plugin.id]?.onUnload?.();