2022-10-21 23:17:06 +00:00
|
|
|
/*
|
|
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-01-03 01:30:54 +00:00
|
|
|
import "./styles.css";
|
|
|
|
|
2022-12-23 02:17:19 +00:00
|
|
|
import * as DataStore from "@api/DataStore";
|
2022-11-28 12:37:55 +00:00
|
|
|
import { showNotice } from "@api/Notices";
|
2023-05-11 23:40:43 +00:00
|
|
|
import { Settings, useSettings } from "@api/Settings";
|
2023-01-03 01:30:54 +00:00
|
|
|
import { classNameFactory } from "@api/Styles";
|
2023-09-23 01:15:07 +00:00
|
|
|
import { CogWheel, InfoIcon } from "@components/Icons";
|
2022-12-23 02:17:19 +00:00
|
|
|
import PluginModal from "@components/PluginSettings/PluginModal";
|
2023-08-04 17:52:20 +00:00
|
|
|
import { AddonCard } from "@components/VencordSettings/AddonCard";
|
2023-05-11 23:40:43 +00:00
|
|
|
import { SettingsTab } from "@components/VencordSettings/shared";
|
2022-11-28 12:37:55 +00:00
|
|
|
import { ChangeList } from "@utils/ChangeList";
|
2024-05-05 02:15:33 +00:00
|
|
|
import { proxyLazy } from "@utils/lazy";
|
2023-05-05 23:36:00 +00:00
|
|
|
import { Logger } from "@utils/Logger";
|
2023-03-01 20:35:08 +00:00
|
|
|
import { Margins } from "@utils/margins";
|
2023-09-05 18:10:42 +00:00
|
|
|
import { classes, isObjectEmpty } from "@utils/misc";
|
2023-10-31 22:56:13 +00:00
|
|
|
import { openModalLazy } from "@utils/modal";
|
2023-09-23 01:15:07 +00:00
|
|
|
import { useAwaiter } from "@utils/react";
|
2022-11-28 12:37:55 +00:00
|
|
|
import { Plugin } from "@utils/types";
|
2023-09-23 01:15:07 +00:00
|
|
|
import { findByPropsLazy } from "@webpack";
|
2023-10-25 19:01:20 +00:00
|
|
|
import { Alerts, Button, Card, Forms, lodash, Parser, React, Select, Text, TextInput, Toasts, Tooltip } from "@webpack/common";
|
2022-11-28 12:37:55 +00:00
|
|
|
|
2022-10-23 21:23:52 +00:00
|
|
|
import Plugins from "~plugins";
|
2022-10-17 19:18:25 +00:00
|
|
|
|
2024-05-05 02:15:33 +00:00
|
|
|
// Avoid circular dependency
|
|
|
|
const { startDependenciesRecursive, startPlugin, stopPlugin } = proxyLazy(() => require("../../plugins"));
|
2023-01-15 21:26:02 +00:00
|
|
|
|
|
|
|
const cl = classNameFactory("vc-plugins-");
|
2022-10-23 18:09:02 +00:00
|
|
|
const logger = new Logger("PluginSettings", "#a6d189");
|
|
|
|
|
2022-11-28 12:37:55 +00:00
|
|
|
const InputStyles = findByPropsLazy("inputDefault", "inputWrapper");
|
2023-04-04 13:26:53 +00:00
|
|
|
const ButtonClasses = findByPropsLazy("button", "disabled", "enabled");
|
2022-10-17 19:18:25 +00:00
|
|
|
|
2022-10-22 21:38:48 +00:00
|
|
|
|
2022-10-17 19:18:25 +00:00
|
|
|
function showErrorToast(message: string) {
|
|
|
|
Toasts.show({
|
|
|
|
message,
|
|
|
|
type: Toasts.Type.FAILURE,
|
|
|
|
id: Toasts.genId(),
|
|
|
|
options: {
|
|
|
|
position: Toasts.Position.BOTTOM
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:26:02 +00:00
|
|
|
function ReloadRequiredCard({ required }: { required: boolean; }) {
|
2022-10-23 18:09:02 +00:00
|
|
|
return (
|
2023-01-15 21:26:02 +00:00
|
|
|
<Card className={cl("info-card", { "restart-card": required })}>
|
|
|
|
{required ? (
|
|
|
|
<>
|
|
|
|
<Forms.FormTitle tag="h5">Restart required!</Forms.FormTitle>
|
|
|
|
<Forms.FormText className={cl("dep-text")}>
|
|
|
|
Restart now to apply new plugins and their settings
|
|
|
|
</Forms.FormText>
|
|
|
|
<Button color={Button.Colors.YELLOW} onClick={() => location.reload()}>
|
|
|
|
Restart
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Forms.FormTitle tag="h5">Plugin Management</Forms.FormTitle>
|
|
|
|
<Forms.FormText>Press the cog wheel or info icon to get more info on a plugin</Forms.FormText>
|
|
|
|
<Forms.FormText>Plugins with a cog wheel have settings you can modify!</Forms.FormText>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Card>
|
2022-10-23 18:09:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-17 19:18:25 +00:00
|
|
|
interface PluginCardProps extends React.HTMLProps<HTMLDivElement> {
|
|
|
|
plugin: Plugin;
|
|
|
|
disabled: boolean;
|
2022-10-23 18:09:02 +00:00
|
|
|
onRestartNeeded(name: string): void;
|
2022-12-23 02:17:19 +00:00
|
|
|
isNew?: boolean;
|
2022-10-17 19:18:25 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 01:42:20 +00:00
|
|
|
export function PluginCard({ plugin, disabled, onRestartNeeded, onMouseEnter, onMouseLeave, isNew }: PluginCardProps) {
|
2023-05-11 23:40:43 +00:00
|
|
|
const settings = Settings.plugins[plugin.name];
|
2022-10-23 18:09:02 +00:00
|
|
|
|
2023-01-23 21:43:25 +00:00
|
|
|
const isEnabled = () => settings.enabled ?? false;
|
2022-10-17 19:18:25 +00:00
|
|
|
|
2023-10-31 22:56:13 +00:00
|
|
|
function openModal() {
|
|
|
|
openModalLazy(async () => {
|
|
|
|
return modalProps => {
|
|
|
|
return <PluginModal {...modalProps} plugin={plugin} onRestartNeeded={() => onRestartNeeded(plugin.name)} />;
|
|
|
|
};
|
|
|
|
});
|
2022-10-17 19:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function toggleEnabled() {
|
2022-10-23 18:09:02 +00:00
|
|
|
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.
|
2023-01-23 21:43:25 +00:00
|
|
|
settings.enabled = true;
|
2022-10-23 18:09:02 +00:00
|
|
|
onRestartNeeded(plugin.name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the plugin has patches, dont use stopPlugin/startPlugin. Wait for restart to apply changes.
|
2023-05-05 23:36:00 +00:00
|
|
|
if (plugin.patches?.length) {
|
2023-01-23 21:43:25 +00:00
|
|
|
settings.enabled = !wasEnabled;
|
2022-10-23 18:09:02 +00:00
|
|
|
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) {
|
2023-01-23 21:43:25 +00:00
|
|
|
settings.enabled = !wasEnabled;
|
2022-10-23 18:09:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = wasEnabled ? stopPlugin(plugin) : startPlugin(plugin);
|
|
|
|
|
2022-10-17 19:18:25 +00:00
|
|
|
if (!result) {
|
2023-06-13 00:29:15 +00:00
|
|
|
settings.enabled = false;
|
|
|
|
|
|
|
|
const msg = `Error while ${wasEnabled ? "stopping" : "starting"} plugin ${plugin.name}`;
|
|
|
|
logger.error(msg);
|
|
|
|
showErrorToast(msg);
|
2022-10-17 19:18:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-10-23 18:09:02 +00:00
|
|
|
|
2023-01-23 21:43:25 +00:00
|
|
|
settings.enabled = !wasEnabled;
|
2022-10-17 19:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-08-04 17:52:20 +00:00
|
|
|
<AddonCard
|
|
|
|
name={plugin.name}
|
|
|
|
description={plugin.description}
|
|
|
|
isNew={isNew}
|
|
|
|
enabled={isEnabled()}
|
|
|
|
setEnabled={toggleEnabled}
|
|
|
|
disabled={disabled}
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
infoButton={
|
2023-10-31 22:56:13 +00:00
|
|
|
<button role="switch" onClick={() => openModal()} className={classes(ButtonClasses.button, cl("info-button"))}>
|
2023-09-05 18:10:42 +00:00
|
|
|
{plugin.options && !isObjectEmpty(plugin.options)
|
2023-01-15 21:26:02 +00:00
|
|
|
? <CogWheel />
|
2023-09-23 01:15:07 +00:00
|
|
|
: <InfoIcon />}
|
2023-01-15 21:26:02 +00:00
|
|
|
</button>
|
2023-08-04 17:52:20 +00:00
|
|
|
}
|
|
|
|
/>
|
2022-10-17 19:18:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-13 00:36:25 +00:00
|
|
|
const enum SearchStatus {
|
2023-01-09 14:57:02 +00:00
|
|
|
ALL,
|
|
|
|
ENABLED,
|
2023-07-13 17:35:40 +00:00
|
|
|
DISABLED,
|
|
|
|
NEW
|
2023-01-09 14:57:02 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 23:40:43 +00:00
|
|
|
export default function PluginSettings() {
|
2022-10-17 19:18:25 +00:00
|
|
|
const settings = useSettings();
|
|
|
|
const changes = React.useMemo(() => new ChangeList<string>(), []);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
return () => void (changes.hasChanges && Alerts.show({
|
|
|
|
title: "Restart required",
|
|
|
|
body: (
|
|
|
|
<>
|
|
|
|
<p>The following plugins require a restart:</p>
|
|
|
|
<div>{changes.map((s, i) => (
|
|
|
|
<>
|
|
|
|
{i > 0 && ", "}
|
|
|
|
{Parser.parse("`" + s + "`")}
|
|
|
|
</>
|
|
|
|
))}</div>
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
confirmText: "Restart now",
|
|
|
|
cancelText: "Later!",
|
|
|
|
onConfirm: () => location.reload()
|
|
|
|
}));
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const depMap = React.useMemo(() => {
|
|
|
|
const o = {} as Record<string, string[]>;
|
|
|
|
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)), []);
|
|
|
|
|
2023-01-09 14:57:02 +00:00
|
|
|
const [searchValue, setSearchValue] = React.useState({ value: "", status: SearchStatus.ALL });
|
2022-10-17 19:18:25 +00:00
|
|
|
|
|
|
|
const onSearch = (query: string) => setSearchValue(prev => ({ ...prev, value: query }));
|
2023-01-09 14:57:02 +00:00
|
|
|
const onStatusChange = (status: SearchStatus) => setSearchValue(prev => ({ ...prev, status }));
|
2022-10-17 19:18:25 +00:00
|
|
|
|
|
|
|
const pluginFilter = (plugin: typeof Plugins[keyof typeof Plugins]) => {
|
2023-02-09 18:36:30 +00:00
|
|
|
const enabled = settings.plugins[plugin.name]?.enabled;
|
2023-01-09 14:57:02 +00:00
|
|
|
if (enabled && searchValue.status === SearchStatus.DISABLED) return false;
|
|
|
|
if (!enabled && searchValue.status === SearchStatus.ENABLED) return false;
|
2023-07-13 17:35:40 +00:00
|
|
|
if (searchValue.status === SearchStatus.NEW && !newPlugins?.includes(plugin.name)) return false;
|
2023-01-09 14:57:02 +00:00
|
|
|
if (!searchValue.value.length) return true;
|
2023-05-12 01:41:00 +00:00
|
|
|
|
|
|
|
const v = searchValue.value.toLowerCase();
|
2022-10-17 19:18:25 +00:00
|
|
|
return (
|
2023-05-12 01:41:00 +00:00
|
|
|
plugin.name.toLowerCase().includes(v) ||
|
|
|
|
plugin.description.toLowerCase().includes(v) ||
|
|
|
|
plugin.tags?.some(t => t.toLowerCase().includes(v))
|
2022-10-17 19:18:25 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-12-23 02:17:19 +00:00
|
|
|
const [newPlugins] = useAwaiter(() => DataStore.get("Vencord_existingPlugins").then((cachedPlugins: Record<string, number> | undefined) => {
|
|
|
|
const now = Date.now() / 1000;
|
|
|
|
const existingTimestamps: Record<string, number> = {};
|
|
|
|
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);
|
|
|
|
|
2023-10-25 18:29:32 +00:00
|
|
|
return lodash.isEqual(newPlugins, sortedPluginNames) ? [] : newPlugins;
|
2022-12-23 02:17:19 +00:00
|
|
|
}));
|
|
|
|
|
2023-01-09 14:57:02 +00:00
|
|
|
type P = JSX.Element | JSX.Element[];
|
|
|
|
let plugins: P, requiredPlugins: P;
|
|
|
|
if (sortedPlugins?.length) {
|
|
|
|
plugins = [];
|
|
|
|
requiredPlugins = [];
|
|
|
|
|
2024-06-05 23:19:53 +00:00
|
|
|
const showApi = searchValue.value === "API";
|
2023-01-09 14:57:02 +00:00
|
|
|
for (const p of sortedPlugins) {
|
2024-06-05 23:19:53 +00:00
|
|
|
if (p.hidden || (!p.options && p.name.endsWith("API") && !showApi))
|
2023-05-11 00:38:59 +00:00
|
|
|
continue;
|
|
|
|
|
2023-01-09 14:57:02 +00:00
|
|
|
if (!pluginFilter(p)) continue;
|
|
|
|
|
2023-01-09 15:23:40 +00:00
|
|
|
const isRequired = p.required || depMap[p.name]?.some(d => settings.plugins[d].enabled);
|
2023-01-09 14:57:02 +00:00
|
|
|
|
|
|
|
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(
|
|
|
|
<Tooltip text={tooltipText} key={p.name}>
|
|
|
|
{({ onMouseLeave, onMouseEnter }) => (
|
|
|
|
<PluginCard
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onRestartNeeded={name => changes.handleChange(name)}
|
|
|
|
disabled={true}
|
|
|
|
plugin={p}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
plugins.push(
|
|
|
|
<PluginCard
|
|
|
|
onRestartNeeded={name => changes.handleChange(name)}
|
|
|
|
disabled={false}
|
|
|
|
plugin={p}
|
|
|
|
isNew={newPlugins?.includes(p.name)}
|
|
|
|
key={p.name}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
plugins = requiredPlugins = <Text variant="text-md/normal">No plugins meet search criteria.</Text>;
|
|
|
|
}
|
|
|
|
|
2022-10-17 19:18:25 +00:00
|
|
|
return (
|
2023-05-11 23:40:43 +00:00
|
|
|
<SettingsTab title="Plugins">
|
2023-01-15 21:26:02 +00:00
|
|
|
<ReloadRequiredCard required={changes.hasChanges} />
|
|
|
|
|
2023-03-01 20:35:08 +00:00
|
|
|
<Forms.FormTitle tag="h5" className={classes(Margins.top20, Margins.bottom8)}>
|
2022-11-25 22:38:55 +00:00
|
|
|
Filters
|
2022-10-17 19:18:25 +00:00
|
|
|
</Forms.FormTitle>
|
2022-10-23 18:09:02 +00:00
|
|
|
|
2023-01-03 01:30:54 +00:00
|
|
|
<div className={cl("filter-controls")}>
|
2023-03-01 20:35:08 +00:00
|
|
|
<TextInput autoFocus value={searchValue.value} placeholder="Search for a plugin..." onChange={onSearch} className={Margins.bottom20} />
|
2022-10-17 19:18:25 +00:00
|
|
|
<div className={InputStyles.inputWrapper}>
|
|
|
|
<Select
|
|
|
|
options={[
|
2023-01-09 14:57:02 +00:00
|
|
|
{ label: "Show All", value: SearchStatus.ALL, default: true },
|
|
|
|
{ label: "Show Enabled", value: SearchStatus.ENABLED },
|
2023-07-13 17:35:40 +00:00
|
|
|
{ label: "Show Disabled", value: SearchStatus.DISABLED },
|
|
|
|
{ label: "Show New", value: SearchStatus.NEW }
|
2022-10-17 19:18:25 +00:00
|
|
|
]}
|
2022-11-01 00:49:41 +00:00
|
|
|
serialize={String}
|
2022-10-17 19:18:25 +00:00
|
|
|
select={onStatusChange}
|
|
|
|
isSelected={v => v === searchValue.status}
|
|
|
|
closeOnSelect={true}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2023-03-01 20:35:08 +00:00
|
|
|
<Forms.FormTitle className={Margins.top20}>Plugins</Forms.FormTitle>
|
2022-11-25 22:38:55 +00:00
|
|
|
|
2023-01-03 01:30:54 +00:00
|
|
|
<div className={cl("grid")}>
|
2023-01-09 14:57:02 +00:00
|
|
|
{plugins}
|
2022-10-17 19:18:25 +00:00
|
|
|
</div>
|
2023-01-25 02:25:29 +00:00
|
|
|
|
2023-03-01 20:35:08 +00:00
|
|
|
<Forms.FormDivider className={Margins.top20} />
|
2023-01-25 02:25:29 +00:00
|
|
|
|
2023-03-01 20:35:08 +00:00
|
|
|
<Forms.FormTitle tag="h5" className={classes(Margins.top20, Margins.bottom8)}>
|
2022-10-17 19:18:25 +00:00
|
|
|
Required Plugins
|
|
|
|
</Forms.FormTitle>
|
2023-01-03 01:30:54 +00:00
|
|
|
<div className={cl("grid")}>
|
2023-01-09 14:57:02 +00:00
|
|
|
{requiredPlugins}
|
2022-10-17 19:18:25 +00:00
|
|
|
</div>
|
2023-05-11 23:40:43 +00:00
|
|
|
</SettingsTab >
|
2022-10-17 19:18:25 +00:00
|
|
|
);
|
2023-05-11 23:40:43 +00:00
|
|
|
}
|
2022-10-17 19:18:25 +00:00
|
|
|
|
|
|
|
function makeDependencyList(deps: string[]) {
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
<Forms.FormText>This plugin is required by:</Forms.FormText>
|
2023-10-31 22:56:13 +00:00
|
|
|
{deps.map((dep: string) => <Forms.FormText className={cl("dep-text")}>{dep}</Forms.FormText>)}
|
2022-10-17 19:18:25 +00:00
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
}
|