clash-verge/src/components/setting/setting-theme.tsx

157 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-03-31 23:38:00 +08:00
import useSWR from "swr";
import { useEffect, useState } from "react";
import { useLockFn } from "ahooks";
import { useTranslation } from "react-i18next";
import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
List,
ListItem,
ListItemText,
styled,
TextField,
2022-05-06 14:04:39 +08:00
useTheme,
2022-03-31 23:38:00 +08:00
} from "@mui/material";
2022-08-06 02:35:11 +08:00
import { getVergeConfig, patchVergeConfig } from "@/services/cmds";
import { defaultTheme, defaultDarkTheme } from "@/pages/_theme";
2022-03-31 23:38:00 +08:00
interface Props {
open: boolean;
onClose: () => void;
onError?: (err: Error) => void;
}
const Item = styled(ListItem)(() => ({
padding: "5px 2px",
}));
const Round = styled("div")(() => ({
width: "24px",
height: "24px",
borderRadius: "18px",
display: "inline-block",
marginRight: "8px",
}));
const SettingTheme = (props: Props) => {
const { open, onClose, onError } = props;
const { t } = useTranslation();
const { data: vergeConfig, mutate } = useSWR(
"getVergeConfig",
getVergeConfig
);
const { theme_setting } = vergeConfig ?? {};
const [theme, setTheme] = useState(theme_setting || {});
useEffect(() => {
setTheme({ ...theme_setting } || {});
}, [theme_setting]);
const textProps = {
size: "small",
autoComplete: "off",
sx: { width: 135 },
} as const;
const handleChange = (field: keyof typeof theme) => (e: any) => {
setTheme((t) => ({ ...t, [field]: e.target.value }));
};
const onSave = useLockFn(async () => {
try {
await patchVergeConfig({ theme_setting: theme });
mutate();
2022-04-01 02:08:42 +08:00
onClose();
2022-03-31 23:38:00 +08:00
} catch (err: any) {
onError?.(err);
}
});
2022-05-06 14:04:39 +08:00
// default theme
const { palette } = useTheme();
const dt = palette.mode === "light" ? defaultTheme : defaultDarkTheme;
type ThemeKey = keyof typeof theme & keyof typeof defaultTheme;
const renderItem = (label: string, key: ThemeKey) => {
2022-04-02 17:18:38 +08:00
return (
<Item>
<ListItemText primary={label} />
2022-05-06 14:04:39 +08:00
<Round sx={{ background: theme[key] || dt[key] }} />
2022-04-02 17:18:38 +08:00
<TextField
{...textProps}
value={theme[key] ?? ""}
2022-05-06 14:04:39 +08:00
placeholder={dt[key]}
2022-04-02 17:18:38 +08:00
onChange={handleChange(key)}
onKeyDown={(e) => e.key === "Enter" && onSave()}
/>
</Item>
);
};
2022-03-31 23:38:00 +08:00
return (
<Dialog open={open} onClose={onClose}>
<DialogTitle>{t("Theme Setting")}</DialogTitle>
<DialogContent
sx={{ width: 400, maxHeight: 300, overflow: "auto", pb: 0 }}
>
<List sx={{ pt: 0 }}>
2022-04-02 17:18:38 +08:00
{renderItem("Primary Color", "primary_color")}
2022-03-31 23:38:00 +08:00
2022-04-02 17:18:38 +08:00
{renderItem("Secondary Color", "secondary_color")}
2022-03-31 23:38:00 +08:00
2022-04-02 17:18:38 +08:00
{renderItem("Primary Text", "primary_text")}
2022-03-31 23:38:00 +08:00
2022-04-02 17:18:38 +08:00
{renderItem("Secondary Text", "secondary_text")}
2022-03-31 23:38:00 +08:00
2022-04-02 17:18:38 +08:00
{renderItem("Info Color", "info_color")}
2022-03-31 23:38:00 +08:00
2022-04-02 17:18:38 +08:00
{renderItem("Error Color", "error_color")}
2022-03-31 23:38:00 +08:00
2022-04-02 17:18:38 +08:00
{renderItem("Warning Color", "warning_color")}
2022-03-31 23:38:00 +08:00
2022-04-02 17:18:38 +08:00
{renderItem("Success Color", "success_color")}
2022-03-31 23:38:00 +08:00
<Item>
<ListItemText primary="Font Family" />
<TextField
{...textProps}
2022-04-01 01:16:23 +08:00
value={theme.font_family ?? ""}
2022-03-31 23:38:00 +08:00
onChange={handleChange("font_family")}
2022-04-02 17:18:38 +08:00
onKeyDown={(e) => e.key === "Enter" && onSave()}
2022-03-31 23:38:00 +08:00
/>
</Item>
<Item>
2022-04-01 02:08:42 +08:00
<ListItemText primary="CSS Injection" />
2022-03-31 23:38:00 +08:00
<TextField
{...textProps}
2022-04-01 02:08:42 +08:00
value={theme.css_injection ?? ""}
onChange={handleChange("css_injection")}
2022-04-02 17:18:38 +08:00
onKeyDown={(e) => e.key === "Enter" && onSave()}
2022-03-31 23:38:00 +08:00
/>
</Item>
</List>
</DialogContent>
<DialogActions>
<Button variant="outlined" onClick={onClose}>
{t("Cancel")}
</Button>
2022-03-31 23:38:00 +08:00
<Button onClick={onSave} variant="contained">
{t("Save")}
</Button>
</DialogActions>
</Dialog>
);
};
export default SettingTheme;