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-03-12 23:07:45 +08:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-11-11 18:32:11 +08:00
|
|
|
import { Route, Routes, useLocation } from "react-router-dom";
|
|
|
|
|
import { CSSTransition, TransitionGroup } from "react-transition-group";
|
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";
|
2024-01-15 10:18:04 +08:00
|
|
|
import { getAxios } from "@/services/api";
|
2022-11-20 20:12:58 +08:00
|
|
|
import { useVerge } from "@/hooks/use-verge";
|
2023-10-21 16:43:16 +08:00
|
|
|
import LogoSvg from "@/assets/image/logo.svg?react";
|
2024-03-09 21:37:21 +08:00
|
|
|
import LogoSvg_dark from "@/assets/image/logo_dark.svg?react";
|
|
|
|
|
import { atomThemeMode } from "@/services/states";
|
|
|
|
|
import { useRecoilState } from "recoil";
|
2022-11-20 22:03:55 +08:00
|
|
|
import { BaseErrorBoundary, Notice } from "@/components/base";
|
2023-09-10 14:30:31 +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";
|
2022-08-06 02:35:11 +08:00
|
|
|
import getSystem from "@/utils/get-system";
|
2023-08-03 03:07:58 +00:00
|
|
|
import "dayjs/locale/ru";
|
2022-03-18 14:45:24 +08:00
|
|
|
import "dayjs/locale/zh-cn";
|
2023-12-15 21:39:34 +08:00
|
|
|
import { getPortableFlag } from "@/services/cmds";
|
2024-01-18 15:36:44 +08:00
|
|
|
import { useNavigate } from "react-router-dom";
|
2023-12-15 21:39:34 +08:00
|
|
|
export let portableFlag = false;
|
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 = () => {
|
2024-03-09 21:37:21 +08:00
|
|
|
const [mode] = useRecoilState(atomThemeMode);
|
|
|
|
|
console.log(mode);
|
|
|
|
|
const isDark = mode === "light" ? false : true;
|
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();
|
2024-01-18 15:36:44 +08:00
|
|
|
const { language, start_page } = verge || {};
|
|
|
|
|
const navigate = useNavigate();
|
2023-11-11 18:32:11 +08:00
|
|
|
const location = useLocation();
|
|
|
|
|
|
2022-01-13 02:11:50 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
window.addEventListener("keydown", (e) => {
|
2023-03-16 20:42:45 +08:00
|
|
|
// macOS有cmd+w
|
|
|
|
|
if (e.key === "Escape" && OS !== "macos") {
|
|
|
|
|
appWindow.close();
|
2022-09-09 16:42:35 +08:00
|
|
|
}
|
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
|
2024-01-15 10:18:04 +08:00
|
|
|
await getAxios(true);
|
2022-03-13 01:37:41 +08:00
|
|
|
mutate("getProxies");
|
2022-11-11 22:45:32 +08:00
|
|
|
mutate("getVersion");
|
2022-01-21 00:29:33 +08:00
|
|
|
mutate("getClashConfig");
|
2024-01-18 01:01:47 +08:00
|
|
|
mutate("getProxyProviders");
|
2022-01-21 00:29:33 +08:00
|
|
|
});
|
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
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-12-15 21:39:34 +08:00
|
|
|
|
|
|
|
|
setTimeout(async () => {
|
|
|
|
|
portableFlag = await getPortableFlag();
|
|
|
|
|
await appWindow.unminimize();
|
|
|
|
|
await appWindow.show();
|
|
|
|
|
await appWindow.setFocus();
|
2023-12-08 13:10:35 +08:00
|
|
|
}, 50);
|
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
|
|
|
}
|
2024-01-18 15:36:44 +08:00
|
|
|
if (start_page) {
|
|
|
|
|
navigate(start_page);
|
|
|
|
|
}
|
|
|
|
|
}, [language, start_page]);
|
2022-01-16 03:11:07 +08:00
|
|
|
|
2021-12-08 23:36:34 +08:00
|
|
|
return (
|
2023-03-16 17:03:12 +08:00
|
|
|
<SWRConfig value={{ errorRetryCount: 3 }}>
|
2021-12-17 00:44:51 +08:00
|
|
|
<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
|
2023-07-22 17:21:04 +08:00
|
|
|
const validList = ["input", "textarea"];
|
|
|
|
|
const target = e.currentTarget;
|
|
|
|
|
if (
|
|
|
|
|
OS === "windows" &&
|
|
|
|
|
!(
|
|
|
|
|
validList.includes(target.tagName.toLowerCase()) ||
|
|
|
|
|
target.isContentEditable
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
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 }) => ({
|
2024-01-15 17:13:55 +08:00
|
|
|
bgcolor: palette.background.paper,
|
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>
|
2024-03-09 21:37:21 +08:00
|
|
|
{!isDark ? <LogoSvg /> : <LogoSvg_dark />}
|
2023-12-15 21:39:34 +08:00
|
|
|
{!portableFlag && <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) => (
|
2024-03-09 21:37:21 +08:00
|
|
|
<LayoutItem
|
|
|
|
|
key={router.label}
|
|
|
|
|
to={router.link}
|
|
|
|
|
img={router.img}
|
|
|
|
|
>
|
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>
|
2023-01-12 00:42:33 +08:00
|
|
|
{OS === "windows" && (
|
2022-02-20 23:46:13 +08:00
|
|
|
<div className="the-bar">
|
|
|
|
|
<LayoutControl />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2022-01-08 22:23:48 +08:00
|
|
|
|
2023-11-11 18:32:11 +08:00
|
|
|
<TransitionGroup className="the-content">
|
|
|
|
|
<CSSTransition
|
|
|
|
|
key={location.pathname}
|
|
|
|
|
timeout={300}
|
|
|
|
|
classNames="page"
|
|
|
|
|
>
|
|
|
|
|
<Routes>
|
|
|
|
|
{routers.map(({ label, link, ele: Ele }) => (
|
|
|
|
|
<Route
|
|
|
|
|
key={label}
|
|
|
|
|
path={link}
|
|
|
|
|
element={
|
|
|
|
|
<BaseErrorBoundary key={label}>
|
|
|
|
|
<Ele />
|
|
|
|
|
</BaseErrorBoundary>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Routes>
|
|
|
|
|
</CSSTransition>
|
|
|
|
|
</TransitionGroup>
|
2021-12-17 00:44:51 +08:00
|
|
|
</div>
|
|
|
|
|
</Paper>
|
|
|
|
|
</ThemeProvider>
|
|
|
|
|
</SWRConfig>
|
2021-12-08 23:36:34 +08:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Layout;
|