import Box from "@mui/material/Box"; import Stack from "@mui/material/Stack"; import InputLabel from "@mui/material/InputLabel"; import TextField from "@mui/material/TextField"; import MenuItem from "@mui/material/MenuItem"; import FormControl from "@mui/material/FormControl"; import Select from "@mui/material/Select"; import Link from "@mui/material/Link"; import FormHelperText from "@mui/material/FormHelperText"; import { useSetting } from "../../hooks/Setting"; import { limitNumber } from "../../libs/utils"; import { useI18n } from "../../hooks/I18n"; import { apiTranslate } from "../../apis"; import { useAlert } from "../../hooks/Alert"; import { UI_LANGS, URL_KISS_PROXY, TRANS_NEWLINE_LENGTH, CACHE_NAME, OPT_TRANS_GOOGLE, OPT_TRANS_DEEPL, OPT_TRANS_OPENAI, } from "../../config"; export default function Settings() { const i18n = useI18n(); const { setting, updateSetting } = useSetting(); const alert = useAlert(); const handleChange = (e) => { e.preventDefault(); let { name, value } = e.target; switch (name) { case "fetchLimit": value = limitNumber(value, 1, 100); break; case "fetchInterval": value = limitNumber(value, 0, 5000); break; case "minLength": value = limitNumber(value, 1, 100); break; case "maxLength": value = limitNumber(value, 100, 10000); break; case "newlineLength": value = limitNumber(value, 1, 1000); break; default: } updateSetting({ [name]: value, }); }; const handleClearCache = () => { try { caches.delete(CACHE_NAME); alert.success(i18n("clear_success")); } catch (err) { console.log("[clear cache]", err); } }; const handleApiTest = async (translator) => { try { const [text] = await apiTranslate({ translator, q: "hello world", fromLang: "en", toLang: "zh-CN", setting, }); if (!text) { throw new Error("empty reault"); } alert.success(i18n("test_success")); } catch (err) { alert.error(`${i18n("test_failed")}: ${err.message}`); } }; const { uiLang, googleUrl, fetchLimit, fetchInterval, minLength, maxLength, openaiUrl, deeplUrl = "", deeplKey = "", openaiKey, openaiModel, openaiPrompt, clearCache, newlineLength = TRANS_NEWLINE_LENGTH, } = setting; return ( {i18n("ui_lang")} {i18n("if_clear_cache")} {i18n("clear_all_cache_now")} {i18n("google_api")} {googleUrl && ( { handleApiTest(OPT_TRANS_GOOGLE); }} > {i18n("click_test")} )} } name="googleUrl" value={googleUrl} onChange={handleChange} helperText={ {i18n("about_api_proxy")} } /> {i18n("deepl_api")} {deeplUrl && ( { handleApiTest(OPT_TRANS_DEEPL); }} > {i18n("click_test")} )} } name="deeplUrl" value={deeplUrl} onChange={handleChange} /> {i18n("openai_api")} {openaiUrl && openaiPrompt && ( { handleApiTest(OPT_TRANS_OPENAI); }} > {i18n("click_test")} )} } name="openaiUrl" value={openaiUrl} onChange={handleChange} helperText={ {i18n("about_api_proxy")} } /> ); }