import { localeType } from '@/provider/i18n' import { invoke } from '@/utils/invoke' import { Button, Dialog, DialogBody, DialogFooter, DialogHeader, Input, Option, Radio, Select, Typography } from '@material-tailwind/react' import { ChangeEvent, useEffect, useState } from 'react' import { FormattedMessage, useIntl } from 'react-intl' type Props = { opened: boolean close: () => void reloadSettings: () => void } const languages = [ { label: 'English', value: 'en' }, { label: '日本語', value: 'ja' }, { label: 'Português', value: 'pt-PT' }, { label: '繁体字', value: 'zh-TW' }, { label: 'español', value: 'es-ES' } ] const themes = [ { label: 'Blue', value: 'theme-blue' }, { label: 'Orange', value: 'theme-orange' }, { label: 'Purple', value: 'theme-purple' }, { label: 'Green', value: 'theme-green' }, { label: 'Brown', value: 'theme-brown' }, { label: 'Gray', value: 'theme-gray' } ] type ProxyValue = 'no' | 'os' | 'manual' export default function Settings(props: Props) { const [language, setLanguage] = useState('en') const [fontSize, setFontSize] = useState(16) const [theme, setTheme] = useState('theme-blue') const [isDark, setIsDark] = useState(false) const [proxy, setProxy] = useState('no') const [proxyProtocol, setProxyProtocol] = useState(null) const [proxyHost, setProxyHost] = useState(null) const [proxyPort, setProxyPort] = useState(null) const [fontFamilies, setFontFamilies] = useState>([]) const [fontFamily, setFontFamily] = useState(null) const { formatMessage } = useIntl() useEffect(() => { const f = async () => { const lists = await invoke('list-fonts', null) if (lists) { setFontFamilies(lists) } } f() if (typeof localStorage !== 'undefined') { const lang = localStorage.getItem('language') if (lang) { setLanguage(lang as localeType) } else { setLanguage('en') } const fontSize = localStorage.getItem('fontSize') if (fontSize) { setFontSize(parseInt(fontSize)) } const fontFamily = localStorage.getItem('fontFamily') if (fontFamily) { setFontFamily(fontFamily) } const dark = localStorage.getItem('color-mode') if (dark === 'dark') { setIsDark(true) } else { setIsDark(false) } const proxyMode = localStorage.getItem('proxyMode') if (proxyMode) { setProxy(proxyMode as ProxyValue) } const proxyProtocol = localStorage.getItem('proxyProtocol') if (proxyProtocol) { setProxyProtocol(proxyProtocol) } const proxyHost = localStorage.getItem('proxyHost') if (proxyHost) { setProxyHost(proxyHost) } const proxyPort = localStorage.getItem('proxyPort') if (proxyPort) { setProxyPort(proxyPort) } } }, []) const languageChanged = (e: string) => { setLanguage(e as localeType) if (typeof localStorage !== 'undefined') { localStorage.setItem('language', e) } props.reloadSettings() } const fontSizeChanged = (e: ChangeEvent) => { setFontSize(parseInt(e.target.value)) if (typeof localStorage !== 'undefined') { localStorage.setItem('fontSize', e.target.value) } props.reloadSettings() } const fontFamilyChanged = (e: string) => { setFontFamily(e) if (typeof localStorage !== 'undefined') { localStorage.setItem('fontFamily', e) } props.reloadSettings() } const themeChanged = (e: string) => { setTheme(e) if (typeof localStorage !== 'undefined') { localStorage.setItem('theme', e) } props.reloadSettings() } const modeChanged = (isDark: boolean) => { setIsDark(isDark) if (typeof localStorage !== 'undefined') { if (isDark) { localStorage.setItem('color-mode', 'dark') } else { localStorage.setItem('color-mode', 'light') } } props.reloadSettings() } const save = () => { if (typeof localStorage !== 'undefined') { localStorage.setItem('proxyMode', proxy) localStorage.setItem('proxyProtocol', proxyProtocol) localStorage.setItem('proxyHost', proxyHost) localStorage.setItem('proxyPort', proxyPort) } props.reloadSettings() props.close() } return (
} onClick={() => modeChanged(true)} defaultChecked={isDark} /> } onClick={() => modeChanged(false)} defaultChecked={!isDark} />
} defaultChecked={proxy === 'no'} onClick={() => setProxy('no')} /> } defaultChecked={proxy === 'os'} onClick={() => setProxy('os')} />
setProxyHost(e.target.value)} />
setProxyPort(e.target.value)} />
} defaultChecked={proxy === 'manual'} onClick={() => setProxy('manual')} />
) }