2025-07-15 15:57:24 +02:00
|
|
|
/*
|
|
|
|
|
* Vencord, a Discord client mod
|
|
|
|
|
* Copyright (c) 2025 Vendicated and contributors
|
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { showNotice } from "@api/Notices";
|
2025-11-16 15:17:35 +01:00
|
|
|
import { isPluginEnabled, startDependenciesRecursive, startPlugin, stopPlugin } from "@api/PluginManager";
|
2025-07-15 15:57:24 +02:00
|
|
|
import { CogWheel, InfoIcon } from "@components/Icons";
|
|
|
|
|
import { AddonCard } from "@components/settings/AddonCard";
|
2025-10-11 01:42:14 +02:00
|
|
|
import { isObjectEmpty } from "@utils/misc";
|
2025-07-15 15:57:24 +02:00
|
|
|
import { Plugin } from "@utils/types";
|
|
|
|
|
import { React, showToast, Toasts } from "@webpack/common";
|
|
|
|
|
import { Settings } from "Vencord";
|
|
|
|
|
|
|
|
|
|
import { cl, logger } from ".";
|
|
|
|
|
import { openPluginModal } from "./PluginModal";
|
|
|
|
|
|
|
|
|
|
interface PluginCardProps extends React.HTMLProps<HTMLDivElement> {
|
|
|
|
|
plugin: Plugin;
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
onRestartNeeded(name: string, key: string): void;
|
|
|
|
|
isNew?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function PluginCard({ plugin, disabled, onRestartNeeded, onMouseEnter, onMouseLeave, isNew }: PluginCardProps) {
|
|
|
|
|
const settings = Settings.plugins[plugin.name];
|
|
|
|
|
|
|
|
|
|
const isEnabled = () => isPluginEnabled(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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (restartNeeded) {
|
|
|
|
|
// If any dependencies have patches, don't start the plugin yet.
|
|
|
|
|
settings.enabled = true;
|
|
|
|
|
onRestartNeeded(plugin.name, "enabled");
|
|
|
|
|
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, "enabled");
|
|
|
|
|
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}`;
|
|
|
|
|
showToast(msg, Toasts.Type.FAILURE, {
|
|
|
|
|
position: Toasts.Position.BOTTOM,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
settings.enabled = !wasEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<AddonCard
|
|
|
|
|
name={plugin.name}
|
|
|
|
|
description={plugin.description}
|
|
|
|
|
isNew={isNew}
|
|
|
|
|
enabled={isEnabled()}
|
|
|
|
|
setEnabled={toggleEnabled}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
|
infoButton={
|
|
|
|
|
<button
|
|
|
|
|
role="switch"
|
|
|
|
|
onClick={() => openPluginModal(plugin, onRestartNeeded)}
|
2025-10-11 01:42:14 +02:00
|
|
|
className={cl("info-button")}
|
2025-07-15 15:57:24 +02:00
|
|
|
>
|
|
|
|
|
{plugin.options && !isObjectEmpty(plugin.options)
|
|
|
|
|
? <CogWheel className={cl("info-icon")} />
|
|
|
|
|
: <InfoIcon className={cl("info-icon")} />
|
|
|
|
|
}
|
|
|
|
|
</button>
|
|
|
|
|
} />
|
|
|
|
|
);
|
|
|
|
|
}
|