feat: profiles ui and put profile support

This commit is contained in:
GyDi
2021-12-17 00:44:51 +08:00
parent 8d7ef0dc94
commit ad60013f52
5 changed files with 217 additions and 36 deletions

View File

@@ -1,4 +1,5 @@
import { useMemo } from "react";
import { SWRConfig } from "swr";
import { Route, Routes } from "react-router-dom";
import { useRecoilValue } from "recoil";
import { createTheme, List, Paper, ThemeProvider } from "@mui/material";
@@ -55,6 +56,15 @@ const Layout = () => {
}
return createTheme({
breakpoints: {
values: {
xs: 0,
sm: 650,
md: 900,
lg: 1200,
xl: 1536,
},
},
palette: {
mode: paletteMode,
primary: {
@@ -69,38 +79,40 @@ const Layout = () => {
}, [paletteMode]);
return (
<ThemeProvider theme={theme}>
<Paper square elevation={0} className="layout">
<div className="layout__sidebar">
<div className="layout__logo">
<img src={LogoSvg} width="100%" alt="" />
<SWRConfig value={{}}>
<ThemeProvider theme={theme}>
<Paper square elevation={0} className="layout">
<div className="layout__sidebar">
<div className="layout__logo">
<img src={LogoSvg} width="100%" alt="" />
</div>
<List sx={{ userSelect: "none" }}>
{routers.map((router) => (
<ListItemLink key={router.label} to={router.link}>
{router.label}
</ListItemLink>
))}
</List>
<div className="layout__traffic">
<Traffic />
</div>
</div>
<List sx={{ userSelect: "none" }}>
{routers.map((router) => (
<ListItemLink key={router.label} to={router.link}>
{router.label}
</ListItemLink>
))}
</List>
<div className="layout__traffic">
<Traffic />
<div className="layout__content">
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/proxy" element={<ProxyPage />} />
<Route path="/rules" element={<RulesPage />} />
<Route path="/log" element={<LogPage />} />
<Route path="/connections" element={<ConnectionsPage />} />
<Route path="/setting" element={<SettingPage />} />
</Routes>
</div>
</div>
<div className="layout__content">
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/proxy" element={<ProxyPage />} />
<Route path="/rules" element={<RulesPage />} />
<Route path="/log" element={<LogPage />} />
<Route path="/connections" element={<ConnectionsPage />} />
<Route path="/setting" element={<SettingPage />} />
</Routes>
</div>
</Paper>
</ThemeProvider>
</Paper>
</ThemeProvider>
</SWRConfig>
);
};

View File

@@ -1,14 +1,18 @@
import { useState } from "react";
import { Box, Button, TextField, Typography } from "@mui/material";
import { importProfile } from "../services/command";
import useSWR, { useSWRConfig } from "swr";
import { Box, Button, Grid, TextField, Typography } from "@mui/material";
import { getProfiles, importProfile, putProfiles } from "../services/command";
import ProfileItemComp from "../components/profile-item";
import useNotice from "../utils/use-notice";
const RulesPage = () => {
const [url, setUrl] = useState("");
const [disabled, setDisabled] = useState(false);
const [notice, noticeElement] = useNotice();
const { mutate } = useSWRConfig();
const { data: profiles = {} } = useSWR("getProfiles", getProfiles);
const onClick = () => {
if (!url) return;
setUrl("");
@@ -19,14 +23,26 @@ const RulesPage = () => {
.finally(() => setDisabled(false));
};
const onProfileChange = (index: number) => {
putProfiles(index)
.then(() => {
mutate("getProfiles", { ...profiles, current: index }, true);
})
.catch((err) => {
console.error(err);
});
};
return (
<Box sx={{ width: 0.9, maxWidth: "850px", mx: "auto", mb: 2 }}>
<Typography variant="h4" component="h1" sx={{ py: 2, mb: 1 }}>
Rules
</Typography>
<Box sx={{ display: "flex" }}>
<Box sx={{ display: "flex", mb: 3 }}>
<TextField
id="profile_url"
name="profile_url"
label="Profile URL"
size="small"
fullWidth
@@ -34,11 +50,27 @@ const RulesPage = () => {
onChange={(e) => setUrl(e.target.value)}
sx={{ mr: 4 }}
/>
<Button disabled={disabled} variant="contained" onClick={onClick}>
<Button
disabled={!url || disabled}
variant="contained"
onClick={onClick}
>
Import
</Button>
</Box>
<Grid container spacing={3}>
{profiles?.items?.map((item, idx) => (
<Grid item xs={12} sm={6} key={item.file}>
<ProfileItemComp
selected={profiles.current === idx}
itemData={item}
onClick={() => onProfileChange(idx)}
/>
</Grid>
))}
</Grid>
{noticeElement}
</Box>
);