Files
clash-verge-rev/src/pages/_layout.tsx

261 lines
7.9 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";
import { useEffect, useCallback } from "react";
2022-03-12 23:07:45 +08:00
import { useTranslation } from "react-i18next";
import { useLocation, useRoutes, useNavigate } from "react-router-dom";
2024-05-14 16:53:39 +08:00
import { List, Paper, ThemeProvider, SvgIcon } from "@mui/material";
2024-09-02 19:33:17 +08:00
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
2022-01-16 03:11:07 +08:00
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-05-14 16:53:39 +08:00
import iconLight from "@/assets/image/icon_light.svg?react";
import iconDark from "@/assets/image/icon_dark.svg?react";
2024-06-07 12:27:37 +08:00
import { useThemeMode } from "@/services/states";
import { Notice } from "@/components/base";
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";
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";
import React from "react";
import { TransitionGroup, CSSTransition } from "react-transition-group";
2024-09-25 21:07:01 +08:00
import { useListen } from "@/hooks/use-listen";
2025-02-13 02:18:17 +08:00
import { listen } from "@tauri-apps/api/event";
2024-09-25 15:45:12 +08:00
2024-09-02 19:33:17 +08:00
const appWindow = getCurrentWebviewWindow();
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
// 通知处理函数
const handleNoticeMessage = (
status: string,
msg: string,
t: (key: string) => string,
navigate: (path: string, options?: any) => void,
) => {
console.log("[通知监听] 收到消息:", status, msg);
switch (status) {
case "import_sub_url::ok":
navigate("/profile", { state: { current: msg } });
Notice.success(t("Import Subscription Successful"));
break;
case "import_sub_url::error":
navigate("/profile");
Notice.error(msg);
break;
case "set_config::error":
Notice.error(msg);
break;
case "config_validate::boot_error":
Notice.error(t("Boot Config Validation Failed"));
break;
case "config_validate::core_change":
Notice.error(t("Core Change Config Validation Failed"));
break;
case "config_validate::error":
Notice.error(t("Config Validation Failed"));
break;
case "config_validate::process_terminated":
Notice.error(t("Config Validation Process Terminated"));
break;
case "config_validate::stderr_error":
Notice.error(msg);
break;
}
};
2021-12-08 23:36:34 +08:00
const Layout = () => {
2024-06-07 12:27:37 +08:00
const mode = useThemeMode();
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();
const { language, start_page } = verge ?? {};
2024-01-18 15:36:44 +08:00
const navigate = useNavigate();
2023-11-11 18:32:11 +08:00
const location = useLocation();
const routersEles = useRoutes(routers);
2024-09-25 21:07:01 +08:00
const { addListener, setupCloseListener } = useListen();
2023-11-11 18:32:11 +08:00
const handleNotice = useCallback(
(payload: [string, string]) => {
const [status, msg] = payload;
handleNoticeMessage(status, msg, t, navigate);
},
[t, navigate],
);
2024-09-25 21:07:01 +08:00
// 设置监听器
2022-01-13 02:11:50 +08:00
useEffect(() => {
const listeners = [
// 配置更新监听
addListener("verge://refresh-clash-config", async () => {
await getAxios(true);
mutate("getProxies");
mutate("getVersion");
mutate("getClashConfig");
mutate("getProxyProviders");
}),
2025-02-13 02:18:17 +08:00
// verge 配置更新监听
addListener("verge://refresh-verge-config", () =>
mutate("getVergeConfig"),
),
// 通知消息监听
addListener("verge://notice-message", ({ payload }) =>
handleNotice(payload as [string, string]),
),
];
// 设置窗口显示/隐藏监听
const setupWindowListeners = async () => {
const [hideUnlisten, showUnlisten] = await Promise.all([
listen("verge://hide-window", () => appWindow.hide()),
listen("verge://show-window", () => appWindow.show()),
]);
2025-02-13 02:18:17 +08:00
return () => {
hideUnlisten();
showUnlisten();
2025-02-13 02:18:17 +08:00
};
};
// 初始化
setupCloseListener();
const cleanupWindow = setupWindowListeners();
2022-01-13 02:11:50 +08:00
// 清理函数
return () => {
// 清理主要监听器
listeners.forEach((listener) => {
if (typeof listener.then === "function") {
listener.then((unlisten) => unlisten());
}
});
// 清理窗口监听器
cleanupWindow.then((cleanup) => cleanup());
};
}, [handleNotice]);
// 语言和起始页设置
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, navigate]);
if (!routersEles) return null;
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 }}>
<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`}
onContextMenu={(e) => {
if (
OS === "windows" &&
!["input", "textarea"].includes(
e.currentTarget.tagName.toLowerCase(),
) &&
!e.currentTarget.isContentEditable
) {
e.preventDefault();
}
}}
2022-01-12 02:27:29 +08:00
sx={[
({ palette }) => ({ bgcolor: palette.background.paper }),
2025-01-19 09:47:43 +08:00
{
borderRadius: "8px",
border: "2px solid var(--divider-color)",
width: "calc(100vw - 4px)",
height: "calc(100vh - 4px)",
},
2022-01-12 02:27:29 +08:00
]}
>
2024-03-16 17:06:59 +08:00
<div className="layout__left">
2024-03-11 12:36:20 +08:00
<div className="the-logo" data-tauri-drag-region="true">
2024-05-12 21:17:21 +08:00
<div
style={{
height: "27px",
display: "flex",
justifyContent: "space-between",
}}
>
2024-05-14 16:53:39 +08:00
<SvgIcon
component={isDark ? iconDark : iconLight}
2024-05-12 21:17:21 +08:00
style={{
height: "36px",
width: "36px",
marginTop: "-3px",
marginRight: "5px",
marginLeft: "-3px",
}}
2024-05-14 16:53:39 +08:00
inheritViewBox
2024-05-12 21:17:21 +08:00
/>
<LogoSvg fill={isDark ? "white" : "black"} />
</div>
<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) => (
<LayoutItem
key={router.label}
to={router.path}
icon={router.icon}
>
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
2024-03-11 12:36:20 +08:00
<div className="the-traffic">
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
2024-03-11 12:36:20 +08:00
<div className="layout__right">
<div className="the-bar">
<div
className="the-dragbar"
data-tauri-drag-region="true"
style={{ width: "100%" }}
/>
{OS !== "macos" && <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"
>
{React.cloneElement(routersEles, { key: location.pathname })}
2023-11-11 18:32:11 +08:00
</CSSTransition>
</TransitionGroup>
</div>
</Paper>
</ThemeProvider>
</SWRConfig>
2021-12-08 23:36:34 +08:00
);
};
export default Layout;