/* * Vencord, a modification for Discord's desktop app * Copyright (c) 2022 Vendicated and contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ import "./styles.css"; import * as DataStore from "@api/DataStore"; import { showNotice } from "@api/Notices"; import { Settings, useSettings } from "@api/Settings"; import { classNameFactory } from "@api/Styles"; import PluginModal from "@components/PluginSettings/PluginModal"; import { AddonCard } from "@components/VencordSettings/AddonCard"; import { SettingsTab } from "@components/VencordSettings/shared"; import { ChangeList } from "@utils/ChangeList"; import { Logger } from "@utils/Logger"; import { Margins } from "@utils/margins"; import { classes, isObjectEmpty } from "@utils/misc"; import { openModalLazy } from "@utils/modal"; import { LazyComponent, useAwaiter } from "@utils/react"; import { Plugin } from "@utils/types"; import { findByCode, findByPropsLazy } from "@webpack"; import { Alerts, Button, Card, Forms, Parser, React, Select, Text, TextInput, Toasts, Tooltip } from "@webpack/common"; import Plugins from "~plugins"; import { startDependenciesRecursive, startPlugin, stopPlugin } from "../../plugins"; const cl = classNameFactory("vc-plugins-"); const logger = new Logger("PluginSettings", "#a6d189"); const InputStyles = findByPropsLazy("inputDefault", "inputWrapper"); const ButtonClasses = findByPropsLazy("button", "disabled", "enabled"); const CogWheel = LazyComponent(() => findByCode("18.564C15.797 19.099 14.932 19.498 14 19.738V22H10V19.738C9.069")); const InfoIcon = LazyComponent(() => findByCode("4.4408921e-16 C4.4771525,-1.77635684e-15 4.4408921e-16")); function showErrorToast(message: string) { Toasts.show({ message, type: Toasts.Type.FAILURE, id: Toasts.genId(), options: { position: Toasts.Position.BOTTOM } }); } function ReloadRequiredCard({ required }: { required: boolean; }) { return ( {required ? ( <> Restart required! Restart now to apply new plugins and their settings ) : ( <> Plugin Management Press the cog wheel or info icon to get more info on a plugin Plugins with a cog wheel have settings you can modify! )} ); } interface PluginCardProps extends React.HTMLProps { plugin: Plugin; disabled: boolean; onRestartNeeded(name: string): void; isNew?: boolean; } export function PluginCard({ plugin, disabled, onRestartNeeded, onMouseEnter, onMouseLeave, isNew }: PluginCardProps) { const settings = Settings.plugins[plugin.name]; const isEnabled = () => settings.enabled ?? false; function openModal() { openModalLazy(async () => { return modalProps => { return onRestartNeeded(plugin.name)} />; }; }); } function toggleEnabled() { const wasEnabled = isEnabled(); // If we're enabling a plugin, make sure all deps are enabled recursively. if (!wasEnabled) { const { restartNeeded, failures } = startDependenciesRecursive(plugin); if (failures.length) { logger.error(`Failed to start dependencies for ${plugin.name}: ${failures.join(", ")}`); showNotice("Failed to start dependencies: " + failures.join(", "), "Close", () => null); return; } else if (restartNeeded) { // If any dependencies have patches, don't start the plugin yet. settings.enabled = true; onRestartNeeded(plugin.name); return; } } // if the plugin has patches, dont use stopPlugin/startPlugin. Wait for restart to apply changes. if (plugin.patches?.length) { settings.enabled = !wasEnabled; onRestartNeeded(plugin.name); return; } // If the plugin is enabled, but hasn't been started, then we can just toggle it off. if (wasEnabled && !plugin.started) { settings.enabled = !wasEnabled; return; } const result = wasEnabled ? stopPlugin(plugin) : startPlugin(plugin); if (!result) { settings.enabled = false; const msg = `Error while ${wasEnabled ? "stopping" : "starting"} plugin ${plugin.name}`; logger.error(msg); showErrorToast(msg); return; } settings.enabled = !wasEnabled; } return ( openModal()} className={classes(ButtonClasses.button, cl("info-button"))}> {plugin.options && !isObjectEmpty(plugin.options) ? : } } /> ); } const enum SearchStatus { ALL, ENABLED, DISABLED, NEW } export default function PluginSettings() { 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; for (const plugin in Plugins) { const deps = Plugins[plugin].dependencies; if (deps) { for (const dep of deps) { o[dep] ??= []; o[dep].push(plugin); } } } return o; }, []); const sortedPlugins = React.useMemo(() => Object.values(Plugins) .sort((a, b) => a.name.localeCompare(b.name)), []); const [searchValue, setSearchValue] = React.useState({ value: "", status: SearchStatus.ALL }); const onSearch = (query: string) => setSearchValue(prev => ({ ...prev, value: query })); const onStatusChange = (status: SearchStatus) => setSearchValue(prev => ({ ...prev, status })); const pluginFilter = (plugin: typeof Plugins[keyof typeof Plugins]) => { const enabled = settings.plugins[plugin.name]?.enabled; if (enabled && searchValue.status === SearchStatus.DISABLED) return false; if (!enabled && searchValue.status === SearchStatus.ENABLED) return false; if (searchValue.status === SearchStatus.NEW && !newPlugins?.includes(plugin.name)) return false; if (!searchValue.value.length) return true; const v = searchValue.value.toLowerCase(); return ( plugin.name.toLowerCase().includes(v) || plugin.description.toLowerCase().includes(v) || plugin.tags?.some(t => t.toLowerCase().includes(v)) ); }; const [newPlugins] = useAwaiter(() => DataStore.get("Vencord_existingPlugins").then((cachedPlugins: Record | undefined) => { const now = Date.now() / 1000; const existingTimestamps: Record = {}; const sortedPluginNames = Object.values(sortedPlugins).map(plugin => plugin.name); const newPlugins: string[] = []; for (const { name: p } of sortedPlugins) { const time = existingTimestamps[p] = cachedPlugins?.[p] ?? now; if ((time + 60 * 60 * 24 * 2) > now) { newPlugins.push(p); } } DataStore.set("Vencord_existingPlugins", existingTimestamps); return window._.isEqual(newPlugins, sortedPluginNames) ? [] : newPlugins; })); type P = JSX.Element | JSX.Element[]; let plugins: P, requiredPlugins: P; if (sortedPlugins?.length) { plugins = []; requiredPlugins = []; for (const p of sortedPlugins) { if (!p.options && p.name.endsWith("API") && searchValue.value !== "API") continue; if (!pluginFilter(p)) continue; const isRequired = p.required || depMap[p.name]?.some(d => settings.plugins[d].enabled); if (isRequired) { const tooltipText = p.required ? "This plugin is required for Vencord to function." : makeDependencyList(depMap[p.name]?.filter(d => settings.plugins[d].enabled)); requiredPlugins.push( {({ onMouseLeave, onMouseEnter }) => ( changes.handleChange(name)} disabled={true} plugin={p} /> )} ); } else { plugins.push( changes.handleChange(name)} disabled={false} plugin={p} isNew={newPlugins?.includes(p.name)} key={p.name} /> ); } } } else { plugins = requiredPlugins = No plugins meet search criteria.; } return ( Filters