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/>.
|
|
|
|
*/
|
|
|
|
|
2022-11-28 12:37:55 +00:00
|
|
|
import { generateId } from "@api/Commands";
|
2023-05-05 23:36:00 +00:00
|
|
|
import { useSettings } from "@api/Settings";
|
2022-11-28 12:58:14 +00:00
|
|
|
import ErrorBoundary from "@components/ErrorBoundary";
|
|
|
|
import { Flex } from "@components/Flex";
|
2023-05-05 23:36:00 +00:00
|
|
|
import { proxyLazy } from "@utils/lazy";
|
2023-04-08 21:28:12 +00:00
|
|
|
import { Margins } from "@utils/margins";
|
2023-05-05 23:36:00 +00:00
|
|
|
import { classes } from "@utils/misc";
|
2022-12-13 23:44:57 +00:00
|
|
|
import { ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalProps, ModalRoot, ModalSize } from "@utils/modal";
|
2023-05-05 23:36:00 +00:00
|
|
|
import { LazyComponent } from "@utils/react";
|
2022-11-28 12:37:55 +00:00
|
|
|
import { OptionType, Plugin } from "@utils/types";
|
|
|
|
import { findByCode, findByPropsLazy } from "@webpack";
|
|
|
|
import { Button, FluxDispatcher, Forms, React, Text, Tooltip, UserStore, UserUtils } from "@webpack/common";
|
2022-10-17 19:18:25 +00:00
|
|
|
import { User } from "discord-types/general";
|
|
|
|
import { Constructor } from "type-fest";
|
|
|
|
|
|
|
|
import {
|
2022-11-01 00:49:41 +00:00
|
|
|
ISettingElementProps,
|
2022-10-17 19:18:25 +00:00
|
|
|
SettingBooleanComponent,
|
2022-10-26 21:42:26 +00:00
|
|
|
SettingCustomComponent,
|
2022-10-17 19:18:25 +00:00
|
|
|
SettingNumericComponent,
|
|
|
|
SettingSelectComponent,
|
2022-11-01 00:49:41 +00:00
|
|
|
SettingSliderComponent,
|
|
|
|
SettingTextComponent
|
2022-10-17 19:18:25 +00:00
|
|
|
} from "./components";
|
|
|
|
|
2022-11-06 17:37:01 +00:00
|
|
|
const UserSummaryItem = LazyComponent(() => findByCode("defaultRenderUser", "showDefaultAvatarsForNullUsers"));
|
2022-11-28 12:37:55 +00:00
|
|
|
const AvatarStyles = findByPropsLazy("moreUsers", "emptyUser", "avatarContainer", "clickableAvatar");
|
2022-10-17 19:18:25 +00:00
|
|
|
const UserRecord: Constructor<Partial<User>> = proxyLazy(() => UserStore.getCurrentUser().constructor) as any;
|
|
|
|
|
|
|
|
interface PluginModalProps extends ModalProps {
|
|
|
|
plugin: Plugin;
|
|
|
|
onRestartNeeded(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** To stop discord making unwanted requests... */
|
|
|
|
function makeDummyUser(user: { name: string, id: BigInt; }) {
|
|
|
|
const newUser = new UserRecord({
|
|
|
|
username: user.name,
|
|
|
|
id: generateId(),
|
|
|
|
bot: true,
|
|
|
|
});
|
|
|
|
FluxDispatcher.dispatch({
|
|
|
|
type: "USER_UPDATE",
|
|
|
|
user: newUser,
|
|
|
|
});
|
|
|
|
return newUser;
|
|
|
|
}
|
|
|
|
|
2022-11-01 00:49:41 +00:00
|
|
|
const Components: Record<OptionType, React.ComponentType<ISettingElementProps<any>>> = {
|
|
|
|
[OptionType.STRING]: SettingTextComponent,
|
|
|
|
[OptionType.NUMBER]: SettingNumericComponent,
|
|
|
|
[OptionType.BIGINT]: SettingNumericComponent,
|
|
|
|
[OptionType.BOOLEAN]: SettingBooleanComponent,
|
|
|
|
[OptionType.SELECT]: SettingSelectComponent,
|
|
|
|
[OptionType.SLIDER]: SettingSliderComponent,
|
|
|
|
[OptionType.COMPONENT]: SettingCustomComponent
|
|
|
|
};
|
|
|
|
|
2022-10-17 19:18:25 +00:00
|
|
|
export default function PluginModal({ plugin, onRestartNeeded, onClose, transitionState }: PluginModalProps) {
|
|
|
|
const [authors, setAuthors] = React.useState<Partial<User>[]>([]);
|
|
|
|
|
|
|
|
const pluginSettings = useSettings().plugins[plugin.name];
|
|
|
|
|
|
|
|
const [tempSettings, setTempSettings] = React.useState<Record<string, any>>({});
|
|
|
|
|
|
|
|
const [errors, setErrors] = React.useState<Record<string, boolean>>({});
|
2022-10-25 17:49:50 +00:00
|
|
|
const [saveError, setSaveError] = React.useState<string | null>(null);
|
2022-10-17 19:18:25 +00:00
|
|
|
|
|
|
|
const canSubmit = () => Object.values(errors).every(e => !e);
|
|
|
|
|
2022-12-13 23:44:57 +00:00
|
|
|
const hasSettings = Boolean(pluginSettings && plugin.options);
|
|
|
|
|
2022-10-17 19:18:25 +00:00
|
|
|
React.useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
for (const user of plugin.authors.slice(0, 6)) {
|
2022-11-01 00:49:41 +00:00
|
|
|
const author = user.id
|
|
|
|
? await UserUtils.fetchUser(`${user.id}`).catch(() => makeDummyUser(user))
|
|
|
|
: makeDummyUser(user);
|
|
|
|
setAuthors(a => [...a, author]);
|
2022-10-17 19:18:25 +00:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
}, []);
|
|
|
|
|
2022-10-25 17:49:50 +00:00
|
|
|
async function saveAndClose() {
|
2022-10-17 19:18:25 +00:00
|
|
|
if (!plugin.options) {
|
|
|
|
onClose();
|
|
|
|
return;
|
|
|
|
}
|
2022-10-25 17:49:50 +00:00
|
|
|
|
|
|
|
if (plugin.beforeSave) {
|
|
|
|
const result = await Promise.resolve(plugin.beforeSave(tempSettings));
|
|
|
|
if (result !== true) {
|
|
|
|
setSaveError(result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 19:18:25 +00:00
|
|
|
let restartNeeded = false;
|
|
|
|
for (const [key, value] of Object.entries(tempSettings)) {
|
|
|
|
const option = plugin.options[key];
|
|
|
|
pluginSettings[key] = value;
|
|
|
|
option?.onChange?.(value);
|
|
|
|
if (option?.restartNeeded) restartNeeded = true;
|
|
|
|
}
|
|
|
|
if (restartNeeded) onRestartNeeded();
|
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderSettings() {
|
2022-12-13 23:44:57 +00:00
|
|
|
if (!hasSettings || !plugin.options) {
|
2022-10-19 19:57:27 +00:00
|
|
|
return <Forms.FormText>There are no settings for this plugin.</Forms.FormText>;
|
2022-12-13 23:44:57 +00:00
|
|
|
} else {
|
|
|
|
const options = Object.entries(plugin.options).map(([key, setting]) => {
|
2023-05-10 21:14:04 +00:00
|
|
|
if (setting.hidden) return null;
|
|
|
|
|
2022-12-13 23:44:57 +00:00
|
|
|
function onChange(newValue: any) {
|
|
|
|
setTempSettings(s => ({ ...s, [key]: newValue }));
|
|
|
|
}
|
|
|
|
|
|
|
|
function onError(hasError: boolean) {
|
|
|
|
setErrors(e => ({ ...e, [key]: hasError }));
|
|
|
|
}
|
|
|
|
|
|
|
|
const Component = Components[setting.type];
|
|
|
|
return (
|
|
|
|
<Component
|
|
|
|
id={key}
|
|
|
|
key={key}
|
|
|
|
option={setting}
|
|
|
|
onChange={onChange}
|
|
|
|
onError={onError}
|
|
|
|
pluginSettings={pluginSettings}
|
2023-01-13 22:15:45 +00:00
|
|
|
definedSettings={plugin.settings}
|
2022-12-13 23:44:57 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-04-29 22:52:20 +00:00
|
|
|
return <Flex flexDirection="column" style={{ gap: 12, marginBottom: 16 }}>{options}</Flex>;
|
2022-10-17 19:18:25 +00: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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-04-08 21:28:12 +00:00
|
|
|
<ModalRoot transitionState={transitionState} size={ModalSize.MEDIUM} className="vc-text-selectable">
|
2022-12-13 23:44:57 +00:00
|
|
|
<ModalHeader separator={false}>
|
|
|
|
<Text variant="heading-lg/semibold" style={{ flexGrow: 1 }}>{plugin.name}</Text>
|
|
|
|
<ModalCloseButton onClick={onClose} />
|
2022-10-17 19:18:25 +00:00
|
|
|
</ModalHeader>
|
2023-04-29 22:52:20 +00:00
|
|
|
<ModalContent>
|
2022-10-19 19:57:27 +00:00
|
|
|
<Forms.FormSection>
|
|
|
|
<Forms.FormTitle tag="h3">About {plugin.name}</Forms.FormTitle>
|
|
|
|
<Forms.FormText>{plugin.description}</Forms.FormText>
|
2022-12-13 23:44:57 +00:00
|
|
|
<Forms.FormTitle tag="h3" style={{ marginTop: 8, marginBottom: 0 }}>Authors</Forms.FormTitle>
|
|
|
|
<div style={{ width: "fit-content", marginBottom: 8 }}>
|
2022-10-17 19:18:25 +00:00
|
|
|
<UserSummaryItem
|
|
|
|
users={authors}
|
|
|
|
count={plugin.authors.length}
|
|
|
|
guildId={undefined}
|
|
|
|
renderIcon={false}
|
|
|
|
max={6}
|
|
|
|
showDefaultAvatarsForNullUsers
|
|
|
|
showUserPopout
|
|
|
|
renderMoreUsers={renderMoreUsers}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-10-19 19:57:27 +00:00
|
|
|
</Forms.FormSection>
|
2022-10-17 19:18:25 +00:00
|
|
|
{!!plugin.settingsAboutComponent && (
|
2023-04-08 21:28:12 +00:00
|
|
|
<div className={classes(Margins.bottom8, "vc-text-selectable")}>
|
2022-10-19 19:57:27 +00:00
|
|
|
<Forms.FormSection>
|
2022-10-17 19:18:25 +00:00
|
|
|
<ErrorBoundary message="An error occurred while rendering this plugin's custom InfoComponent">
|
2022-12-07 14:33:40 +00:00
|
|
|
<plugin.settingsAboutComponent tempSettings={tempSettings} />
|
2022-10-17 19:18:25 +00:00
|
|
|
</ErrorBoundary>
|
2022-10-19 19:57:27 +00:00
|
|
|
</Forms.FormSection>
|
2022-10-17 19:18:25 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2022-10-19 19:57:27 +00:00
|
|
|
<Forms.FormSection>
|
|
|
|
<Forms.FormTitle tag="h3">Settings</Forms.FormTitle>
|
2022-10-17 19:18:25 +00:00
|
|
|
{renderSettings()}
|
2022-10-19 19:57:27 +00:00
|
|
|
</Forms.FormSection>
|
2022-10-17 19:18:25 +00:00
|
|
|
</ModalContent>
|
2022-12-13 23:44:57 +00:00
|
|
|
{hasSettings && <ModalFooter>
|
2022-10-25 17:49:50 +00:00
|
|
|
<Flex flexDirection="column" style={{ width: "100%" }}>
|
|
|
|
<Flex style={{ marginLeft: "auto" }}>
|
|
|
|
<Button
|
|
|
|
onClick={onClose}
|
|
|
|
size={Button.Sizes.SMALL}
|
2022-12-13 23:44:57 +00:00
|
|
|
color={Button.Colors.WHITE}
|
|
|
|
look={Button.Looks.LINK}
|
2022-10-25 17:49:50 +00:00
|
|
|
>
|
2022-11-22 22:05:46 +00:00
|
|
|
Cancel
|
2022-10-25 17:49:50 +00:00
|
|
|
</Button>
|
|
|
|
<Tooltip text="You must fix all errors before saving" shouldShow={!canSubmit()}>
|
|
|
|
{({ onMouseEnter, onMouseLeave }) => (
|
|
|
|
<Button
|
|
|
|
size={Button.Sizes.SMALL}
|
|
|
|
color={Button.Colors.BRAND}
|
|
|
|
onClick={saveAndClose}
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
disabled={!canSubmit()}
|
|
|
|
>
|
2022-11-22 22:05:46 +00:00
|
|
|
Save & Close
|
2022-10-25 17:49:50 +00:00
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</Tooltip>
|
|
|
|
</Flex>
|
|
|
|
{saveError && <Text variant="text-md/semibold" style={{ color: "var(--text-danger)" }}>Error while saving: {saveError}</Text>}
|
2022-10-17 19:18:25 +00:00
|
|
|
</Flex>
|
2022-12-13 23:44:57 +00:00
|
|
|
</ModalFooter>}
|
2022-10-17 19:18:25 +00:00
|
|
|
</ModalRoot>
|
|
|
|
);
|
|
|
|
}
|