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

177 lines
5.0 KiB
TypeScript
Raw Normal View History

2021-12-17 00:44:51 +08:00
import useSWR, { useSWRConfig } from "swr";
2022-02-26 17:39:36 +08:00
import { useLockFn } from "ahooks";
2022-03-06 14:59:25 +08:00
import { useEffect, useMemo, useState } from "react";
2022-03-05 22:54:39 +08:00
import { Box, Button, Grid, TextField } from "@mui/material";
2022-03-12 23:07:45 +08:00
import { useTranslation } from "react-i18next";
2021-12-28 01:47:43 +08:00
import {
getProfiles,
2022-01-05 02:00:59 +08:00
patchProfile,
2022-03-06 14:59:25 +08:00
selectProfile,
2021-12-28 01:47:43 +08:00
importProfile,
} from "../services/cmds";
import { getProxies, updateProxy } from "../services/api";
import Notice from "../components/base/base-notice";
import BasePage from "../components/base/base-page";
import ProfileNew from "../components/profile/profile-new";
2022-03-05 19:04:20 +08:00
import ProfileItem from "../components/profile/profile-item";
2022-04-03 01:41:48 +08:00
import EnhancedMode from "../components/profile/enhanced";
2021-12-12 01:09:51 +08:00
2021-12-25 01:08:03 +08:00
const ProfilePage = () => {
2022-03-12 23:07:45 +08:00
const { t } = useTranslation();
2022-03-05 22:54:39 +08:00
const { mutate } = useSWRConfig();
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);
2022-03-05 19:04:20 +08:00
const [dialogOpen, setDialogOpen] = useState(false);
2021-12-17 00:44:51 +08:00
2022-03-05 22:54:39 +08:00
const { data: profiles = {} } = useSWR("getProfiles", getProfiles);
2022-03-22 12:38:59 +08:00
// distinguish type
2022-03-05 22:54:39 +08:00
const { regularItems, enhanceItems } = useMemo(() => {
2022-03-06 14:59:25 +08:00
const items = profiles.items || [];
const chain = profiles.chain || [];
const type1 = ["local", "remote"];
const type2 = ["merge", "script"];
const regularItems = items.filter((i) => type1.includes(i.type!));
const restItems = items.filter((i) => type2.includes(i.type!));
const restMap = Object.fromEntries(restItems.map((i) => [i.uid, i]));
const enhanceItems = chain
.map((i) => restMap[i]!)
.concat(restItems.filter((i) => !chain.includes(i.uid)));
2022-03-05 22:54:39 +08:00
return { regularItems, enhanceItems };
}, [profiles]);
2022-03-22 12:38:59 +08:00
// sync selected proxy
2021-12-28 01:47:43 +08:00
useEffect(() => {
if (profiles.current == null) return;
2022-01-08 16:52:18 +08:00
const current = profiles.current;
2022-03-05 22:54:39 +08:00
const profile = regularItems.find((p) => p.uid === 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
let hasChange = false;
2022-03-22 12:38:59 +08:00
const { global, groups } = proxiesData;
[global, ...groups].forEach((group) => {
2021-12-28 01:47:43 +08:00
const { name, now } = group;
if (!now || selectedMap[name] === now) return;
if (selectedMap[name] == null) {
selectedMap[name] = now!;
} else {
hasChange = true;
updateProxy(name, selectedMap[name]);
}
});
2022-03-22 12:38:59 +08:00
2021-12-28 01:47:43 +08:00
// update profile selected list
profile.selected = Object.entries(selectedMap).map(([name, now]) => ({
name,
now,
}));
2022-03-07 01:41:42 +08:00
2022-03-22 12:38:59 +08:00
patchProfile(current!, { selected: profile.selected });
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);
2022-03-05 22:54:39 +08:00
}, [profiles, regularItems]);
2021-12-28 01:47:43 +08:00
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);
2022-01-16 17:56:43 +08:00
Notice.success("Successfully import profile.");
2022-03-01 08:58:47 +08:00
getProfiles().then((newProfiles) => {
mutate("getProfiles", newProfiles);
if (!newProfiles.current && newProfiles.items?.length) {
const current = newProfiles.items[0].uid;
selectProfile(current);
mutate("getProfiles", { ...newProfiles, current }, true);
}
});
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
};
2022-03-05 22:54:39 +08:00
const onSelect = useLockFn(async (uid: string, force: boolean) => {
if (!force && uid === profiles.current) return;
2022-02-26 17:39:36 +08:00
2022-01-17 02:28:23 +08:00
try {
2022-03-05 22:54:39 +08:00
await selectProfile(uid);
mutate("getProfiles", { ...profiles, current: uid }, true);
2022-01-17 02:28:23 +08:00
} catch (err: any) {
2022-03-05 22:54:39 +08:00
Notice.error(err?.message || err.toString());
2022-02-07 17:26:05 +08:00
}
2022-02-26 17:39:36 +08:00
});
2022-02-07 17:26:05 +08:00
2021-12-12 01:09:51 +08:00
return (
2022-03-12 23:07:45 +08:00
<BasePage title={t("Profiles")}>
2022-03-05 22:54:39 +08:00
<Box sx={{ display: "flex", mb: 2.5 }}>
2021-12-16 00:08:28 +08:00
<TextField
2022-03-05 22:54:39 +08:00
id="clas_verge_profile_url"
2021-12-17 00:44:51 +08:00
name="profile_url"
2022-03-12 23:07:45 +08:00
label={t("Profile URL")}
2021-12-16 00:08:28 +08:00
size="small"
fullWidth
value={url}
onChange={(e) => setUrl(e.target.value)}
2022-02-07 17:26:05 +08:00
sx={{ mr: 1 }}
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}
2022-02-07 17:26:05 +08:00
sx={{ mr: 1 }}
2021-12-17 00:44:51 +08:00
>
2022-03-12 23:07:45 +08:00
{t("Import")}
2021-12-16 00:08:28 +08:00
</Button>
<Button variant="contained" onClick={() => setDialogOpen(true)}>
2022-03-12 23:07:45 +08:00
{t("New")}
2022-02-07 17:26:05 +08:00
</Button>
2021-12-16 00:08:28 +08:00
</Box>
2021-12-15 01:54:06 +08:00
2022-03-05 22:54:39 +08:00
<Grid container spacing={2}>
{regularItems.map((item) => (
2021-12-17 00:44:51 +08:00
<Grid item xs={12} sm={6} key={item.file}>
2022-01-16 18:30:25 +08:00
<ProfileItem
2022-03-01 08:58:47 +08:00
selected={profiles.current === item.uid}
2021-12-17 00:44:51 +08:00
itemData={item}
2022-03-01 08:58:47 +08:00
onSelect={(f) => onSelect(item.uid, f)}
2021-12-17 00:44:51 +08:00
/>
</Grid>
))}
</Grid>
2022-04-03 01:41:48 +08:00
{enhanceItems.length && (
<EnhancedMode items={enhanceItems} chain={profiles.chain || []} />
)}
2022-03-05 19:04:20 +08:00
2022-03-05 22:54:39 +08:00
<ProfileNew open={dialogOpen} onClose={() => setDialogOpen(false)} />
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;