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-01-21 00:29:33 +08:00
|
|
|
import useSWR, { SWRConfig, useSWRConfig } 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";
|
2022-01-21 00:29:33 +08:00
|
|
|
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";
|
|
|
|
import { getVergeConfig, getProfiles } from "@/services/cmds";
|
|
|
|
import { ReactComponent as LogoSvg } from "@/assets/image/logo.svg";
|
|
|
|
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
|
|
|
|
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-01-21 00:29:33 +08:00
|
|
|
const { mutate } = useSWRConfig();
|
2022-02-19 18:14:40 +08:00
|
|
|
|
2022-03-30 12:36:39 +08:00
|
|
|
const { theme } = useCustomTheme();
|
|
|
|
|
|
|
|
const { data: vergeConfig } = useSWR("getVergeConfig", getVergeConfig);
|
|
|
|
const { theme_blur, language } = vergeConfig || {};
|
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-05-25 16:06:39 +08:00
|
|
|
if (e.key === "Escape") appWindow.close();
|
2022-01-13 02:11:50 +08:00
|
|
|
});
|
2022-01-21 00:29:33 +08:00
|
|
|
|
2022-03-13 01:37:41 +08:00
|
|
|
listen("verge://refresh-clash-config", async () => {
|
2022-01-21 00:29:33 +08:00
|
|
|
// the clash info may be updated
|
|
|
|
await getAxios(true);
|
2022-03-13 01:37:41 +08:00
|
|
|
mutate("getProxies");
|
2022-01-21 00:29:33 +08:00
|
|
|
mutate("getClashConfig");
|
|
|
|
});
|
2022-03-20 01:36:43 +08:00
|
|
|
|
|
|
|
// update the verge config
|
|
|
|
listen("verge://refresh-verge-config", () => mutate("getVergeConfig"));
|
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 (
|
2021-12-17 00:44:51 +08:00
|
|
|
<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();
|
|
|
|
}}
|
2022-03-19 19:28:53 +08:00
|
|
|
onContextMenu={(e) => {
|
|
|
|
// only prevent it on Windows
|
2022-03-22 01:36:06 +08:00
|
|
|
if (OS === "windows") e.preventDefault();
|
2022-03-19 19:28:53 +08:00
|
|
|
}}
|
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
|
|
|
|
2022-01-16 03:11:07 +08:00
|
|
|
<UpdateButton className="the-newbtn" />
|
2021-12-17 00:44:51 +08:00
|
|
|
</div>
|
2021-12-08 23:36:34 +08:00
|
|
|
|
2022-05-06 14:04:39 +08:00
|
|
|
<List className="the-menu">
|
2021-12-17 00:44:51 +08:00
|
|
|
{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>
|
2021-12-17 00:44:51 +08:00
|
|
|
))}
|
|
|
|
</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 />
|
2021-12-17 00:44:51 +08:00
|
|
|
</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-01-08 22:23:48 +08:00
|
|
|
<Routes>
|
2022-01-10 02:05:35 +08:00
|
|
|
{routers.map(({ label, link, ele: Ele }) => (
|
|
|
|
<Route key={label} path={link} element={<Ele />} />
|
2022-01-08 22:23:48 +08:00
|
|
|
))}
|
|
|
|
</Routes>
|
|
|
|
</div>
|
2021-12-17 00:44:51 +08:00
|
|
|
</div>
|
|
|
|
</Paper>
|
|
|
|
</ThemeProvider>
|
|
|
|
</SWRConfig>
|
2021-12-08 23:36:34 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Layout;
|