From b09b7b11a187a23b9d19f47d1a4dc9ec68bc14dc Mon Sep 17 00:00:00 2001
From: GyDi <segydi@foxmail.com>
Date: Tue, 22 Mar 2022 12:38:59 +0800
Subject: [PATCH] feat: save global selected

---
 src/components/proxy/proxy-global.tsx | 25 ++++++++++++++++++++++---
 src/pages/profiles.tsx                | 12 +++++++-----
 2 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/src/components/proxy/proxy-global.tsx b/src/components/proxy/proxy-global.tsx
index ac64a77..b2b4b1c 100644
--- a/src/components/proxy/proxy-global.tsx
+++ b/src/components/proxy/proxy-global.tsx
@@ -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 { Box, IconButton, TextField } from "@mui/material";
@@ -13,6 +13,7 @@ import {
 } from "@mui/icons-material";
 import { ApiType } from "../../services/types";
 import { updateProxy } from "../../services/api";
+import { getProfiles, patchProfile } from "../../services/cmds";
 import delayManager from "../../services/delay";
 import useFilterProxy from "./use-filter-proxy";
 import ProxyItem from "./proxy-item";
@@ -23,6 +24,7 @@ interface Props {
   proxies: ApiType.ProxyItem[];
 }
 
+// this component will be used for DIRECT/GLOBAL
 const ProxyGlobal = (props: Props) => {
   const { groupName, curProxy, proxies } = props;
 
@@ -35,10 +37,27 @@ const ProxyGlobal = (props: Props) => {
   const virtuosoRef = useRef<any>();
   const filterProxies = useFilterProxy(proxies, groupName, filterText);
 
+  const { data: profiles } = useSWR("getProfiles", getProfiles);
+
   const onChangeProxy = useLockFn(async (name: string) => {
-    await updateProxy("GLOBAL", name);
-    mutate("getProxies");
+    await updateProxy(groupName, name);
     setNow(name);
+
+    if (groupName === "DIRECT") return;
+
+    // update global selected
+    const profile = profiles?.items?.find((p) => p.uid === profiles.current);
+    if (!profile) return;
+    if (!profile.selected) profile.selected = [];
+
+    const index = profile.selected.findIndex((item) => item.name === groupName);
+    if (index < 0) {
+      profile.selected.unshift({ name: groupName, now: name });
+    } else {
+      profile.selected[index] = { name: groupName, now: name };
+    }
+
+    await patchProfile(profiles!.current!, { selected: profile.selected });
   });
 
   const onLocation = (smooth = true) => {
diff --git a/src/pages/profiles.tsx b/src/pages/profiles.tsx
index 5cd331d..d5b6335 100644
--- a/src/pages/profiles.tsx
+++ b/src/pages/profiles.tsx
@@ -29,6 +29,7 @@ const ProfilePage = () => {
 
   const { data: profiles = {} } = useSWR("getProfiles", getProfiles);
 
+  // distinguish type
   const { regularItems, enhanceItems } = useMemo(() => {
     const items = profiles.items || [];
     const chain = profiles.chain || [];
@@ -48,6 +49,7 @@ const ProfilePage = () => {
     return { regularItems, enhanceItems };
   }, [profiles]);
 
+  // sync selected proxy
   useEffect(() => {
     if (profiles.current == null) return;
 
@@ -65,9 +67,10 @@ const ProfilePage = () => {
         selected.map((each) => [each.name!, each.now!])
       );
 
-      // todo: enhance error handle
       let hasChange = false;
-      proxiesData.groups.forEach((group) => {
+
+      const { global, groups } = proxiesData;
+      [global, ...groups].forEach((group) => {
         const { name, now } = group;
 
         if (!now || selectedMap[name] === now) return;
@@ -78,15 +81,14 @@ const ProfilePage = () => {
           updateProxy(name, selectedMap[name]);
         }
       });
+
       // update profile selected list
       profile.selected = Object.entries(selectedMap).map(([name, now]) => ({
         name,
         now,
       }));
 
-      patchProfile(current!, { selected: profile.selected }).catch(
-        console.error
-      );
+      patchProfile(current!, { selected: profile.selected });
       // update proxies cache
       if (hasChange) mutate("getProxies", getProxies());
     }, 100);
-- 
GitLab