'use client' import type { Locale } from '@/i18n-config' import { Select, SelectContent, SelectItem, SelectItemIndicator, SelectItemText, SelectTrigger, } from '@langgenius/dify-ui/select' import { toast } from '@langgenius/dify-ui/toast' import { useAtomValue, useSetAtom } from 'jotai' import { useTheme } from 'next-themes' import { useState } from 'react' import { useTranslation } from 'react-i18next' import { refreshUserProfileAtom, userProfileAtom } from '@/context/account-state' import { useLocale } from '@/context/i18n' import { setLocaleOnClient } from '@/i18n-config' import { languages } from '@/i18n-config/language' import { useRouter } from '@/next/navigation' import { updateUserProfile } from '@/service/common' import { timezones } from '@/utils/timezone' type SelectOption = { value: string name: string } type TimezoneOption = { value: string name: string } const titleClassName = ` mb-1 system-sm-medium text-text-secondary ` const themes = ['system', 'light', 'dark'] as const type ThemeOption = (typeof themes)[number] const isThemeOption = (value: string): value is ThemeOption => { return (themes as readonly string[]).includes(value) } export default function PreferencePage() { const locale = useLocale() const userProfile = useAtomValue(userProfileAtom) const refreshUserProfile = useSetAtom(refreshUserProfileAtom) const [editing, setEditing] = useState(false) const { t } = useTranslation() const router = useRouter() const { theme, setTheme } = useTheme() const languageOptions: SelectOption[] = languages.filter((item) => item.supported) const themeOptions: SelectOption[] = [ { value: 'system', name: t(($) => $['account.appearanceFollowSystem'], { ns: 'common' }) }, { value: 'light', name: t(($) => $['account.appearanceLight'], { ns: 'common' }) }, { value: 'dark', name: t(($) => $['account.appearanceDark'], { ns: 'common' }) }, ] const selectedLanguage = languageOptions.find( (item) => item.value === (locale || userProfile.interface_language), ) const selectedTheme = themeOptions.find((item) => item.value === (theme || 'system')) const selectedTimezone = timezones.find((item) => item.value === userProfile.timezone) const handleSelectTheme = (item: SelectOption) => { if (isThemeOption(item.value)) setTheme(item.value) } const handleSelectLanguage = async (item: SelectOption) => { const url = '/account/interface-language' const bodyKey = 'interface_language' setEditing(true) try { await updateUserProfile({ url, body: { [bodyKey]: item.value } }) toast.success(t(($) => $['actionMsg.modifiedSuccessfully'], { ns: 'common' })) setLocaleOnClient(item.value.toString() as Locale, false) router.refresh() } catch (e) { toast.error((e as Error).message) } finally { setEditing(false) } } const handleSelectTimezone = async (item: TimezoneOption) => { const url = '/account/timezone' const bodyKey = 'timezone' setEditing(true) try { await updateUserProfile({ url, body: { [bodyKey]: item.value } }) toast.success(t(($) => $['actionMsg.modifiedSuccessfully'], { ns: 'common' })) refreshUserProfile() } catch (e) { toast.error((e as Error).message) } finally { setEditing(false) } } return ( <>
{t(($) => $['account.appearanceLabel'], { ns: 'common' })}
{t(($) => $['language.displayLanguage'], { ns: 'common' })}
{t(($) => $['language.timezone'], { ns: 'common' })}
) }