Skip to content
Snippets Groups Projects
setting-verge.tsx 5.33 KiB
Newer Older
import { useRef } from "react";
GyDi's avatar
GyDi committed
import { useTranslation } from "react-i18next";
import {
  IconButton,
  MenuItem,
  Select,
  Switch,
  Typography,
} from "@mui/material";
GyDi's avatar
GyDi committed
import { openAppDir, openCoreDir, openLogsDir } from "@/services/cmds";
import { ArrowForward } from "@mui/icons-material";
GyDi's avatar
GyDi committed
import { useVerge } from "@/hooks/use-verge";
GyDi's avatar
GyDi committed
import { version } from "@root/package.json";
import { DialogRef } from "@/components/base";
import { SettingList, SettingItem } from "./mods/setting-comp";
import { ThemeModeSwitch } from "./mods/theme-mode-switch";
import { ConfigViewer } from "./mods/config-viewer";
import { HotkeyViewer } from "./mods/hotkey-viewer";
import { MiscViewer } from "./mods/misc-viewer";
import { ThemeViewer } from "./mods/theme-viewer";
import { GuardState } from "./mods/guard-state";
GyDi's avatar
GyDi committed

interface Props {
  onError?: (err: Error) => void;
}

const SettingVerge = ({ onError }: Props) => {
GyDi's avatar
GyDi committed
  const { t } = useTranslation();
GyDi's avatar
GyDi committed

GyDi's avatar
GyDi committed
  const { verge, patchVerge, mutateVerge } = useVerge();

  const { theme_mode, theme_blur, traffic_graph, language } = verge ?? {};
GyDi's avatar
GyDi committed

  const configRef = useRef<DialogRef>(null);
  const hotkeyRef = useRef<DialogRef>(null);
  const miscRef = useRef<DialogRef>(null);
  const themeRef = useRef<DialogRef>(null);
GyDi's avatar
GyDi committed

GyDi's avatar
GyDi committed
  const onSwitchFormat = (_e: any, value: boolean) => value;
GyDi's avatar
GyDi committed
  const onChangeData = (patch: Partial<IVergeConfig>) => {
GyDi's avatar
GyDi committed
    mutateVerge({ ...verge, ...patch }, false);
GyDi's avatar
GyDi committed
  };

  return (
GyDi's avatar
GyDi committed
    <SettingList title={t("Verge Setting")}>
      <ThemeViewer ref={themeRef} />
      <ConfigViewer ref={configRef} />
      <HotkeyViewer ref={hotkeyRef} />
      <MiscViewer ref={miscRef} />
GyDi's avatar
GyDi committed

GyDi's avatar
GyDi committed
      <SettingItem label={t("Language")}>
        <GuardState
          value={language ?? "en"}
          onCatch={onError}
          onFormat={(e: any) => e.target.value}
          onChange={(e) => onChangeData({ language: e })}
GyDi's avatar
GyDi committed
          onGuard={(e) => patchVerge({ language: e })}
GyDi's avatar
GyDi committed
          <Select size="small" sx={{ width: 100, "> div": { py: "7.5px" } }}>
            <MenuItem value="zh">中文</MenuItem>
            <MenuItem value="en">English</MenuItem>
            <MenuItem value="ru">Русский</MenuItem>
          </Select>
        </GuardState>
      </SettingItem>

GyDi's avatar
GyDi committed
      <SettingItem label={t("Theme Mode")}>
GyDi's avatar
GyDi committed
        <GuardState
          value={theme_mode}
GyDi's avatar
GyDi committed
          onCatch={onError}
          onChange={(e) => onChangeData({ theme_mode: e })}
GyDi's avatar
GyDi committed
          onGuard={(e) => patchVerge({ theme_mode: e })}
GyDi's avatar
GyDi committed
        >
          <ThemeModeSwitch />
GyDi's avatar
GyDi committed
        </GuardState>
      </SettingItem>

GyDi's avatar
GyDi committed
      <SettingItem label={t("Theme Blur")}>
GyDi's avatar
GyDi committed
        <GuardState
GyDi's avatar
GyDi committed
          value={theme_blur ?? false}
GyDi's avatar
GyDi committed
          valueProps="checked"
          onCatch={onError}
          onFormat={onSwitchFormat}
          onChange={(e) => onChangeData({ theme_blur: e })}
GyDi's avatar
GyDi committed
          onGuard={(e) => patchVerge({ theme_blur: e })}
GyDi's avatar
GyDi committed
        >
          <Switch edge="end" />
        </GuardState>
      </SettingItem>

GyDi's avatar
GyDi committed
      <SettingItem label={t("Traffic Graph")}>
        <GuardState
          value={traffic_graph ?? true}
          valueProps="checked"
          onCatch={onError}
          onFormat={onSwitchFormat}
          onChange={(e) => onChangeData({ traffic_graph: e })}
GyDi's avatar
GyDi committed
          onGuard={(e) => patchVerge({ traffic_graph: e })}
GyDi's avatar
GyDi committed
        >
          <Switch edge="end" />
        </GuardState>
      </SettingItem>

      <SettingItem label={t("Miscellaneous")}>
        <IconButton
          color="inherit"
          size="small"
          sx={{ my: "2px" }}
          onClick={() => miscRef.current?.open()}
        >
          <ArrowForward />
        </IconButton>
      </SettingItem>

GyDi's avatar
GyDi committed
      <SettingItem label={t("Theme Setting")}>
GyDi's avatar
GyDi committed
        <IconButton
          color="inherit"
          size="small"
GyDi's avatar
GyDi committed
          sx={{ my: "2px" }}
          onClick={() => themeRef.current?.open()}
GyDi's avatar
GyDi committed
        >
          <ArrowForward />
        </IconButton>
      </SettingItem>

GyDi's avatar
GyDi committed
      <SettingItem label={t("Hotkey Setting")}>
        <IconButton
          color="inherit"
          size="small"
          sx={{ my: "2px" }}
          onClick={() => hotkeyRef.current?.open()}
GyDi's avatar
GyDi committed
        >
          <ArrowForward />
        </IconButton>
      </SettingItem>

GyDi's avatar
GyDi committed
      <SettingItem label={t("Runtime Config")}>
        <IconButton
          color="inherit"
          size="small"
          sx={{ my: "2px" }}
          onClick={() => configRef.current?.open()}
GyDi's avatar
GyDi committed
        >
          <ArrowForward />
        </IconButton>
      </SettingItem>

GyDi's avatar
GyDi committed
      <SettingItem label={t("Open App Dir")}>
        <IconButton
          color="inherit"
          size="small"
          sx={{ my: "2px" }}
          onClick={openAppDir}
        >
GyDi's avatar
GyDi committed
          <ArrowForward />
        </IconButton>
      </SettingItem>

GyDi's avatar
GyDi committed
      <SettingItem label={t("Open Core Dir")}>
        <IconButton
          color="inherit"
          size="small"
          sx={{ my: "2px" }}
          onClick={openCoreDir}
        >
          <ArrowForward />
        </IconButton>
      </SettingItem>

GyDi's avatar
GyDi committed
      <SettingItem label={t("Open Logs Dir")}>
        <IconButton
          color="inherit"
          size="small"
          sx={{ my: "2px" }}
          onClick={openLogsDir}
        >
GyDi's avatar
GyDi committed
          <ArrowForward />
        </IconButton>
      </SettingItem>

GyDi's avatar
GyDi committed
      <SettingItem label={t("Verge Version")}>
GyDi's avatar
GyDi committed
        <Typography sx={{ py: "7px", pr: 1 }}>v{version}</Typography>
GyDi's avatar
GyDi committed
      </SettingItem>
GyDi's avatar
GyDi committed
    </SettingList>
GyDi's avatar
GyDi committed
  );
};

export default SettingVerge;