refactor: profile config

This commit is contained in:
GyDi
2022-03-01 08:58:47 +08:00
parent 444f2172fa
commit 749df89229
15 changed files with 567 additions and 759 deletions

View File

@@ -39,14 +39,14 @@ const round = keyframes`
`;
interface Props {
index: number;
// index: number;
selected: boolean;
itemData: CmdType.ProfileItem;
onSelect: (force: boolean) => void;
}
const ProfileItem: React.FC<Props> = (props) => {
const { index, selected, itemData, onSelect } = props;
const { selected, itemData, onSelect } = props;
const { mutate } = useSWRConfig();
const [loading, setLoading] = useState(false);
@@ -69,7 +69,7 @@ const ProfileItem: React.FC<Props> = (props) => {
const onView = async () => {
setAnchorEl(null);
try {
await viewProfile(index);
await viewProfile(itemData.uid);
} catch (err: any) {
Notice.error(err.toString());
}
@@ -85,7 +85,7 @@ const ProfileItem: React.FC<Props> = (props) => {
if (loading) return;
setLoading(true);
try {
await updateProfile(index, withProxy);
await updateProfile(itemData.uid, withProxy);
mutate("getProfiles");
} catch (err: any) {
Notice.error(err.toString());
@@ -98,7 +98,7 @@ const ProfileItem: React.FC<Props> = (props) => {
setAnchorEl(null);
try {
await deleteProfile(index);
await deleteProfile(itemData.uid);
mutate("getProfiles");
} catch (err: any) {
Notice.error(err.toString());

View File

@@ -1,5 +1,5 @@
import useSWR, { useSWRConfig } from "swr";
import { useEffect, useRef, useState } from "react";
import { useSWRConfig } from "swr";
import { useLockFn } from "ahooks";
import { Virtuoso } from "react-virtuoso";
import {
@@ -46,6 +46,8 @@ const ProxyGroup = ({ group }: Props) => {
const virtuosoRef = useRef<any>();
const filterProxies = useFilterProxy(proxies, group.name, filterText);
const { data: profiles } = useSWR("getProfiles", getProfiles);
const onChangeProxy = useLockFn(async (name: string) => {
// Todo: support another proxy group type
if (group.type !== "Selector") return;
@@ -60,8 +62,7 @@ const ProxyGroup = ({ group }: Props) => {
}
try {
const profiles = await getProfiles();
const profile = profiles.items![profiles.current!]!;
const profile = profiles?.items?.find((p) => p.uid === profiles.current);
if (!profile) return;
if (!profile.selected) profile.selected = [];
@@ -74,7 +75,7 @@ const ProxyGroup = ({ group }: Props) => {
} else {
profile.selected[index] = { name: group.name, now: name };
}
await patchProfile(profiles.current!, profile);
await patchProfile(profiles!.current!, profile);
} catch (err) {
console.error(err);
}

View File

@@ -10,7 +10,6 @@ import {
newProfile,
} from "../services/cmds";
import { getProxies, updateProxy } from "../services/api";
import noop from "../utils/noop";
import Notice from "../components/base/base-notice";
import BasePage from "../components/base/base-page";
import ProfileItem from "../components/profile/profile-item";
@@ -28,7 +27,7 @@ const ProfilePage = () => {
if (!profiles.items) profiles.items = [];
const current = profiles.current;
const profile = profiles.items![current];
const profile = profiles.items.find((p) => p.uid === current);
if (!profile) return;
setTimeout(async () => {
@@ -72,9 +71,17 @@ const ProfilePage = () => {
try {
await importProfile(url);
mutate("getProfiles", getProfiles());
if (!profiles.items?.length) selectProfile(0).catch(noop);
Notice.success("Successfully import profile.");
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);
}
});
} catch {
Notice.error("Failed to import profile.");
} finally {
@@ -82,12 +89,12 @@ const ProfilePage = () => {
}
};
const onSelect = useLockFn(async (index: number, force: boolean) => {
if (!force && index === profiles.current) return;
const onSelect = useLockFn(async (current: string, force: boolean) => {
if (!force && current === profiles.current) return;
try {
await selectProfile(index);
mutate("getProfiles", { ...profiles, current: index }, true);
await selectProfile(current);
mutate("getProfiles", { ...profiles, current: current }, true);
} catch (err: any) {
err && Notice.error(err.toString());
}
@@ -131,13 +138,12 @@ const ProfilePage = () => {
</Box>
<Grid container spacing={3}>
{profiles?.items?.map((item, idx) => (
{profiles?.items?.map((item) => (
<Grid item xs={12} sm={6} key={item.file}>
<ProfileItem
index={idx}
selected={profiles.current === idx}
selected={profiles.current === item.uid}
itemData={item}
onSelect={(f) => onSelect(idx, f)}
onSelect={(f) => onSelect(item.uid, f)}
/>
</Grid>
))}

View File

@@ -13,7 +13,7 @@ export async function newProfile(name: string, desc: string) {
return invoke<void>("new_profile", { name, desc });
}
export async function viewProfile(index: number) {
export async function viewProfile(index: string) {
return invoke<void>("view_profile", { index });
}
@@ -21,22 +21,22 @@ export async function importProfile(url: string) {
return invoke<void>("import_profile", { url, withProxy: true });
}
export async function updateProfile(index: number, withProxy: boolean) {
export async function updateProfile(index: string, withProxy: boolean) {
return invoke<void>("update_profile", { index, withProxy });
}
export async function deleteProfile(index: number) {
export async function deleteProfile(index: string) {
return invoke<void>("delete_profile", { index });
}
export async function patchProfile(
index: number,
index: string,
profile: CmdType.ProfileItem
) {
return invoke<void>("patch_profile", { index, profile });
}
export async function selectProfile(index: number) {
export async function selectProfile(index: string) {
return invoke<void>("select_profile", { index });
}

View File

@@ -86,6 +86,8 @@ export namespace CmdType {
}
export interface ProfileItem {
uid: string;
type?: string;
name?: string;
desc?: string;
file?: string;
@@ -105,7 +107,8 @@ export namespace CmdType {
}
export interface ProfilesConfig {
current?: number;
current?: string;
chain?: string[];
items?: ProfileItem[];
}