feat: record selected proxy

This commit is contained in:
GyDi
2021-12-28 01:47:43 +08:00
parent 25978bde8e
commit 96eca59afc
4 changed files with 69 additions and 11 deletions

View File

@@ -1,8 +1,13 @@
import { useRef, useState } from "react";
import { useEffect, useRef, useState } from "react";
import useSWR, { useSWRConfig } from "swr";
import { Box, Button, Grid, TextField, Typography } from "@mui/material";
import { getProfiles, importProfile, putProfiles } from "../services/cmds";
import { getProxies } from "../services/api";
import {
getProfiles,
putProfiles,
setProfiles,
importProfile,
} from "../services/cmds";
import { getProxies, updateProxy } from "../services/api";
import ProfileItemComp from "../components/profile-item";
import useNotice from "../utils/use-notice";
import noop from "../utils/noop";
@@ -15,6 +20,43 @@ const ProfilePage = () => {
const { mutate } = useSWRConfig();
const { data: profiles = {} } = useSWR("getProfiles", getProfiles);
useEffect(() => {
if (profiles.current == null) return;
const profile = profiles.items![profiles.current];
if (!profile) return;
getProxies().then((proxiesData) => {
mutate("getProxies", proxiesData);
// init selected array
const { selected = [] } = profile;
const selectedMap = Object.fromEntries(
selected.map((each) => [each.name!, each.now!])
);
// 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,
}));
setProfiles(profiles.current!, profile).catch(console.error);
// update proxies cache
if (hasChange) mutate("getProxies", getProxies());
});
}, [profiles]);
const onImport = async () => {
if (!url) return;
setUrl("");
@@ -39,7 +81,6 @@ const ProfilePage = () => {
putProfiles(index)
.then(() => {
mutate("getProfiles", { ...profiles, current: index }, true);
mutate("getProxies", getProxies());
})
.catch((err) => {
console.error(err);

View File

@@ -4,8 +4,8 @@ import { getProxies } from "../services/api";
import ProxyGroup from "../components/proxy-group";
const ProxyPage = () => {
const { data } = useSWR("getProxies", getProxies);
const { groups = [] } = data ?? {};
const { data: proxiesData } = useSWR("getProxies", getProxies);
const { groups = [] } = proxiesData ?? {};
return (
<Box sx={{ width: 0.9, maxWidth: "850px", mx: "auto", mb: 2 }}>