From 5eddf4f1aa0b13383654726a66fbf88fbf6b12d2 Mon Sep 17 00:00:00 2001
From: GyDi <segydi@foxmail.com>
Date: Mon, 17 Jan 2022 02:28:23 +0800
Subject: [PATCH] feat: enable force select profile

---
 src/components/profile-item.tsx | 14 ++++++++++----
 src/pages/profiles.tsx          | 24 +++++++++++-------------
 2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/src/components/profile-item.tsx b/src/components/profile-item.tsx
index 7161ae7..6096410 100644
--- a/src/components/profile-item.tsx
+++ b/src/components/profile-item.tsx
@@ -41,11 +41,11 @@ interface Props {
   index: number;
   selected: boolean;
   itemData: CmdType.ProfileItem;
-  onClick: () => void;
+  onSelect: (force: boolean) => void;
 }
 
 const ProfileItem: React.FC<Props> = (props) => {
-  const { index, selected, itemData, onClick } = props;
+  const { index, selected, itemData, onSelect } = props;
 
   const { mutate } = useSWRConfig();
   const [loading, setLoading] = useState(false);
@@ -68,6 +68,11 @@ const ProfileItem: React.FC<Props> = (props) => {
     }
   };
 
+  const onForceSelect = () => {
+    setAnchorEl(null);
+    onSelect(true);
+  };
+
   const onUpdateWrapper = (withProxy: boolean) => async () => {
     setAnchorEl(null);
     if (loading) return;
@@ -136,7 +141,7 @@ const ProfileItem: React.FC<Props> = (props) => {
 
           return { bgcolor, color, "& h2": { color: h2color } };
         }}
-        onClick={onClick}
+        onClick={() => onSelect(false)}
         onContextMenu={handleContextMenu}
       >
         <Box display="flex" justifyContent="space-between">
@@ -211,7 +216,8 @@ const ProfileItem: React.FC<Props> = (props) => {
         anchorPosition={position}
         anchorReference="anchorPosition"
       >
-        <MenuItem onClick={onEdit}>Edit</MenuItem>
+        <MenuItem onClick={onForceSelect}>Select</MenuItem>
+        <MenuItem onClick={onEdit}>Edit(VScode)</MenuItem>
         <MenuItem onClick={onUpdateWrapper(false)}>Update</MenuItem>
         <MenuItem onClick={onUpdateWrapper(true)}>Update(Proxy)</MenuItem>
         <MenuItem onClick={onDelete}>Delete</MenuItem>
diff --git a/src/pages/profiles.tsx b/src/pages/profiles.tsx
index bef8314..75a2639 100644
--- a/src/pages/profiles.tsx
+++ b/src/pages/profiles.tsx
@@ -80,20 +80,18 @@ const ProfilePage = () => {
   };
 
   const lockRef = useRef(false);
-  const onProfileChange = (index: number) => {
-    if (index === profiles.current || lockRef.current) return;
+  const onSelect = async (index: number, force: boolean) => {
     if (lockRef.current) return;
+    if (!force && index === profiles.current) return;
     lockRef.current = true;
-    selectProfile(index)
-      .then(() => {
-        mutate("getProfiles", { ...profiles, current: index }, true);
-      })
-      .catch((err) => {
-        console.error(err);
-      })
-      .finally(() => {
-        lockRef.current = false;
-      });
+    try {
+      await selectProfile(index);
+      mutate("getProfiles", { ...profiles, current: index }, true);
+    } catch (err: any) {
+      err && Notice.error(err.toString());
+    } finally {
+      lockRef.current = false;
+    }
   };
 
   return (
@@ -125,7 +123,7 @@ const ProfilePage = () => {
               index={idx}
               selected={profiles.current === idx}
               itemData={item}
-              onClick={() => onProfileChange(idx)}
+              onSelect={(f) => onSelect(idx, f)}
             />
           </Grid>
         ))}
-- 
GitLab