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

210 lines
6.4 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-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";
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
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();
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();
const routersEles = useRoutes(routers);
2024-09-25 21:07:01 +08:00
const { addListener, setupCloseListener } = useListen();
if (!routersEles) return null;
2023-11-11 18:32:11 +08:00
2024-09-25 21:07:01 +08:00
setupCloseListener();
2022-01-13 02:11:50 +08:00
useEffect(() => {
2024-09-25 21:07:01 +08:00
addListener("verge://refresh-clash-config", async () => {
// the clash info may be updated
2024-01-15 10:18:04 +08:00
await getAxios(true);
mutate("getProxies");
2022-11-11 22:45:32 +08:00
mutate("getVersion");
mutate("getClashConfig");
2024-01-18 01:01:47 +08:00
mutate("getProxyProviders");
});
// update the verge config
2024-09-25 21:07:01 +08:00
addListener("verge://refresh-verge-config", () => mutate("getVergeConfig"));
2022-06-04 18:55:39 +08:00
2022-09-26 20:46:29 +08:00
// 设置提示监听
2024-09-25 21:07:01 +08:00
addListener("verge://notice-message", ({ payload }) => {
2022-09-26 20:46:29 +08:00
const [status, msg] = payload as [string, string];
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;
2024-11-30 07:04:09 +08:00
case "set_config::error":
Notice.error(msg);
break;
2022-09-26 20:46:29 +08:00
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 }}>
<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) => {
// only prevent it on Windows
const validList = ["input", "textarea"];
const target = e.currentTarget;
if (
OS === "windows" &&
!(
validList.includes(target.tagName.toLowerCase()) ||
target.isContentEditable
)
) {
e.preventDefault();
}
}}
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
}),
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>
2024-03-11 12:36:20 +08:00
{<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">
{
2024-03-16 20:37:39 +08:00
<div className="the-bar">
<div
className="the-dragbar"
data-tauri-drag-region="true"
style={{ width: "100%" }}
></div>
{OS !== "macos" && <LayoutControl />}
2022-02-20 23:46:13 +08:00
</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;