refactor: the logic of profiles activation

This commit is contained in:
huzibaca 2024-10-24 05:02:47 +08:00
parent 97f434ad4a
commit a979638368

View File

@ -66,7 +66,7 @@ const ProfilePage = () => {
coordinateGetter: sortableKeyboardCoordinates, coordinateGetter: sortableKeyboardCoordinates,
}) })
); );
const { current } = location.state || {}; const { stateCurrent } = location.state || {};
useEffect(() => { useEffect(() => {
const unlisten = addListener("tauri://file-drop", async (event) => { const unlisten = addListener("tauri://file-drop", async (event) => {
@ -135,23 +135,8 @@ const ProfilePage = () => {
Notice.success(t("Profile Imported Successfully")); Notice.success(t("Profile Imported Successfully"));
setUrl(""); setUrl("");
setLoading(false); setLoading(false);
mutateProfiles();
getProfiles().then(async (newProfiles) => { await onEnhance(false);
mutate("getProfiles", newProfiles);
const remoteItem = newProfiles.items?.find((e) => e.type === "remote");
const itemsCount = newProfiles.items?.filter(
(e) => e.type === "remote" || e.type === "local"
).length as number;
if (remoteItem && (itemsCount == 1 || !newProfiles.current)) {
const current = remoteItem.uid;
await patchProfiles({ current });
mutateLogs();
setTimeout(() => activateSelected(), 2000);
}
});
} catch (err: any) { } catch (err: any) {
Notice.error(err.message || err.toString()); Notice.error(err.message || err.toString());
setLoading(false); setLoading(false);
@ -171,9 +156,8 @@ const ProfilePage = () => {
} }
}; };
const activateProfile = async (profile: string) => { const activateProfile = async (profile: string, notifySuccess: boolean) => {
// 避免大多数情况下loading态闪烁 // 避免大多数情况下loading态闪烁
const reset = setTimeout(() => { const reset = setTimeout(() => {
setActivatings((prev) => [...prev, profile]); setActivatings((prev) => [...prev, profile]);
}, 100); }, 100);
@ -183,7 +167,9 @@ const ProfilePage = () => {
await mutateLogs(); await mutateLogs();
closeAllConnections(); closeAllConnections();
await activateSelected(); await activateSelected();
Notice.success(t("Profile Switched"), 1000); if (notifySuccess) {
Notice.success(t("Profile Switched"), 1000);
}
} catch (err: any) { } catch (err: any) {
Notice.error(err?.message || err.toString(), 4000); Notice.error(err?.message || err.toString(), 4000);
} finally { } finally {
@ -193,24 +179,25 @@ const ProfilePage = () => {
}; };
const onSelect = useLockFn(async (current: string, force: boolean) => { const onSelect = useLockFn(async (current: string, force: boolean) => {
if (!force && current === profiles.current) return; if (!force && current === profiles.current) return;
await activateProfile(current); await activateProfile(current, true);
}); });
useEffect(() => { useEffect(() => {
(async () => { (async () => {
if (current && current !== profiles.current) { if (stateCurrent && stateCurrent !== profiles.current) {
console.log("current:", current); await activateProfile(stateCurrent, false);
await activateProfile(current);
} }
})(); })();
}, current); }, stateCurrent);
const onEnhance = useLockFn(async () => { const onEnhance = useLockFn(async (notifySuccess: boolean) => {
setActivatings(currentActivatings()); setActivatings(currentActivatings());
try { try {
await enhanceProfiles(); await enhanceProfiles();
mutateLogs(); mutateLogs();
Notice.success(t("Profile Reactivated"), 1000); if (notifySuccess) {
Notice.success(t("Profile Reactivated"), 1000);
}
} catch (err: any) { } catch (err: any) {
Notice.error(err.message || err.toString(), 3000); Notice.error(err.message || err.toString(), 3000);
} finally { } finally {
@ -225,7 +212,7 @@ const ProfilePage = () => {
await deleteProfile(uid); await deleteProfile(uid);
mutateProfiles(); mutateProfiles();
mutateLogs(); mutateLogs();
current && (await onEnhance()); current && (await onEnhance(false));
} catch (err: any) { } catch (err: any) {
Notice.error(err?.message || err.toString()); Notice.error(err?.message || err.toString());
} finally { } finally {
@ -302,7 +289,7 @@ const ProfilePage = () => {
size="small" size="small"
color="primary" color="primary"
title={t("Reactivate Profiles")} title={t("Reactivate Profiles")}
onClick={onEnhance} onClick={() => onEnhance(true)}
> >
<LocalFireDepartmentRounded /> <LocalFireDepartmentRounded />
</IconButton> </IconButton>
@ -401,7 +388,7 @@ const ProfilePage = () => {
onEdit={() => viewerRef.current?.edit(item)} onEdit={() => viewerRef.current?.edit(item)}
onSave={async (prev, curr) => { onSave={async (prev, curr) => {
if (prev !== curr && profiles.current === item.uid) { if (prev !== curr && profiles.current === item.uid) {
await onEnhance(); await onEnhance(false);
} }
}} }}
onDelete={() => onDelete(item.uid)} onDelete={() => onDelete(item.uid)}
@ -424,7 +411,7 @@ const ProfilePage = () => {
id="Merge" id="Merge"
onSave={async (prev, curr) => { onSave={async (prev, curr) => {
if (prev !== curr) { if (prev !== curr) {
await onEnhance(); await onEnhance(false);
} }
}} }}
/> />
@ -435,7 +422,7 @@ const ProfilePage = () => {
logInfo={chainLogs["Script"]} logInfo={chainLogs["Script"]}
onSave={async (prev, curr) => { onSave={async (prev, curr) => {
if (prev !== curr) { if (prev !== curr) {
await onEnhance(); await onEnhance(false);
} }
}} }}
/> />
@ -444,7 +431,13 @@ const ProfilePage = () => {
</Box> </Box>
</Box> </Box>
<ProfileViewer ref={viewerRef} onChange={() => mutateProfiles()} /> <ProfileViewer
ref={viewerRef}
onChange={async () => {
mutateProfiles();
await onEnhance(false);
}}
/>
<ConfigViewer ref={configRef} /> <ConfigViewer ref={configRef} />
</BasePage> </BasePage>
); );