2022-01-20 19:29:33 +03:00
|
|
|
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";
|
2022-01-20 19:29:33 +03:00
|
|
|
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";
|
2022-02-19 13:14:40 +03:00
|
|
|
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 = () => {
|
2022-01-20 19:29:33 +03:00
|
|
|
const { mutate } = useSWRConfig();
|
2022-02-19 13:14:40 +03:00
|
|
|
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
|
|
|
});
|
2022-01-20 19:29:33 +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({
|
2021-12-16 19:44:51 +03:00
|
|
|
breakpoints: {
|
2022-01-08 17:23:48 +03:00
|
|
|
values: { xs: 0, sm: 650, md: 900, lg: 1200, xl: 1536 },
|
2021-12-16 19:44:51 +03:00
|
|
|
},
|
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 (
|
2021-12-16 19:44:51 +03:00
|
|
|
<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" />
|
2021-12-16 19:44:51 +03:00
|
|
|
</div>
|
2021-12-08 18:36:34 +03:00
|
|
|
|
2022-01-15 22:11:07 +03:00
|
|
|
<List className="the-menu" data-windrag>
|
2021-12-16 19:44:51 +03:00
|
|
|
{routers.map((router) => (
|
2021-12-25 17:33:29 +03:00
|
|
|
<LayoutItem key={router.label} to={router.link}>
|
2021-12-16 19:44:51 +03:00
|
|
|
{router.label}
|
2021-12-25 17:33:29 +03:00
|
|
|
</LayoutItem>
|
2021-12-16 19:44:51 +03:00
|
|
|
))}
|
|
|
|
</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 />
|
2021-12-16 19:44:51 +03:00
|
|
|
</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>
|
2021-12-16 19:44:51 +03:00
|
|
|
</div>
|
|
|
|
</Paper>
|
|
|
|
</ThemeProvider>
|
|
|
|
</SWRConfig>
|
2021-12-08 18:36:34 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Layout;
|