From 317c38693331a9d6c7affc91219d1ff0a540a0c7 Mon Sep 17 00:00:00 2001 From: Sqaaakoi Date: Fri, 23 Aug 2024 15:53:51 +1200 Subject: [PATCH] New Settings menu update --- NewPluginsModal.tsx | 111 ++++++++++++++++++++++++++++---------------- index.tsx | 9 ++-- knownSettings.ts | 20 +++++--- 3 files changed, 91 insertions(+), 49 deletions(-) diff --git a/NewPluginsModal.tsx b/NewPluginsModal.tsx index 12a079e..a2a25c2 100644 --- a/NewPluginsModal.tsx +++ b/NewPluginsModal.tsx @@ -8,12 +8,14 @@ import { useSettings } from "@api/Settings"; import { classNameFactory } from "@api/Styles"; import { PluginCard } from "@components/PluginSettings"; import { ChangeList } from "@utils/ChangeList"; -import { ModalContent, ModalFooter, ModalHeader, ModalProps, ModalRoot, ModalSize, openModal } from "@utils/modal"; -import { Alerts, Button, Flex, Forms, Parser, React, Text, Tooltip, useMemo } from "@webpack/common"; +import { ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalProps, ModalRoot, ModalSize, openModal } from "@utils/modal"; +import { useForceUpdater } from "@utils/react"; +import { Button, Flex, Forms, Parser, React, Text, Tooltip, useMemo } from "@webpack/common"; +import { JSX } from "react"; import Plugins from "~plugins"; -import { getNewPlugins, writeKnownPlugins } from "./knownPlugins"; +import { getNewPlugins, getNewSettings, KnownPluginSettingsMap, writeKnownSettings } from "./knownSettings"; const cl = classNameFactory("vc-plugins-"); @@ -21,29 +23,10 @@ let hasSeen = false; // Most of this was stolen from PluginSettings directly. -export function NewPluginsModal({ modalProps, newPlugins }: { modalProps: ModalProps; newPlugins: Set; }) { +export function NewPluginsModal({ modalProps, newPlugins, newSettings }: { modalProps: ModalProps; newPlugins: Set; newSettings: KnownPluginSettingsMap; }) { 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() - })); - }, []); + let updateContinueButton = () => { }; const depMap = React.useMemo(() => { const o = {} as Record; @@ -59,8 +42,13 @@ export function NewPluginsModal({ modalProps, newPlugins }: { modalProps: ModalP return o; }, []); - const sortedPlugins = useMemo(() => [...newPlugins].map(pn => Plugins[pn]) - .sort((a, b) => a.name.localeCompare(b.name)), []); + const mapPlugins = (array: string[]) => array.map(pn => Plugins[pn]) + .sort((a, b) => a.name.localeCompare(b.name)); + + const sortedPlugins = useMemo(() => [ + ...mapPlugins([...newPlugins]), + ...mapPlugins([...newSettings.keys()].filter(p => !newPlugins.has(p))) + ], []); const plugins = [] as JSX.Element[]; const requiredPlugins = [] as JSX.Element[]; @@ -82,10 +70,14 @@ export function NewPluginsModal({ modalProps, newPlugins }: { modalProps: ModalP changes.handleChange(name)} + onRestartNeeded={name => { + changes.handleChange(name); + updateContinueButton(); + }} disabled={true} plugin={p} key={p.name} + isNew={newPlugins.has(p.name)} /> )} @@ -93,10 +85,14 @@ export function NewPluginsModal({ modalProps, newPlugins }: { modalProps: ModalP } else { plugins.push( changes.handleChange(name)} + onRestartNeeded={name => { + changes.handleChange(name); + updateContinueButton(); + }} disabled={false} plugin={p} key={p.name} + isNew={newPlugins.has(p.name)} /> ); } @@ -105,7 +101,16 @@ export function NewPluginsModal({ modalProps, newPlugins }: { modalProps: ModalP return - New Plugins ({[...plugins, ...requiredPlugins].length}) + New Plugins and Settings ({[...plugins, ...requiredPlugins].length}) + + {tooltipProps => + + } +
@@ -114,20 +119,46 @@ export function NewPluginsModal({ modalProps, newPlugins }: { modalProps: ModalP - + void) => updateContinueButton = v} + /> ; } +function ContinueButton(props: { callback: (update: () => void) => void; changes: ChangeList; close: () => any; }) { + const update = useForceUpdater(); + props.callback(update); + return + The following plugins require a restart: +
{props.changes.map((s, i) => ( + <> + {i > 0 && ", "} + {Parser.parse("`" + s + "`")} + + ))}
+ } + shouldShow={props.changes.hasChanges} + > + {tooltipProps => + + } +
; +} + function makeDependencyList(deps: string[]) { return ( @@ -140,12 +171,14 @@ function makeDependencyList(deps: string[]) { export async function openNewPluginsModal() { const newPlugins = await getNewPlugins(); - if (newPlugins.size && !hasSeen) { + const newSettings = await getNewSettings(); + if (newSettings.size && !hasSeen) { hasSeen = true; openModal(modalProps => ( )); } diff --git a/index.tsx b/index.tsx index 9ab045c..4bff929 100644 --- a/index.tsx +++ b/index.tsx @@ -7,19 +7,22 @@ import { Devs } from "@utils/constants"; import definePlugin from "@utils/types"; -import { KNOWN_PLUGINS_DATA_KEY } from "./knownPlugins"; +import * as KnownSettings from "./knownSettings"; +import { KNOWN_PLUGINS_LEGACY_DATA_KEY, KNOWN_SETTINGS_DATA_KEY } from "./knownSettings"; import { openNewPluginsModal } from "./NewPluginsModal"; export default definePlugin({ name: "NewPluginsManager", description: "Utility that notifies you when new plugins are added to Vencord", authors: [Devs.Sqaaakoi], - enabledByDefault: true, + enabledByDefault: true, // This is intentional. flux: { async POST_CONNECTION_OPEN() { openNewPluginsModal(); } }, openNewPluginsModal, - KNOWN_PLUGINS_DATA_KEY + KNOWN_PLUGINS_LEGACY_DATA_KEY, + KNOWN_SETTINGS_DATA_KEY, + KnownSettings }); diff --git a/knownSettings.ts b/knownSettings.ts index e1417ac..179c9e4 100644 --- a/knownSettings.ts +++ b/knownSettings.ts @@ -11,11 +11,6 @@ import plugins from "~plugins"; export type KnownPluginSettingsMap = Map>; -type PluginSettings = { - [setting: string]: any; - enabled?: boolean; -}; - export const KNOWN_PLUGINS_LEGACY_DATA_KEY = "NewPluginsManager_KnownPlugins"; export const KNOWN_SETTINGS_DATA_KEY = "NewPluginsManager_KnownSettings"; @@ -35,7 +30,7 @@ export async function getKnownSettings(): Promise { let map = await DataStore.get(KNOWN_SETTINGS_DATA_KEY) as KnownPluginSettingsMap; if (map === undefined) { const knownPlugins = await DataStore.get(KNOWN_PLUGINS_LEGACY_DATA_KEY) ?? [] as string[]; - DataStore.del(KNOWN_PLUGINS_LEGACY_DATA_KEY); + // DataStore.del(KNOWN_PLUGINS_LEGACY_DATA_KEY); const Plugins = [...Object.keys(plugins), ...knownPlugins]; map = getCurrentSettings(Plugins); DataStore.set(KNOWN_SETTINGS_DATA_KEY, map); @@ -48,7 +43,7 @@ export async function getNewSettings(): Promise { const knownSettings = await getKnownSettings(); map.forEach((settings, plugin) => { const filteredSettings = [...settings].filter(setting => !knownSettings.get(plugin)?.has(setting)); - if (filteredSettings.length === 0 && knownSettings.has(plugin)) return map.delete(plugin); + if (!filteredSettings.length) return map.delete(plugin); map.set(plugin, new Set(filteredSettings)); }); return map; @@ -77,3 +72,14 @@ export async function writeKnownSettings() { }); await DataStore.set(KNOWN_SETTINGS_DATA_KEY, allSettings); } + +export async function debugWipeSomeData() { + const settings = await getKnownSettings(); + settings.forEach((value, key) => { + if (Math.random() > 0.8) { + if (Math.random() > 0.5) return settings.set(key, new Set([...value].filter(() => Math.random() > 0.5))); + return settings.delete(key); + } + }); + await DataStore.set(KNOWN_SETTINGS_DATA_KEY, settings); +}