clash-verge/src/pages/_layout.tsx

122 lines
3.7 KiB
TypeScript
Raw Normal View History

import useSWR, { SWRConfig, useSWRConfig } from "swr";
2022-01-15 22:11:07 +03:00
import { useEffect, useMemo } from "react";
2021-12-08 18:36:34 +03:00
import { Route, Routes } from "react-router-dom";
2022-01-15 22:11:07 +03:00
import { alpha, createTheme, List, Paper, ThemeProvider } from "@mui/material";
import { listen } from "@tauri-apps/api/event";
2022-01-15 22:11:07 +03:00
import { appWindow } from "@tauri-apps/api/window";
import { routers } from "./_routers";
import { getAxios } from "../services/api";
import { getVergeConfig } from "../services/cmds";
2021-12-11 15:35:02 +03:00
import LogoSvg from "../assets/image/logo.svg";
2022-02-13 14:27:06 +03: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";
2021-12-19 19:01:32 +03:00
2022-02-20 18:46:13 +03:00
const isMacos = navigator.userAgent.includes("Mac OS X");
2021-12-08 18:36:34 +03:00
const Layout = () => {
const { mutate } = useSWRConfig();
const { data } = useSWR("getVergeConfig", getVergeConfig);
const blur = !!data?.theme_blur;
const mode = data?.theme_mode ?? "light";
2021-12-09 18:28:57 +03:00
2022-01-12 21:11:50 +03:00
useEffect(() => {
window.addEventListener("keydown", (e) => {
2022-01-15 22:11:07 +03:00
if (e.key === "Escape") appWindow.hide();
2022-01-12 21:11:50 +03:00
});
listen("restart_clash", async () => {
// the clash info may be updated
await getAxios(true);
// make sure that the clash is ok
setTimeout(() => mutate("getProxies"), 1000);
setTimeout(() => mutate("getProxies"), 2000);
mutate("getClashConfig");
});
2022-01-12 21:11:50 +03:00
}, []);
2021-12-09 18:28:57 +03:00
const theme = useMemo(() => {
2022-01-08 17:23:48 +03:00
// const background = mode === "light" ? "#f5f5f5" : "#000";
const selectColor = mode === "light" ? "#f5f5f5" : "#d5d5d5";
const rootEle = document.documentElement;
rootEle.style.background = "transparent";
rootEle.style.setProperty("--selection-color", selectColor);
2021-12-09 18:28:57 +03:00
return createTheme({
breakpoints: {
2022-01-08 17:23:48 +03:00
values: { xs: 0, sm: 650, md: 900, lg: 1200, xl: 1536 },
},
2021-12-09 18:28:57 +03:00
palette: {
2021-12-19 19:01:32 +03:00
mode,
2022-01-08 17:23:48 +03:00
primary: { main: "#5b5c9d" },
text: { primary: "#637381", secondary: "#909399" },
2021-12-09 18:28:57 +03:00
},
});
2021-12-19 19:01:32 +03:00
}, [mode]);
2021-12-09 18:28:57 +03:00
2022-01-15 22:11:07 +03:00
const onDragging = (e: any) => {
if (e?.target?.dataset?.windrag) {
appWindow.startDragging();
}
};
2021-12-08 18:36:34 +03:00
return (
<SWRConfig value={{}}>
<ThemeProvider theme={theme}>
2022-01-11 21:27:29 +03:00
<Paper
square
elevation={0}
2022-02-20 18:46:13 +03:00
className={`${isMacos ? "macos " : ""}layout`}
2022-01-15 22:11:07 +03:00
onPointerDown={onDragging}
2022-01-11 21:27:29 +03:00
sx={[
(theme) => ({
bgcolor: alpha(theme.palette.background.paper, blur ? 0.85 : 1),
}),
]}
>
2022-01-15 22:11:07 +03:00
<div className="layout__left" data-windrag>
<div className="the-logo" data-windrag>
<img src={LogoSvg} alt="" data-windrag />
2022-01-09 21:05:35 +03:00
2022-01-15 22:11:07 +03:00
<UpdateButton className="the-newbtn" />
</div>
2021-12-08 18:36:34 +03:00
2022-01-15 22:11:07 +03:00
<List className="the-menu" data-windrag>
{routers.map((router) => (
2021-12-25 17:33:29 +03:00
<LayoutItem key={router.label} to={router.link}>
{router.label}
2021-12-25 17:33:29 +03:00
</LayoutItem>
))}
</List>
2021-12-08 18:36:34 +03:00
2022-01-15 22:11:07 +03:00
<div className="the-traffic" data-windrag>
2022-02-13 14:27:06 +03:00
<LayoutTraffic />
</div>
2021-12-09 18:28:57 +03:00
</div>
2021-12-08 18:36:34 +03:00
2022-01-15 22:11:07 +03:00
<div className="layout__right" data-windrag>
2022-02-20 18:46:13 +03:00
{!isMacos && (
<div className="the-bar">
<LayoutControl />
</div>
)}
2022-01-08 17:23:48 +03:00
2022-01-24 20:48:26 +03:00
<div className="the-content">
2022-01-08 17:23:48 +03:00
<Routes>
2022-01-09 21:05:35 +03:00
{routers.map(({ label, link, ele: Ele }) => (
<Route key={label} path={link} element={<Ele />} />
2022-01-08 17:23:48 +03:00
))}
</Routes>
</div>
</div>
</Paper>
</ThemeProvider>
</SWRConfig>
2021-12-08 18:36:34 +03:00
);
};
export default Layout;