Whalebird-desktop-client-ma.../renderer/components/Settings.tsx

194 lines
4.8 KiB
TypeScript
Raw Normal View History

import { localeType } from '@/provider/i18n'
2024-03-12 15:02:18 +01:00
import { Dialog, DialogBody, DialogHeader, Input, Option, Radio, Select, Typography } from '@material-tailwind/react'
2023-12-04 17:17:52 +01:00
import { ChangeEvent, useEffect, useState } from 'react'
2023-12-04 17:08:12 +01:00
import { FormattedMessage } from 'react-intl'
type Props = {
opened: boolean
close: () => void
2023-12-04 17:17:52 +01:00
reloadSettings: () => void
2023-12-04 17:08:12 +01:00
}
const languages = [
{
label: 'English',
value: 'en'
},
{
label: '日本語',
value: 'ja'
2024-02-05 13:52:13 +01:00
},
{
label: 'Português',
value: 'pt-PT'
},
{
label: '繁体字',
value: 'zh-TW'
2023-12-04 17:08:12 +01:00
}
]
2024-03-09 11:09:36 +01:00
const themes = [
{
label: 'Blue',
value: 'theme-blue'
},
{
label: 'Orange',
value: 'theme-orange'
2024-03-09 14:13:44 +01:00
},
{
label: 'Purple',
value: 'theme-purple'
},
{
label: 'Green',
value: 'theme-green'
},
{
label: 'Brown',
value: 'theme-brown'
},
{
label: 'Gray',
value: 'theme-gray'
2024-03-09 11:09:36 +01:00
}
]
2023-12-04 17:08:12 +01:00
export default function Settings(props: Props) {
const [language, setLanguage] = useState<localeType>('en')
2023-12-06 16:10:16 +01:00
const [fontSize, setFontSize] = useState<number>(16)
2024-03-09 11:09:36 +01:00
const [theme, setTheme] = useState<string>('theme-blue')
2024-03-12 15:02:18 +01:00
const [isDark, setIsDark] = useState(false)
2023-12-04 17:08:12 +01:00
useEffect(() => {
if (typeof localStorage !== 'undefined') {
const lang = localStorage.getItem('language')
2024-02-05 13:52:13 +01:00
if (lang) {
setLanguage(lang as localeType)
} else {
setLanguage('en')
}
2024-03-12 15:02:18 +01:00
const dark = localStorage.getItem('color-mode')
if (dark === 'dark') {
setIsDark(true)
} else {
setIsDark(false)
}
2023-12-04 17:08:12 +01:00
}
}, [])
const languageChanged = (e: string) => {
2024-02-05 13:52:13 +01:00
setLanguage(e as localeType)
2023-12-04 17:08:12 +01:00
if (typeof localStorage !== 'undefined') {
localStorage.setItem('language', e)
2023-12-04 17:08:12 +01:00
}
2023-12-04 17:17:52 +01:00
props.reloadSettings()
2023-12-04 17:08:12 +01:00
}
2023-12-06 16:10:16 +01:00
const fontSizeChanged = (e: ChangeEvent<HTMLInputElement>) => {
setFontSize(parseInt(e.target.value))
if (typeof localStorage !== 'undefined') {
localStorage.setItem('fontSize', e.target.value)
}
props.reloadSettings()
}
2024-03-09 11:09:36 +01:00
const themeChanged = (e: string) => {
setTheme(e)
if (typeof localStorage !== 'undefined') {
localStorage.setItem('theme', e)
}
props.reloadSettings()
}
2024-03-12 15:02:18 +01:00
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()
}
2023-12-04 17:08:12 +01:00
return (
<Dialog open={props.opened} handler={props.close} size="sm">
<DialogHeader>
2023-12-04 17:08:12 +01:00
<FormattedMessage id="settings.title" />
</DialogHeader>
<DialogBody>
2023-12-06 16:10:16 +01:00
<div className="flex flex-col gap-4">
<div>
<div className="mb-2">
<Typography>
2023-12-06 16:10:16 +01:00
<FormattedMessage id="settings.font_size" />
</Typography>
2023-12-06 16:10:16 +01:00
</div>
<div>
2024-03-14 17:12:19 +01:00
<Input type="number" color="blue-gray" value={fontSize} onChange={fontSizeChanged} />
2023-12-06 16:10:16 +01:00
</div>
</div>
<div>
<div className="mb-2">
<Typography>
2023-12-06 16:10:16 +01:00
<FormattedMessage id="settings.language" />
</Typography>
2023-12-06 16:10:16 +01:00
</div>
<div>
2024-03-14 17:12:19 +01:00
<Select id="language" color="blue-gray" onChange={languageChanged} value={language}>
2023-12-06 16:10:16 +01:00
{languages.map(lang => (
<Option key={lang.value} value={lang.value}>
2023-12-06 16:10:16 +01:00
{lang.label}
</Option>
2023-12-06 16:10:16 +01:00
))}
</Select>
</div>
</div>
2024-03-12 15:02:18 +01:00
<div>
<div className="mb-2">
<Typography>
<FormattedMessage id="settings.mode" />
</Typography>
</div>
<div>
<Radio
name="mode"
2024-03-14 17:12:19 +01:00
color="blue"
2024-03-12 15:02:18 +01:00
label={<FormattedMessage id="settings.dark_mode" />}
onClick={() => modeChanged(true)}
defaultChecked={isDark}
/>
<Radio
name="mode"
2024-03-14 17:12:19 +01:00
color="blue"
2024-03-12 15:02:18 +01:00
label={<FormattedMessage id="settings.light_mode" />}
onClick={() => modeChanged(false)}
defaultChecked={!isDark}
/>
</div>
</div>
2024-03-09 11:09:36 +01:00
<div>
<div className="mb-2">
<Typography>
<FormattedMessage id="settings.theme" />
</Typography>
</div>
<div>
2024-03-14 17:12:19 +01:00
<Select id="theme" color="blue-gray" onChange={themeChanged} value={theme}>
2024-03-09 11:09:36 +01:00
{themes.map(t => (
<Option key={t.value} value={t.value}>
{t.label}
</Option>
))}
</Select>
</div>
</div>
2023-12-04 17:08:12 +01:00
</div>
</DialogBody>
</Dialog>
2023-12-04 17:08:12 +01:00
)
}