import { useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import { useTranslation } from "react-i18next"; import { Button, Divider, Input, Switch, Textarea, Typography } from "@mui/joy"; import { formatBytes } from "@/helpers/utils"; import { useGlobalStore } from "@/store/module"; import * as api from "@/helpers/api"; import Icon from "../Icon"; import showUpdateCustomizedProfileDialog from "../UpdateCustomizedProfileDialog"; import "@/less/settings/system-section.less"; interface State { dbSize: number; allowSignUp: boolean; ignoreUpgrade: boolean; disablePublicMemos: boolean; additionalStyle: string; additionalScript: string; openAIConfig: OpenAIConfig; } const SystemSection = () => { const { t } = useTranslation(); const globalStore = useGlobalStore(); const systemStatus = globalStore.state.systemStatus; const [state, setState] = useState({ dbSize: systemStatus.dbSize, allowSignUp: systemStatus.allowSignUp, ignoreUpgrade: systemStatus.ignoreUpgrade, additionalStyle: systemStatus.additionalStyle, additionalScript: systemStatus.additionalScript, disablePublicMemos: systemStatus.disablePublicMemos, openAIConfig: systemStatus.openAIConfig, }); useEffect(() => { globalStore.fetchSystemStatus(); }, []); useEffect(() => { setState({ ...state, dbSize: systemStatus.dbSize, allowSignUp: systemStatus.allowSignUp, additionalStyle: systemStatus.additionalStyle, additionalScript: systemStatus.additionalScript, disablePublicMemos: systemStatus.disablePublicMemos, openAIConfig: systemStatus.openAIConfig, }); }, [systemStatus]); const handleAllowSignUpChanged = async (value: boolean) => { setState({ ...state, allowSignUp: value, }); await api.upsertSystemSetting({ name: "allow-signup", value: JSON.stringify(value), }); }; const handleIgnoreUpgradeChanged = async (value: boolean) => { setState({ ...state, ignoreUpgrade: value, }); await api.upsertSystemSetting({ name: "ignore-upgrade", value: JSON.stringify(value), }); }; const handleUpdateCustomizedProfileButtonClick = () => { showUpdateCustomizedProfileDialog(); }; const handleVacuumBtnClick = async () => { try { await api.vacuumDatabase(); await globalStore.fetchSystemStatus(); } catch (error) { console.error(error); return; } toast.success(t("message.succeed-vacuum-database")); }; const handleOpenAIConfigKeyChanged = (value: string) => { setState({ ...state, openAIConfig: { ...state.openAIConfig, key: value, }, }); }; const handleSaveOpenAIConfig = async () => { try { await api.upsertSystemSetting({ name: "openai-config", value: JSON.stringify(state.openAIConfig), }); globalStore.setSystemStatus({ openAIConfig: state.openAIConfig }); } catch (error) { console.error(error); return; } toast.success("OpenAI Config updated"); }; const handleOpenAIConfigHostChanged = (value: string) => { setState({ ...state, openAIConfig: { ...state.openAIConfig, host: value, }, }); }; const handleAdditionalStyleChanged = (value: string) => { setState({ ...state, additionalStyle: value, }); }; const handleSaveAdditionalStyle = async () => { try { await api.upsertSystemSetting({ name: "additional-style", value: JSON.stringify(state.additionalStyle), }); } catch (error) { console.error(error); return; } toast.success(t("message.succeed-update-additional-style")); }; const handleAdditionalScriptChanged = (value: string) => { setState({ ...state, additionalScript: value, }); }; const handleSaveAdditionalScript = async () => { try { await api.upsertSystemSetting({ name: "additional-script", value: JSON.stringify(state.additionalScript), }); } catch (error) { console.error(error); return; } toast.success(t("message.succeed-update-additional-script")); }; const handleDisablePublicMemosChanged = async (value: boolean) => { setState({ ...state, disablePublicMemos: value, }); globalStore.setSystemStatus({ disablePublicMemos: value }); await api.upsertSystemSetting({ name: "disable-public-memos", value: JSON.stringify(value), }); }; return (

{t("common.basic")}

{t("setting.system-section.server-name")}: {systemStatus.customizedProfile.name}
{t("setting.system-section.database-file-size")}: {formatBytes(state.dbSize)}

{t("common.settings")}

{t("setting.system-section.allow-user-signup")} handleAllowSignUpChanged(event.target.checked)} />
{t("setting.system-section.ignore-version-upgrade")} handleIgnoreUpgradeChanged(event.target.checked)} />
{t("setting.system-section.disable-public-memos")} handleDisablePublicMemosChanged(event.target.checked)} />
{t("setting.system-section.openai-api-key")} {t("setting.system-section.openai-api-key-description")}
handleOpenAIConfigKeyChanged(event.target.value)} />
{t("setting.system-section.openai-api-host")}
handleOpenAIConfigHostChanged(event.target.value)} />
{t("setting.system-section.additional-style")}