clash-verge/src/pages/_layout.tsx

113 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-12-19 19:01:32 +03:00
import useSWR, { SWRConfig } 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";
2021-12-19 19:01:32 +03:00
import { useRecoilState } from "recoil";
2022-01-15 22:11:07 +03:00
import { alpha, createTheme, List, Paper, ThemeProvider } from "@mui/material";
import { appWindow } from "@tauri-apps/api/window";
2022-01-11 21:27:29 +03:00
import { atomPaletteMode, atomThemeBlur } from "../states/setting";
2022-01-15 22:11:07 +03:00
import { getVergeConfig } from "../services/cmds";
import { routers } from "./_routers";
2021-12-11 15:35:02 +03:00
import LogoSvg from "../assets/image/logo.svg";
2021-12-08 18:36:34 +03:00
import Traffic from "../components/traffic";
2022-01-15 22:11:07 +03:00
import LayoutItem from "../components/layout-item";
import UpdateButton from "../components/update-button";
import WindowControl from "../components/window-control";
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);
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
});
}, []);
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
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}
className="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>
<Traffic />
</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>
<div className="the-bar">
<WindowControl />
2022-01-08 17:23:48 +03:00
</div>
2022-01-15 22:11:07 +03:00
<div className="the-content" data-windrag>
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;