2023-07-20 13:45:41 +08:00
|
|
|
import Box from "@mui/material/Box";
|
|
|
|
|
import Stack from "@mui/material/Stack";
|
|
|
|
|
import InputLabel from "@mui/material/InputLabel";
|
|
|
|
|
import TextField from "@mui/material/TextField";
|
|
|
|
|
import MenuItem from "@mui/material/MenuItem";
|
|
|
|
|
import FormControl from "@mui/material/FormControl";
|
|
|
|
|
import Select from "@mui/material/Select";
|
|
|
|
|
import { useSetting, useSettingUpdate } from "../../hooks/Setting";
|
2023-08-17 13:27:22 +08:00
|
|
|
import { limitNumber, debounce } from "../../libs/utils";
|
2023-07-20 13:45:41 +08:00
|
|
|
import { useI18n } from "../../hooks/I18n";
|
|
|
|
|
import { UI_LANGS } from "../../config";
|
2023-08-17 13:27:22 +08:00
|
|
|
import { useMemo } from "react";
|
2023-07-20 13:45:41 +08:00
|
|
|
|
|
|
|
|
export default function Settings() {
|
|
|
|
|
const i18n = useI18n();
|
|
|
|
|
const setting = useSetting();
|
|
|
|
|
const updateSetting = useSettingUpdate();
|
|
|
|
|
|
2023-08-17 13:27:22 +08:00
|
|
|
const handleChange = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
debounce((e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
let { name, value } = e.target;
|
|
|
|
|
switch (name) {
|
|
|
|
|
case "fetchLimit":
|
|
|
|
|
value = limitNumber(value, 1, 100);
|
|
|
|
|
break;
|
|
|
|
|
case "fetchInterval":
|
|
|
|
|
value = limitNumber(value, 0, 5000);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
updateSetting({
|
|
|
|
|
[name]: value,
|
|
|
|
|
});
|
|
|
|
|
}, 500),
|
|
|
|
|
[updateSetting]
|
|
|
|
|
);
|
|
|
|
|
|
2023-07-20 13:45:41 +08:00
|
|
|
if (!setting) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
uiLang,
|
|
|
|
|
googleUrl,
|
|
|
|
|
fetchLimit,
|
2023-08-04 16:05:14 +08:00
|
|
|
fetchInterval,
|
2023-07-20 13:45:41 +08:00
|
|
|
openaiUrl,
|
|
|
|
|
openaiKey,
|
|
|
|
|
openaiModel,
|
|
|
|
|
openaiPrompt,
|
|
|
|
|
clearCache,
|
|
|
|
|
} = setting;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Box>
|
|
|
|
|
<Stack spacing={3}>
|
|
|
|
|
<FormControl size="small">
|
|
|
|
|
<InputLabel>{i18n("ui_lang")}</InputLabel>
|
|
|
|
|
<Select
|
2023-08-17 13:27:22 +08:00
|
|
|
name="uiLang"
|
2023-07-20 13:45:41 +08:00
|
|
|
value={uiLang}
|
|
|
|
|
label={i18n("ui_lang")}
|
2023-08-17 13:27:22 +08:00
|
|
|
onChange={handleChange}
|
2023-07-20 13:45:41 +08:00
|
|
|
>
|
|
|
|
|
{UI_LANGS.map(([lang, name]) => (
|
2023-08-04 10:08:54 +08:00
|
|
|
<MenuItem key={lang} value={lang}>
|
|
|
|
|
{name}
|
|
|
|
|
</MenuItem>
|
2023-07-20 13:45:41 +08:00
|
|
|
))}
|
|
|
|
|
</Select>
|
|
|
|
|
</FormControl>
|
|
|
|
|
|
|
|
|
|
<TextField
|
|
|
|
|
size="small"
|
|
|
|
|
label={i18n("fetch_limit")}
|
|
|
|
|
type="number"
|
2023-08-17 13:27:22 +08:00
|
|
|
name="fetchLimit"
|
2023-07-20 13:45:41 +08:00
|
|
|
defaultValue={fetchLimit}
|
2023-08-17 13:27:22 +08:00
|
|
|
onChange={handleChange}
|
2023-08-04 16:05:14 +08:00
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<TextField
|
|
|
|
|
size="small"
|
|
|
|
|
label={i18n("fetch_interval")}
|
|
|
|
|
type="number"
|
2023-08-17 13:27:22 +08:00
|
|
|
name="fetchInterval"
|
2023-08-04 16:05:14 +08:00
|
|
|
defaultValue={fetchInterval}
|
2023-08-17 13:27:22 +08:00
|
|
|
onChange={handleChange}
|
2023-07-20 13:45:41 +08:00
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<FormControl size="small">
|
|
|
|
|
<InputLabel>{i18n("clear_cache")}</InputLabel>
|
|
|
|
|
<Select
|
2023-08-17 13:27:22 +08:00
|
|
|
name="clearCache"
|
2023-07-20 13:45:41 +08:00
|
|
|
value={clearCache}
|
|
|
|
|
label={i18n("clear_cache")}
|
2023-08-17 13:27:22 +08:00
|
|
|
onChange={handleChange}
|
2023-07-20 13:45:41 +08:00
|
|
|
>
|
|
|
|
|
<MenuItem value={false}>{i18n("clear_cache_never")}</MenuItem>
|
|
|
|
|
<MenuItem value={true}>{i18n("clear_cache_restart")}</MenuItem>
|
|
|
|
|
</Select>
|
|
|
|
|
</FormControl>
|
|
|
|
|
|
|
|
|
|
<TextField
|
|
|
|
|
size="small"
|
|
|
|
|
label={i18n("google_api")}
|
2023-08-17 13:27:22 +08:00
|
|
|
name="googleUrl"
|
2023-07-20 13:45:41 +08:00
|
|
|
defaultValue={googleUrl}
|
2023-08-17 13:27:22 +08:00
|
|
|
onChange={handleChange}
|
2023-07-20 13:45:41 +08:00
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<TextField
|
|
|
|
|
size="small"
|
|
|
|
|
label={i18n("openai_api")}
|
2023-08-17 13:27:22 +08:00
|
|
|
name="openaiUrl"
|
2023-07-20 13:45:41 +08:00
|
|
|
defaultValue={openaiUrl}
|
2023-08-17 13:27:22 +08:00
|
|
|
onChange={handleChange}
|
2023-07-20 13:45:41 +08:00
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<TextField
|
|
|
|
|
size="small"
|
2023-07-31 03:10:09 +08:00
|
|
|
type="password"
|
2023-07-20 13:45:41 +08:00
|
|
|
label={i18n("openai_key")}
|
2023-08-17 13:27:22 +08:00
|
|
|
name="openaiKey"
|
2023-07-20 13:45:41 +08:00
|
|
|
defaultValue={openaiKey}
|
2023-08-17 13:27:22 +08:00
|
|
|
onChange={handleChange}
|
2023-07-20 13:45:41 +08:00
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<TextField
|
|
|
|
|
size="small"
|
|
|
|
|
label={i18n("openai_model")}
|
2023-08-17 13:27:22 +08:00
|
|
|
name="openaiModel"
|
2023-07-20 13:45:41 +08:00
|
|
|
defaultValue={openaiModel}
|
2023-08-17 13:27:22 +08:00
|
|
|
onChange={handleChange}
|
2023-07-20 13:45:41 +08:00
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<TextField
|
|
|
|
|
size="small"
|
|
|
|
|
label={i18n("openai_prompt")}
|
2023-08-17 13:27:22 +08:00
|
|
|
name="openaiPrompt"
|
2023-07-20 13:45:41 +08:00
|
|
|
defaultValue={openaiPrompt}
|
2023-08-17 13:27:22 +08:00
|
|
|
onChange={handleChange}
|
2023-07-20 13:45:41 +08:00
|
|
|
multiline
|
|
|
|
|
/>
|
|
|
|
|
</Stack>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|