clash-verge/src/pages/_layout.tsx

156 lines
4.6 KiB
TypeScript
Raw Normal View History

2022-03-18 14:45:24 +08:00
import dayjs from "dayjs";
2022-03-12 23:07:45 +08:00
import i18next from "i18next";
2022-03-18 14:45:24 +08:00
import relativeTime from "dayjs/plugin/relativeTime";
2022-11-20 20:12:58 +08:00
import { SWRConfig, mutate } from "swr";
2022-03-30 12:36:39 +08:00
import { useEffect } from "react";
2022-06-04 18:55:39 +08:00
import { useSetRecoilState } from "recoil";
2022-03-12 23:07:45 +08:00
import { useTranslation } from "react-i18next";
2021-12-08 23:36:34 +08:00
import { Route, Routes } from "react-router-dom";
2022-03-30 12:36:39 +08:00
import { alpha, List, Paper, ThemeProvider } from "@mui/material";
import { listen } from "@tauri-apps/api/event";
2022-01-16 03:11:07 +08:00
import { appWindow } from "@tauri-apps/api/window";
import { routers } from "./_routers";
2022-08-06 02:35:11 +08:00
import { getAxios } from "@/services/api";
import { atomCurrentProfile } from "@/services/states";
2022-11-20 20:12:58 +08:00
import { getProfiles } from "@/services/cmds";
import { useVerge } from "@/hooks/use-verge";
2022-08-06 02:35:11 +08:00
import { ReactComponent as LogoSvg } from "@/assets/image/logo.svg";
import { BaseErrorBoundary, Notice } from "@/components/base";
2022-08-06 02:35:11 +08:00
import LayoutItem from "@/components/layout/layout-item";
import LayoutControl from "@/components/layout/layout-control";
import LayoutTraffic from "@/components/layout/layout-traffic";
import UpdateButton from "@/components/layout/update-button";
import useCustomTheme from "@/components/layout/use-custom-theme";
import getSystem from "@/utils/get-system";
2022-03-18 14:45:24 +08:00
import "dayjs/locale/zh-cn";
2021-12-20 00:01:32 +08:00
declare global {
const WIN_PORTABLE: boolean;
}
2022-03-18 14:45:24 +08:00
dayjs.extend(relativeTime);
2022-03-19 15:35:59 +08:00
2022-03-22 01:36:06 +08:00
const OS = getSystem();
2022-02-20 23:46:13 +08:00
2021-12-08 23:36:34 +08:00
const Layout = () => {
2022-03-12 23:07:45 +08:00
const { t } = useTranslation();
2022-03-30 12:36:39 +08:00
const { theme } = useCustomTheme();
2022-11-20 20:12:58 +08:00
const { verge } = useVerge();
const { theme_blur, language } = verge || {};
2021-12-09 23:28:57 +08:00
2022-06-04 18:55:39 +08:00
const setCurrentProfile = useSetRecoilState(atomCurrentProfile);
2022-01-13 02:11:50 +08:00
useEffect(() => {
window.addEventListener("keydown", (e) => {
2022-09-09 16:42:35 +08:00
if (e.key === "Escape") {
if (OS === "macos") appWindow.hide();
else appWindow.close();
}
2022-01-13 02:11:50 +08:00
});
listen("verge://refresh-clash-config", async () => {
// the clash info may be updated
await getAxios(true);
mutate("getProxies");
2022-11-11 22:45:32 +08:00
mutate("getVersion");
mutate("getClashConfig");
});
// update the verge config
listen("verge://refresh-verge-config", () => mutate("getVergeConfig"));
2022-06-04 18:55:39 +08:00
2022-09-26 20:46:29 +08:00
// 设置提示监听
listen("verge://notice-message", ({ payload }) => {
const [status, msg] = payload as [string, string];
switch (status) {
case "set_config::ok":
Notice.success("Refresh clash config");
break;
case "set_config::error":
Notice.error(msg);
break;
default:
break;
}
});
2022-06-04 18:55:39 +08:00
// set current profile uid
getProfiles().then((data) => setCurrentProfile(data.current ?? ""));
2022-01-13 02:11:50 +08:00
}, []);
2022-03-12 23:07:45 +08:00
useEffect(() => {
2022-03-30 12:36:39 +08:00
if (language) {
dayjs.locale(language === "zh" ? "zh-cn" : language);
i18next.changeLanguage(language);
2022-01-16 03:11:07 +08:00
}
2022-03-30 12:36:39 +08:00
}, [language]);
2022-01-16 03:11:07 +08:00
2021-12-08 23:36:34 +08:00
return (
<SWRConfig value={{}}>
<ThemeProvider theme={theme}>
2022-01-12 02:27:29 +08:00
<Paper
square
elevation={0}
2022-03-22 01:36:06 +08:00
className={`${OS} layout`}
2022-03-30 12:36:39 +08:00
onPointerDown={(e: any) => {
if (e.target?.dataset?.windrag) appWindow.startDragging();
}}
onContextMenu={(e) => {
// only prevent it on Windows
2022-03-22 01:36:06 +08:00
if (OS === "windows") e.preventDefault();
}}
2022-01-12 02:27:29 +08:00
sx={[
2022-03-30 12:36:39 +08:00
({ palette }) => ({
2022-05-06 14:04:39 +08:00
bgcolor: alpha(palette.background.paper, theme_blur ? 0.8 : 1),
2022-01-12 02:27:29 +08:00
}),
]}
>
2022-01-16 03:11:07 +08:00
<div className="layout__left" data-windrag>
<div className="the-logo" data-windrag>
2022-03-30 12:36:39 +08:00
<LogoSvg />
2022-01-10 02:05:35 +08:00
{!(OS === "windows" && WIN_PORTABLE) && (
<UpdateButton className="the-newbtn" />
)}
</div>
2021-12-08 23:36:34 +08:00
2022-05-06 14:04:39 +08:00
<List className="the-menu">
{routers.map((router) => (
2021-12-25 22:33:29 +08:00
<LayoutItem key={router.label} to={router.link}>
2022-03-12 23:07:45 +08:00
{t(router.label)}
2021-12-25 22:33:29 +08:00
</LayoutItem>
))}
</List>
2021-12-08 23:36:34 +08:00
2022-01-16 03:11:07 +08:00
<div className="the-traffic" data-windrag>
2022-02-13 19:27:06 +08:00
<LayoutTraffic />
</div>
2021-12-09 23:28:57 +08:00
</div>
2021-12-08 23:36:34 +08:00
2022-01-16 03:11:07 +08:00
<div className="layout__right" data-windrag>
2022-03-23 21:43:13 +08:00
{OS !== "macos" && (
2022-02-20 23:46:13 +08:00
<div className="the-bar">
<LayoutControl />
</div>
)}
2022-01-08 22:23:48 +08:00
2022-01-25 01:48:26 +08:00
<div className="the-content">
2022-11-19 01:22:19 +08:00
<BaseErrorBoundary>
<Routes>
{routers.map(({ label, link, ele: Ele }) => (
<Route key={label} path={link} element={<Ele />} />
))}
</Routes>
</BaseErrorBoundary>
2022-01-08 22:23:48 +08:00
</div>
</div>
</Paper>
</ThemeProvider>
</SWRConfig>
2021-12-08 23:36:34 +08:00
);
};
export default Layout;