Equicord/src/plugins/translate/TranslateModal.tsx

102 lines
3.5 KiB
TypeScript
Raw Normal View History

/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2023 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/>.
*/
import { Divider } from "@components/Divider";
2025-10-03 00:18:39 +02:00
import { FormSwitch } from "@components/FormSwitch";
2025-10-10 21:11:31 -04:00
import { HeadingPrimary, HeadingSecondary } from "@components/Heading";
import { Margins } from "@utils/margins";
import { ModalCloseButton, ModalContent, ModalHeader, ModalProps, ModalRoot } from "@utils/modal";
2025-10-10 21:11:31 -04:00
import { SearchableSelect, useMemo } from "@webpack/common";
import { settings } from "./settings";
import { cl, getLanguages } from "./utils";
const LanguageSettingKeys = ["receivedInput", "receivedOutput", "sentInput", "sentOutput"] as const;
function LanguageSelect({ settingsKey, includeAuto }: { settingsKey: typeof LanguageSettingKeys[number]; includeAuto: boolean; }) {
const currentValue = settings.use([settingsKey])[settingsKey];
const options = useMemo(
() => {
const options = Object.entries(getLanguages()).map(([value, label]) => ({ value, label }));
if (!includeAuto)
options.shift();
return options;
}, []
);
return (
<section className={Margins.bottom16}>
2025-10-10 21:11:31 -04:00
<HeadingSecondary>
{settings.def[settingsKey].description}
2025-10-10 21:11:31 -04:00
</HeadingSecondary>
<SearchableSelect
options={options}
2026-02-06 03:34:29 +01:00
value={options.find(o => o.value === currentValue)?.value}
placeholder="Select a language"
maxVisibleItems={5}
closeOnSelect={true}
onChange={v => settings.store[settingsKey] = v}
/>
</section>
);
}
function AutoTranslateToggle() {
const value = settings.use(["autoTranslate"]).autoTranslate;
return (
2025-10-03 00:18:39 +02:00
<FormSwitch
title="Auto Translate"
description={settings.def.autoTranslate.description}
value={value}
onChange={v => settings.store.autoTranslate = v}
hideBorder
2025-10-03 00:18:39 +02:00
/>
);
}
export function TranslateModal({ rootProps }: { rootProps: ModalProps; }) {
return (
<ModalRoot {...rootProps}>
<ModalHeader className={cl("modal-header")}>
2025-10-10 21:11:31 -04:00
<HeadingPrimary className={cl("modal-title")}>
Translate
2025-10-10 21:11:31 -04:00
</HeadingPrimary>
<ModalCloseButton onClick={rootProps.onClose} />
</ModalHeader>
<ModalContent className={cl("modal-content")}>
{LanguageSettingKeys.map(s => (
<LanguageSelect
key={s}
settingsKey={s}
includeAuto={s.endsWith("Input")}
/>
))}
<Divider className={Margins.bottom16} />
<AutoTranslateToggle />
</ModalContent>
</ModalRoot>
);
}