import { useState, useEffect, useMemo } from "react"; import { JsonRpcResponse, useJsonRpc } from "@hooks/useJsonRpc"; import { useDeviceUiNavigation } from "@hooks/useAppNavigation"; import { useDeviceStore } from "@hooks/stores"; import { Button } from "@components/Button"; import Checkbox from "@components/Checkbox"; import { SelectMenuBasic } from "@components/SelectMenuBasic"; import { SettingsItem } from "@components/SettingsItem"; import { SettingsPageHeader } from "@components/SettingsPageheader"; import notifications from "@/notifications"; import { getLocale, setLocale, locales, baseLocale } from '@localizations/runtime.js'; import { m } from "@localizations/messages.js"; import { deleteCookie, map_locale_code_to_name } from "@/utils"; export default function SettingsGeneralRoute() { const { send } = useJsonRpc(); const { navigateTo } = useDeviceUiNavigation(); const [autoUpdate, setAutoUpdate] = useState(true); const currentVersions = useDeviceStore(state => { const { appVersion, systemVersion } = state; if (!appVersion || !systemVersion) return null; return { appVersion, systemVersion }; }); useEffect(() => { send("getAutoUpdateState", {}, (resp: JsonRpcResponse) => { if ("error" in resp) return; setAutoUpdate(resp.result as boolean); }); }, [send]); const handleAutoUpdateChange = (enabled: boolean) => { send("setAutoUpdateState", { enabled }, (resp: JsonRpcResponse) => { if ("error" in resp) { notifications.error( m.general_auto_update_error({ error: resp.error.data || m.unknown_error() }), ); return; } setAutoUpdate(enabled); }); }; const [currentLocale, setCurrentLocale] = useState(getLocale()); const localeOptions = useMemo(() => { return ["", ...locales] .map((code) => { const [localizedName, nativeName] = map_locale_code_to_name(currentLocale, code); // don't repeat the name if it's the same in both locales (or blank) const label = nativeName && nativeName !== localizedName ? `${localizedName} - ${nativeName}` : localizedName; return { value: code, label: label } }); }, [currentLocale]); const handleLocaleChange = (newLocale: string) => { if (newLocale === currentLocale) return; let validLocale = newLocale as typeof locales[number]; if (newLocale !== "") { if (!locales.includes(validLocale)) { validLocale = baseLocale; } setLocale(validLocale); // tell the i18n system to change locale } else { deleteCookie("JETKVM_LOCALE", "", "/"); // delete the cookie that the i18n system uses to store the locale } setCurrentLocale(validLocale); notifications.success(m.locale_change_success({ locale: validLocale || m.locale_auto() })); }; return (