clash-verge/src/pages/_layout.tsx

183 lines
5.0 KiB
TypeScript
Raw Normal View History

2022-01-09 21:05:35 +03:00
import { useEffect, useMemo, useState } from "react";
2021-12-19 19:01:32 +03:00
import useSWR, { SWRConfig } from "swr";
2021-12-08 18:36:34 +03:00
import { Route, Routes } from "react-router-dom";
2021-12-19 19:01:32 +03:00
import { useRecoilState } from "recoil";
2022-01-08 17:23:48 +03:00
import {
2022-01-11 21:27:29 +03:00
alpha,
2022-01-09 21:05:35 +03:00
Button,
2022-01-08 17:23:48 +03:00
createTheme,
IconButton,
List,
Paper,
ThemeProvider,
} from "@mui/material";
import { HorizontalRuleRounded, CloseRounded } from "@mui/icons-material";
2022-01-09 21:05:35 +03:00
import { checkUpdate } from "@tauri-apps/api/updater";
2022-01-11 21:27:29 +03:00
import { atomPaletteMode, atomThemeBlur } from "../states/setting";
2022-01-08 17:23:48 +03:00
import { getVergeConfig, windowDrag, windowHide } from "../services/cmds";
2021-12-11 15:35:02 +03:00
import LogoSvg from "../assets/image/logo.svg";
2021-12-24 20:08:03 +03:00
import LogPage from "./log";
import ProfilePage from "./profile";
import ProxyPage from "./proxy";
import SettingPage from "./setting";
import ConnectionsPage from "./connections";
2021-12-25 17:33:29 +03:00
import LayoutItem from "../components/layout-item";
2021-12-08 18:36:34 +03:00
import Traffic from "../components/traffic";
2022-01-09 21:05:35 +03:00
import UpdateDialog from "../components/update-dialog";
2021-12-08 18:36:34 +03:00
2021-12-19 19:01:32 +03:00
const routers = [
{
2022-01-07 21:54:37 +03:00
label: "Proxy",
2022-01-08 17:23:48 +03:00
link: "/",
ele: ProxyPage,
2021-12-19 19:01:32 +03:00
},
{
2022-01-07 21:54:37 +03:00
label: "Profile",
2021-12-24 20:08:03 +03:00
link: "/profile",
2022-01-08 17:23:48 +03:00
ele: ProfilePage,
2021-12-19 19:01:32 +03:00
},
{
2022-01-07 21:54:37 +03:00
label: "Connections",
2021-12-19 19:01:32 +03:00
link: "/connections",
2022-01-08 17:23:48 +03:00
ele: ConnectionsPage,
2021-12-19 19:01:32 +03:00
},
{
2022-01-07 21:54:37 +03:00
label: "Log",
2021-12-19 19:01:32 +03:00
link: "/log",
2022-01-08 17:23:48 +03:00
ele: LogPage,
2021-12-19 19:01:32 +03:00
},
{
2022-01-07 21:54:37 +03:00
label: "Setting",
2021-12-19 19:01:32 +03:00
link: "/setting",
2022-01-08 17:23:48 +03:00
ele: SettingPage,
2021-12-19 19:01:32 +03:00
},
];
2021-12-08 18:36:34 +03:00
const Layout = () => {
2021-12-19 19:01:32 +03:00
const [mode, setMode] = useRecoilState(atomPaletteMode);
2022-01-11 21:27:29 +03:00
const [blur, setBlur] = useRecoilState(atomThemeBlur);
2021-12-19 19:01:32 +03:00
const { data: vergeConfig } = useSWR("getVergeConfig", getVergeConfig);
2022-01-09 21:05:35 +03:00
const { data: updateInfo } = useSWR("checkUpdate", checkUpdate, {
errorRetryCount: 2,
revalidateIfStale: false,
focusThrottleInterval: 36e5, // 1 hour
});
const [dialogOpen, setDialogOpen] = useState(false);
2021-12-09 18:28:57 +03:00
2022-01-12 21:11:50 +03:00
useEffect(() => {
window.addEventListener("keydown", (e) => {
if (e.key === "Escape") windowHide();
});
}, []);
2021-12-19 19:01:32 +03:00
useEffect(() => {
2022-01-11 21:27:29 +03:00
if (!vergeConfig) return;
setBlur(!!vergeConfig.theme_blur);
setMode(vergeConfig.theme_mode ?? "light");
}, [vergeConfig]);
2021-12-08 18:36:34 +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
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}
className="layout"
sx={[
(theme) => ({
bgcolor: alpha(theme.palette.background.paper, blur ? 0.85 : 1),
}),
]}
>
2022-01-08 17:23:48 +03:00
<div className="layout__left">
<div className="the-logo">
<img
src={LogoSvg}
width="100%"
alt=""
onPointerDown={(e) => {
windowDrag();
e.preventDefault();
}}
/>
2022-01-09 21:05:35 +03:00
{updateInfo?.shouldUpdate && (
<Button
color="error"
variant="contained"
size="small"
className="the-newbtn"
onClick={() => setDialogOpen(true)}
>
New
</Button>
)}
</div>
2021-12-08 18:36:34 +03:00
2022-01-08 17:23:48 +03:00
<List className="the-menu">
{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-08 17:23:48 +03:00
<div className="the-traffic">
<Traffic />
</div>
2021-12-09 18:28:57 +03:00
</div>
2021-12-08 18:36:34 +03:00
2022-01-08 17:23:48 +03:00
<div className="layout__right">
<div
className="the-bar"
onPointerDown={(e) =>
e.target === e.currentTarget && windowDrag()
}
>
{/* todo: onClick = windowMini */}
<IconButton size="small" sx={{ mx: 1 }} onClick={windowHide}>
<HorizontalRuleRounded fontSize="inherit" />
</IconButton>
<IconButton size="small" onClick={windowHide}>
<CloseRounded fontSize="inherit" />
</IconButton>
</div>
<div className="the-content">
<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>
2022-01-09 21:05:35 +03:00
<UpdateDialog open={dialogOpen} onClose={() => setDialogOpen(false)} />
</ThemeProvider>
</SWRConfig>
2021-12-08 18:36:34 +03:00
);
};
export default Layout;