2022-10-22 01:17:06 +02: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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-06-20 04:40:07 +02:00
|
|
|
import "./PluginModal.css";
|
|
|
|
|
|
2022-11-28 13:37:55 +01:00
|
|
|
import { generateId } from "@api/Commands";
|
2023-05-06 01:36:00 +02:00
|
|
|
import { useSettings } from "@api/Settings";
|
2024-06-20 04:40:07 +02:00
|
|
|
import { classNameFactory } from "@api/Styles";
|
2022-11-28 13:58:14 +01:00
|
|
|
import ErrorBoundary from "@components/ErrorBoundary";
|
|
|
|
|
import { Flex } from "@components/Flex";
|
2025-07-15 15:57:24 +02:00
|
|
|
import { debounce } from "@shared/debounce";
|
2024-06-20 04:40:07 +02:00
|
|
|
import { gitRemote } from "@shared/vencordUserAgent";
|
2023-05-06 01:36:00 +02:00
|
|
|
import { proxyLazy } from "@utils/lazy";
|
2023-04-08 23:28:12 +02:00
|
|
|
import { Margins } from "@utils/margins";
|
2025-07-09 18:42:38 -04:00
|
|
|
import { isObjectEmpty } from "@utils/misc";
|
2025-07-15 15:57:24 +02:00
|
|
|
import { ModalCloseButton, ModalContent, ModalHeader, ModalProps, ModalRoot, ModalSize, openModal } from "@utils/modal";
|
2022-11-28 13:37:55 +01:00
|
|
|
import { OptionType, Plugin } from "@utils/types";
|
2025-07-10 00:38:39 +02:00
|
|
|
import { User } from "@vencord/discord-types";
|
2025-07-15 15:57:24 +02:00
|
|
|
import { findByPropsLazy } from "@webpack";
|
|
|
|
|
import { Clickable, FluxDispatcher, Forms, React, Text, Tooltip, useEffect, UserStore, UserSummaryItem, UserUtils, useState } from "@webpack/common";
|
2022-10-17 20:18:25 +01:00
|
|
|
import { Constructor } from "type-fest";
|
|
|
|
|
|
2024-06-20 04:40:07 +02:00
|
|
|
import { PluginMeta } from "~plugins";
|
|
|
|
|
|
2025-07-15 15:57:24 +02:00
|
|
|
import { OptionComponentMap } from "./components";
|
2023-09-08 03:42:20 +02:00
|
|
|
import { openContributorModal } from "./ContributorModal";
|
2024-06-20 04:40:07 +02:00
|
|
|
import { GithubButton, WebsiteButton } from "./LinkIconButton";
|
|
|
|
|
|
|
|
|
|
const cl = classNameFactory("vc-plugin-modal-");
|
2022-10-17 20:18:25 +01:00
|
|
|
|
2022-11-28 13:37:55 +01:00
|
|
|
const AvatarStyles = findByPropsLazy("moreUsers", "emptyUser", "avatarContainer", "clickableAvatar");
|
2022-10-17 20:18:25 +01:00
|
|
|
const UserRecord: Constructor<Partial<User>> = proxyLazy(() => UserStore.getCurrentUser().constructor) as any;
|
|
|
|
|
|
|
|
|
|
interface PluginModalProps extends ModalProps {
|
|
|
|
|
plugin: Plugin;
|
2025-07-15 15:57:24 +02:00
|
|
|
onRestartNeeded(key: string): void;
|
2022-10-17 20:18:25 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 01:55:39 +02:00
|
|
|
function makeDummyUser(user: { username: string; id?: string; avatar?: string; }) {
|
2022-10-17 20:18:25 +01:00
|
|
|
const newUser = new UserRecord({
|
2023-05-23 01:55:39 +02:00
|
|
|
username: user.username,
|
|
|
|
|
id: user.id ?? generateId(),
|
|
|
|
|
avatar: user.avatar,
|
|
|
|
|
/** To stop discord making unwanted requests... */
|
2022-10-17 20:18:25 +01:00
|
|
|
bot: true,
|
|
|
|
|
});
|
2025-07-15 15:57:24 +02:00
|
|
|
|
2022-10-17 20:18:25 +01:00
|
|
|
FluxDispatcher.dispatch({
|
|
|
|
|
type: "USER_UPDATE",
|
|
|
|
|
user: newUser,
|
|
|
|
|
});
|
2025-07-15 15:57:24 +02:00
|
|
|
|
2022-10-17 20:18:25 +01:00
|
|
|
return newUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function PluginModal({ plugin, onRestartNeeded, onClose, transitionState }: PluginModalProps) {
|
|
|
|
|
const pluginSettings = useSettings().plugins[plugin.name];
|
2023-09-05 20:10:42 +02:00
|
|
|
const hasSettings = Boolean(pluginSettings && plugin.options && !isObjectEmpty(plugin.options));
|
2022-12-13 20:44:57 -03:00
|
|
|
|
2025-07-15 15:57:24 +02:00
|
|
|
const [authors, setAuthors] = useState<Partial<User>[]>([]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-10-17 20:18:25 +01:00
|
|
|
(async () => {
|
|
|
|
|
for (const user of plugin.authors.slice(0, 6)) {
|
2022-11-01 01:49:41 +01:00
|
|
|
const author = user.id
|
2023-10-25 21:57:50 +07:00
|
|
|
? await UserUtils.getUser(`${user.id}`)
|
2023-05-23 01:55:39 +02:00
|
|
|
.catch(() => makeDummyUser({ username: user.name }))
|
|
|
|
|
: makeDummyUser({ username: user.name });
|
|
|
|
|
|
2022-11-01 01:49:41 +01:00
|
|
|
setAuthors(a => [...a, author]);
|
2022-10-17 20:18:25 +01:00
|
|
|
}
|
|
|
|
|
})();
|
2025-01-04 07:24:50 +01:00
|
|
|
}, [plugin.authors]);
|
2022-10-17 20:18:25 +01:00
|
|
|
|
|
|
|
|
function renderSettings() {
|
2025-07-15 15:57:24 +02:00
|
|
|
if (!hasSettings || !plugin.options)
|
2022-10-19 20:57:27 +01:00
|
|
|
return <Forms.FormText>There are no settings for this plugin.</Forms.FormText>;
|
2023-05-10 23:14:04 +02:00
|
|
|
|
2025-07-15 15:57:24 +02:00
|
|
|
const options = Object.entries(plugin.options).map(([key, setting]) => {
|
|
|
|
|
if (setting.type === OptionType.CUSTOM || setting.hidden) return null;
|
|
|
|
|
|
|
|
|
|
function onChange(newValue: any) {
|
|
|
|
|
const option = plugin.options?.[key];
|
|
|
|
|
if (!option || option.type === OptionType.CUSTOM) return;
|
2022-12-13 20:44:57 -03:00
|
|
|
|
2025-07-15 15:57:24 +02:00
|
|
|
pluginSettings[key] = newValue;
|
2022-12-13 20:44:57 -03:00
|
|
|
|
2025-07-15 15:57:24 +02:00
|
|
|
if (option.restartNeeded) onRestartNeeded(key);
|
|
|
|
|
}
|
2022-12-13 20:44:57 -03:00
|
|
|
|
2025-07-15 15:57:24 +02:00
|
|
|
const Component = OptionComponentMap[setting.type];
|
|
|
|
|
return (
|
|
|
|
|
<Component
|
|
|
|
|
id={key}
|
|
|
|
|
key={key}
|
|
|
|
|
option={setting}
|
|
|
|
|
onChange={debounce(onChange)}
|
|
|
|
|
pluginSettings={pluginSettings}
|
|
|
|
|
definedSettings={plugin.settings}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return <Flex flexDirection="column" style={{ gap: 12, marginBottom: 16 }}>{options}</Flex>;
|
2022-10-17 20:18:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderMoreUsers(_label: string, count: number) {
|
|
|
|
|
const sliceCount = plugin.authors.length - count;
|
|
|
|
|
const sliceStart = plugin.authors.length - sliceCount;
|
|
|
|
|
const sliceEnd = sliceStart + plugin.authors.length - count;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Tooltip text={plugin.authors.slice(sliceStart, sliceEnd).map(u => u.name).join(", ")}>
|
|
|
|
|
{({ onMouseEnter, onMouseLeave }) => (
|
|
|
|
|
<div
|
|
|
|
|
className={AvatarStyles.moreUsers}
|
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
|
>
|
|
|
|
|
+{sliceCount}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</Tooltip>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 04:40:07 +02:00
|
|
|
const pluginMeta = PluginMeta[plugin.name];
|
|
|
|
|
|
2022-10-17 20:18:25 +01:00
|
|
|
return (
|
2025-07-09 18:42:38 -04:00
|
|
|
<ModalRoot transitionState={transitionState} size={ModalSize.MEDIUM}>
|
2022-12-13 20:44:57 -03:00
|
|
|
<ModalHeader separator={false}>
|
|
|
|
|
<Text variant="heading-lg/semibold" style={{ flexGrow: 1 }}>{plugin.name}</Text>
|
|
|
|
|
<ModalCloseButton onClick={onClose} />
|
2022-10-17 20:18:25 +01:00
|
|
|
</ModalHeader>
|
2023-04-30 00:52:20 +02:00
|
|
|
<ModalContent>
|
2022-10-19 20:57:27 +01:00
|
|
|
<Forms.FormSection>
|
2024-06-20 04:40:07 +02:00
|
|
|
<Flex className={cl("info")}>
|
|
|
|
|
<Forms.FormText className={cl("description")}>{plugin.description}</Forms.FormText>
|
|
|
|
|
{!pluginMeta.userPlugin && (
|
|
|
|
|
<div className="vc-settings-modal-links">
|
|
|
|
|
<WebsiteButton
|
|
|
|
|
text="View more info"
|
|
|
|
|
href={`https://vencord.dev/plugins/${plugin.name}`}
|
|
|
|
|
/>
|
|
|
|
|
<GithubButton
|
|
|
|
|
text="View source code"
|
|
|
|
|
href={`https://github.com/${gitRemote}/tree/main/src/plugins/${pluginMeta.folderName}`}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</Flex>
|
2022-12-13 20:44:57 -03:00
|
|
|
<Forms.FormTitle tag="h3" style={{ marginTop: 8, marginBottom: 0 }}>Authors</Forms.FormTitle>
|
|
|
|
|
<div style={{ width: "fit-content", marginBottom: 8 }}>
|
2022-10-17 20:18:25 +01:00
|
|
|
<UserSummaryItem
|
|
|
|
|
users={authors}
|
|
|
|
|
guildId={undefined}
|
|
|
|
|
renderIcon={false}
|
|
|
|
|
max={6}
|
|
|
|
|
showDefaultAvatarsForNullUsers
|
|
|
|
|
renderMoreUsers={renderMoreUsers}
|
2023-09-08 03:42:20 +02:00
|
|
|
renderUser={(user: User) => (
|
|
|
|
|
<Clickable
|
|
|
|
|
className={AvatarStyles.clickableAvatar}
|
|
|
|
|
onClick={() => openContributorModal(user)}
|
|
|
|
|
>
|
|
|
|
|
<img
|
|
|
|
|
className={AvatarStyles.avatar}
|
|
|
|
|
src={user.getAvatarURL(void 0, 80, true)}
|
|
|
|
|
alt={user.username}
|
|
|
|
|
title={user.username}
|
|
|
|
|
/>
|
|
|
|
|
</Clickable>
|
|
|
|
|
)}
|
2022-10-17 20:18:25 +01:00
|
|
|
/>
|
|
|
|
|
</div>
|
2022-10-19 20:57:27 +01:00
|
|
|
</Forms.FormSection>
|
2025-07-15 15:57:24 +02:00
|
|
|
|
2022-10-17 20:18:25 +01:00
|
|
|
{!!plugin.settingsAboutComponent && (
|
2025-07-09 18:42:38 -04:00
|
|
|
<div className={Margins.bottom8}>
|
2022-10-19 20:57:27 +01:00
|
|
|
<Forms.FormSection>
|
2025-06-06 18:48:56 +02:00
|
|
|
<ErrorBoundary message="An error occurred while rendering this plugin's custom Info Component">
|
2025-07-15 15:57:24 +02:00
|
|
|
<plugin.settingsAboutComponent />
|
2022-10-17 20:18:25 +01:00
|
|
|
</ErrorBoundary>
|
2022-10-19 20:57:27 +01:00
|
|
|
</Forms.FormSection>
|
2022-10-17 20:18:25 +01:00
|
|
|
</div>
|
|
|
|
|
)}
|
2025-07-15 15:57:24 +02:00
|
|
|
|
2023-08-04 17:56:40 +00:00
|
|
|
<Forms.FormSection className={Margins.bottom16}>
|
2022-10-19 20:57:27 +01:00
|
|
|
<Forms.FormTitle tag="h3">Settings</Forms.FormTitle>
|
2022-10-17 20:18:25 +01:00
|
|
|
{renderSettings()}
|
2022-10-19 20:57:27 +01:00
|
|
|
</Forms.FormSection>
|
2022-10-17 20:18:25 +01:00
|
|
|
</ModalContent>
|
|
|
|
|
</ModalRoot>
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-06-29 20:18:29 +02:00
|
|
|
|
2025-07-15 15:57:24 +02:00
|
|
|
export function openPluginModal(plugin: Plugin, onRestartNeeded?: (pluginName: string, key: string) => void) {
|
2024-06-29 20:18:29 +02:00
|
|
|
openModal(modalProps => (
|
|
|
|
|
<PluginModal
|
|
|
|
|
{...modalProps}
|
|
|
|
|
plugin={plugin}
|
2025-07-15 15:57:24 +02:00
|
|
|
onRestartNeeded={(key: string) => onRestartNeeded?.(plugin.name, key)}
|
2024-06-29 20:18:29 +02:00
|
|
|
/>
|
|
|
|
|
));
|
|
|
|
|
}
|