From 2410582cf8f607990ce9e9b6b92360bba98f6a57 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Sat, 1 Oct 2022 22:09:20 +0200 Subject: [PATCH] Dedicated Updater Page, Settings feedback --- src/Vencord.ts | 2 +- src/components/Settings.tsx | 63 +++++++++++++++++++----------- src/components/Updater.tsx | 76 ++++++++++++++++++++++--------------- src/components/index.ts | 1 + src/plugins/index.ts | 57 ++++++++++++++-------------- src/plugins/settings.ts | 3 +- src/utils/ChangeList.ts | 24 ++++++++++++ 7 files changed, 143 insertions(+), 83 deletions(-) create mode 100644 src/utils/ChangeList.ts diff --git a/src/Vencord.ts b/src/Vencord.ts index adf8e7b2..5b3d6f26 100644 --- a/src/Vencord.ts +++ b/src/Vencord.ts @@ -32,7 +32,7 @@ async function init() { "View Update", () => { popNotice(); - Router.open("Vencord"); + Router.open("VencordUpdater"); } ); }, 10000); diff --git a/src/components/Settings.tsx b/src/components/Settings.tsx index dd23b733..54e6ddb6 100644 --- a/src/components/Settings.tsx +++ b/src/components/Settings.tsx @@ -1,20 +1,50 @@ -import { classes, humanFriendlyJoin, lazy, useAwaiter } from "../utils/misc"; +import { classes, humanFriendlyJoin, useAwaiter } from "../utils/misc"; import Plugins from 'plugins'; import { useSettings } from "../api/settings"; import IpcEvents from "../utils/IpcEvents"; -import { Button, Switch, Forms, React, Margins } from "../webpack/common"; +import { Button, Switch, Forms, React, Margins, Toasts, Alerts, Parser } from "../webpack/common"; import ErrorBoundary from "./ErrorBoundary"; import { startPlugin } from "../plugins"; import { stopPlugin } from '../plugins/index'; import { Flex } from './Flex'; -import { isOutdated } from "../utils/updater"; -import { Updater } from "./Updater"; +import { ChangeList } from '../utils/ChangeList'; -export default ErrorBoundary.wrap(function Settings(props) { +function showErrorToast(message: string) { + Toasts.show({ + message, + type: Toasts.Type.FAILURE, + id: Toasts.genId(), + options: { + position: Toasts.Position.BOTTOM + } + }); +} + +export default ErrorBoundary.wrap(function Settings() { const [settingsDir, , settingsDirPending] = useAwaiter(() => VencordNative.ipc.invoke(IpcEvents.GET_SETTINGS_DIR), "Loading..."); - const [outdated, setOutdated] = React.useState(isOutdated); const settings = useSettings(); + const changes = React.useMemo(() => new ChangeList, []); + + React.useEffect(() => { + return () => void (changes.hasChanges && Alerts.show({ + title: "Restart required", + body: ( + <> +

The following plugins require a restart:

+
{changes.map((s, i) => ( + <> + {i > 0 && ", "} + {Parser.parse('`' + s + '`')} + + ))}
+ + ), + confirmText: "Restart now", + cancelText: "Later!", + onConfirm: () => location.reload() + })); + }, []); const depMap = React.useMemo(() => { const o = {} as Record; @@ -34,16 +64,7 @@ export default ErrorBoundary.wrap(function Settings(props) { return ( - {outdated && ( - <> - Updater - - - )} - - - - + Settings @@ -111,21 +132,19 @@ export default ErrorBoundary.wrap(function Settings(props) { p.dependencies?.forEach(d => { settings.plugins[d].enabled = true; if (!Plugins[d].started && !stopPlugin) { - // TODO show notification settings.plugins[p.name].enabled = false; + showErrorToast(`Failed to start dependency ${d}. Check the console for more info.`); } }); if (!p.started && !startPlugin(p)) { - // TODO show notification + showErrorToast(`Failed to start plugin ${p.name}. Check the console for more info.`); } } else { if (p.started && !stopPlugin(p)) { - // TODO show notification + showErrorToast(`Failed to stop plugin ${p.name}. Check the console for more info.`); } } - if (p.patches) { - // TODO show notification - } + if (p.patches) changes.handleChange(p.name); }} note={p.description} tooltipNote={ diff --git a/src/components/Updater.tsx b/src/components/Updater.tsx index e7b6d543..3d760f9e 100644 --- a/src/components/Updater.tsx +++ b/src/components/Updater.tsx @@ -1,13 +1,11 @@ import gitHash from "git-hash"; import { changes, checkForUpdates, getRepo, rebuild, update, UpdateLogger } from "../utils/updater"; -import { React, Forms, Button, Margins, Alerts, Card, Parser } from '../webpack/common'; +import { React, Forms, Button, Margins, Alerts, Card, Parser, Toasts } from '../webpack/common'; import { Flex } from "./Flex"; import { useAwaiter } from '../utils/misc'; import { Link } from "./Link"; +import ErrorBoundary from "./ErrorBoundary"; -interface Props { - setIsOutdated(b: boolean): void; -} function withDispatcher(dispatcher: React.Dispatch>, action: () => any) { return async () => { @@ -42,7 +40,7 @@ function withDispatcher(dispatcher: React.Dispatch }; }; -export function Updater(p: Props) { +export default ErrorBoundary.wrap(function Updater() { const [repo, err, repoPending] = useAwaiter(getRepo, "Loading..."); const [isChecking, setIsChecking] = React.useState(false); const [isUpdating, setIsUpdating] = React.useState(false); @@ -53,39 +51,48 @@ export function Updater(p: Props) { UpdateLogger.error("Failed to retrieve repo", err); }, [err]); + const isOutdated = updates.length > 0; + return ( - <> - Repo: {repoPending ? repo : err ? "Failed to retrieve - check console" : ( + + Repo + + {repoPending ? repo : err ? "Failed to retrieve - check console" : ( {repo.split("/").slice(-2).join("/")} )} ({gitHash}) + + + Updates + - There are {updates.length} Updates + {updates.length ? `There are ${updates.length} Updates` : "Up to Date!"} - - {updates.map(({ hash, author, message }) => ( -
- - {hash} - - {message} - {author} -
- ))} -
+ {updates.length > 0 && ( + + {updates.map(({ hash, author, message }) => ( +
+ + {hash} + + {message} - {author} +
+ ))} +
+ )} - + Update Now + } - +
); -} +}); diff --git a/src/components/index.ts b/src/components/index.ts index bf87b3e6..de53489d 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -1 +1,2 @@ export { default as Settings } from "./Settings"; +export { default as Updater } from "./Updater"; diff --git a/src/plugins/index.ts b/src/plugins/index.ts index e4d07752..e03c588d 100644 --- a/src/plugins/index.ts +++ b/src/plugins/index.ts @@ -23,37 +23,38 @@ export function startAllPlugins() { } export function startPlugin(p: Plugin) { - if (p.start) { - logger.info("Starting plugin", p.name); - if (p.started) { - logger.warn(`${p.name} already started`); - return false; - } - try { - p.start(); - p.started = true; - return true; - } catch (err: any) { - logger.error(`Failed to start ${p.name}\n`, err); - return false; - } + if (!p.start) return true; + + logger.info("Starting plugin", p.name); + if (p.started) { + logger.warn(`${p.name} already started`); + return false; + } + + try { + p.start(); + p.started = true; + return true; + } catch (err: any) { + logger.error(`Failed to start ${p.name}\n`, err); + return false; } } export function stopPlugin(p: Plugin) { - if (p.stop) { - logger.info("Stopping plugin", p.name); - if (!p.started) { - logger.warn(`${p.name} already stopped / never started`); - return false; - } - try { - p.stop(); - p.started = false; - return true; - } catch (err: any) { - logger.error(`Failed to stop ${p.name}\n`, err); - return false; - } + if (!p.stop) return true; + + logger.info("Stopping plugin", p.name); + if (!p.started) { + logger.warn(`${p.name} already stopped / never started`); + return false; + } + try { + p.stop(); + p.started = false; + return true; + } catch (err: any) { + logger.error(`Failed to stop ${p.name}\n`, err); + return false; } } diff --git a/src/plugins/settings.ts b/src/plugins/settings.ts index afefa91e..2ed85e63 100644 --- a/src/plugins/settings.ts +++ b/src/plugins/settings.ts @@ -27,7 +27,8 @@ export default definePlugin({ match: /\{section:(.{1,2})\.ID\.HEADER,\s*label:(.{1,2})\..{1,2}\.Messages\.ACTIVITY_SETTINGS\}/, replace: (m, mod) => `{section:${mod}.ID.HEADER,label:"Vencord"},` + - `{section:"Vencord",label:"Vencord",element:Vencord.Components.Settings},` + + `{section:"VencordSetting",label:"Vencord",element:Vencord.Components.Settings},` + + `{section:"VencordUpdater",label:"Updater",element:Vencord.Components.Updater},` + `{section:${mod}.ID.DIVIDER},${m}` } diff --git a/src/utils/ChangeList.ts b/src/utils/ChangeList.ts new file mode 100644 index 00000000..d8f74497 --- /dev/null +++ b/src/utils/ChangeList.ts @@ -0,0 +1,24 @@ +export class ChangeList{ + private set = new Set; + + public get changeCount() { + return this.set.size; + } + + public get hasChanges() { + return this.changeCount > 0; + } + + public handleChange(item: T) { + if (!this.set.delete(item)) + this.set.add(item); + } + + public getChanges() { + return this.set.values(); + } + + public map(mapper: (v: T, idx: number, arr: T[]) => R): R[] { + return [...this.getChanges()].map(mapper); + } +}