import { useEffect, useMemo, useState } from "react"; import useSWR, { SWRConfig } from "swr"; import { Route, Routes } from "react-router-dom"; import { useRecoilState } from "recoil"; import { alpha, Button, createTheme, IconButton, List, Paper, ThemeProvider, } from "@mui/material"; import { HorizontalRuleRounded, CloseRounded } from "@mui/icons-material"; import { checkUpdate } from "@tauri-apps/api/updater"; import { atomPaletteMode, atomThemeBlur } from "../states/setting"; import { getVergeConfig, windowDrag, windowHide } from "../services/cmds"; import LogoSvg from "../assets/image/logo.svg"; import LogPage from "./log"; import ProfilePage from "./profile"; import ProxyPage from "./proxy"; import SettingPage from "./setting"; import ConnectionsPage from "./connections"; import LayoutItem from "../components/layout-item"; import Traffic from "../components/traffic"; import UpdateDialog from "../components/update-dialog"; const routers = [ { label: "Proxy", link: "/", ele: ProxyPage, }, { label: "Profile", link: "/profile", ele: ProfilePage, }, { label: "Connections", link: "/connections", ele: ConnectionsPage, }, { label: "Log", link: "/log", ele: LogPage, }, { label: "Setting", link: "/setting", ele: SettingPage, }, ]; const Layout = () => { const [mode, setMode] = useRecoilState(atomPaletteMode); const [blur, setBlur] = useRecoilState(atomThemeBlur); const { data: vergeConfig } = useSWR("getVergeConfig", getVergeConfig); const { data: updateInfo } = useSWR("checkUpdate", checkUpdate, { errorRetryCount: 2, revalidateIfStale: false, focusThrottleInterval: 36e5, // 1 hour }); const [dialogOpen, setDialogOpen] = useState(false); useEffect(() => { window.addEventListener("keydown", (e) => { if (e.key === "Escape") windowHide(); }); }, []); useEffect(() => { if (!vergeConfig) return; setBlur(!!vergeConfig.theme_blur); setMode(vergeConfig.theme_mode ?? "light"); }, [vergeConfig]); const theme = useMemo(() => { // 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); return createTheme({ breakpoints: { values: { xs: 0, sm: 650, md: 900, lg: 1200, xl: 1536 }, }, palette: { mode, primary: { main: "#5b5c9d" }, text: { primary: "#637381", secondary: "#909399" }, }, }); }, [mode]); return ( ({ bgcolor: alpha(theme.palette.background.paper, blur ? 0.85 : 1), }), ]} >
{ windowDrag(); e.preventDefault(); }} /> {updateInfo?.shouldUpdate && ( )}
{routers.map((router) => ( {router.label} ))}
e.target === e.currentTarget && windowDrag() } > {/* todo: onClick = windowMini */}
{routers.map(({ label, link, ele: Ele }) => ( } /> ))}
setDialogOpen(false)} />
); }; export default Layout;