Files
clash-verge-rev/src/pages/profiles.tsx
T

137 lines
3.7 KiB
TypeScript
Raw Normal View History

2021-12-17 00:44:51 +08:00
import useSWR, { useSWRConfig } from "swr";
2022-01-16 03:11:07 +08:00
import { useEffect, useRef, useState } from "react";
import { Box, Button, Grid, TextField } from "@mui/material";
2021-12-28 01:47:43 +08:00
import {
getProfiles,
2022-01-05 02:00:59 +08:00
selectProfile,
patchProfile,
2021-12-28 01:47:43 +08:00
importProfile,
} from "../services/cmds";
import { getProxies, updateProxy } from "../services/api";
2021-12-21 22:48:39 +08:00
import noop from "../utils/noop";
2022-01-16 17:56:43 +08:00
import Notice from "../components/notice";
2022-01-16 03:11:07 +08:00
import BasePage from "../components/base-page";
2022-01-16 18:30:25 +08:00
import ProfileItem from "../components/profile-item";
2021-12-12 01:09:51 +08:00
2021-12-25 01:08:03 +08:00
const ProfilePage = () => {
2021-12-13 02:30:34 +08:00
const [url, setUrl] = useState("");
2021-12-15 01:54:06 +08:00
const [disabled, setDisabled] = useState(false);
2021-12-16 00:08:28 +08:00
2021-12-17 00:44:51 +08:00
const { mutate } = useSWRConfig();
const { data: profiles = {} } = useSWR("getProfiles", getProfiles);
2021-12-28 01:47:43 +08:00
useEffect(() => {
if (profiles.current == null) return;
2021-12-29 01:01:22 +08:00
if (!profiles.items) profiles.items = [];
2021-12-28 01:47:43 +08:00
2022-01-08 16:52:18 +08:00
const current = profiles.current;
const profile = profiles.items![current];
2021-12-28 01:47:43 +08:00
if (!profile) return;
2022-01-08 16:52:18 +08:00
setTimeout(async () => {
const proxiesData = await getProxies();
2021-12-28 01:47:43 +08:00
mutate("getProxies", proxiesData);
2022-01-08 16:52:18 +08:00
2021-12-28 01:47:43 +08:00
// init selected array
const { selected = [] } = profile;
const selectedMap = Object.fromEntries(
selected.map((each) => [each.name!, each.now!])
);
2022-01-08 16:52:18 +08:00
2021-12-28 01:47:43 +08:00
// todo: enhance error handle
let hasChange = false;
proxiesData.groups.forEach((group) => {
const { name, now } = group;
if (!now || selectedMap[name] === now) return;
if (selectedMap[name] == null) {
selectedMap[name] = now!;
} else {
hasChange = true;
updateProxy(name, selectedMap[name]);
}
});
// update profile selected list
profile.selected = Object.entries(selectedMap).map(([name, now]) => ({
name,
now,
}));
2022-01-08 16:52:18 +08:00
patchProfile(current!, profile).catch(console.error);
2021-12-28 01:47:43 +08:00
// update proxies cache
if (hasChange) mutate("getProxies", getProxies());
2022-01-08 16:52:18 +08:00
}, 100);
2021-12-28 01:47:43 +08:00
}, [profiles]);
2021-12-21 22:48:39 +08:00
const onImport = async () => {
2021-12-13 02:30:34 +08:00
if (!url) return;
2021-12-15 01:54:06 +08:00
setUrl("");
setDisabled(true);
2021-12-21 22:48:39 +08:00
try {
await importProfile(url);
mutate("getProfiles", getProfiles());
2022-01-05 02:00:59 +08:00
if (!profiles.items?.length) selectProfile(0).catch(noop);
2022-01-16 17:56:43 +08:00
Notice.success("Successfully import profile.");
2021-12-21 22:48:39 +08:00
} catch {
2022-01-16 17:56:43 +08:00
Notice.error("Failed to import profile.");
2021-12-21 22:48:39 +08:00
} finally {
setDisabled(false);
}
2021-12-13 02:30:34 +08:00
};
2021-12-20 01:09:32 +08:00
const lockRef = useRef(false);
2021-12-17 00:44:51 +08:00
const onProfileChange = (index: number) => {
2021-12-31 18:21:35 +08:00
if (index === profiles.current || lockRef.current) return;
2021-12-20 01:09:32 +08:00
if (lockRef.current) return;
lockRef.current = true;
2022-01-05 02:00:59 +08:00
selectProfile(index)
2021-12-17 00:44:51 +08:00
.then(() => {
mutate("getProfiles", { ...profiles, current: index }, true);
})
.catch((err) => {
console.error(err);
2021-12-20 01:09:32 +08:00
})
.finally(() => {
lockRef.current = false;
2021-12-17 00:44:51 +08:00
});
};
2021-12-12 01:09:51 +08:00
return (
2022-01-16 03:11:07 +08:00
<BasePage title="Profiles">
2021-12-17 00:44:51 +08:00
<Box sx={{ display: "flex", mb: 3 }}>
2021-12-16 00:08:28 +08:00
<TextField
2021-12-17 00:44:51 +08:00
id="profile_url"
name="profile_url"
2021-12-16 00:08:28 +08:00
label="Profile URL"
size="small"
fullWidth
value={url}
onChange={(e) => setUrl(e.target.value)}
2022-01-16 17:56:43 +08:00
sx={{ mr: 2 }}
2021-12-16 00:08:28 +08:00
/>
2021-12-17 00:44:51 +08:00
<Button
disabled={!url || disabled}
variant="contained"
2021-12-21 22:48:39 +08:00
onClick={onImport}
2021-12-17 00:44:51 +08:00
>
2021-12-16 00:08:28 +08:00
Import
</Button>
</Box>
2021-12-15 01:54:06 +08:00
2021-12-17 00:44:51 +08:00
<Grid container spacing={3}>
{profiles?.items?.map((item, idx) => (
<Grid item xs={12} sm={6} key={item.file}>
2022-01-16 18:30:25 +08:00
<ProfileItem
2021-12-26 14:08:25 +08:00
index={idx}
2021-12-17 00:44:51 +08:00
selected={profiles.current === idx}
itemData={item}
onClick={() => onProfileChange(idx)}
/>
</Grid>
))}
</Grid>
2022-01-16 03:11:07 +08:00
</BasePage>
2021-12-12 01:09:51 +08:00
);
};
2021-12-25 01:08:03 +08:00
export default ProfilePage;