From f72536bce017532faea8d159a75f2e6f5fc6c655 Mon Sep 17 00:00:00 2001 From: GyDi Date: Tue, 21 Dec 2021 22:48:39 +0800 Subject: [PATCH] feat: auto update profiles --- src/pages/rules.tsx | 20 ++++++++++++++------ src/services/command.ts | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/pages/rules.tsx b/src/pages/rules.tsx index 7e4f250..9c2547f 100644 --- a/src/pages/rules.tsx +++ b/src/pages/rules.tsx @@ -10,6 +10,7 @@ import { } from "../services/command"; import ProfileItemComp from "../components/profile-item"; import useNotice from "../utils/use-notice"; +import noop from "../utils/noop"; const RulesPage = () => { const [url, setUrl] = useState(""); @@ -19,14 +20,21 @@ const RulesPage = () => { const { mutate } = useSWRConfig(); const { data: profiles = {} } = useSWR("getProfiles", getProfiles); - const onClick = () => { + const onImport = async () => { if (!url) return; setUrl(""); setDisabled(true); - importProfile(url) - .then(() => notice.success("Successfully import profile.")) - .catch(() => notice.error("Failed to import profile.")) - .finally(() => setDisabled(false)); + + try { + await importProfile(url); + mutate("getProfiles", getProfiles()); + if (!profiles.items?.length) putProfiles(0).catch(noop); + notice.success("Successfully import profile."); + } catch { + notice.error("Failed to import profile."); + } finally { + setDisabled(false); + } }; const lockRef = useRef(false); @@ -76,7 +84,7 @@ const RulesPage = () => { diff --git a/src/services/command.ts b/src/services/command.ts index a5e9327..d6d3c36 100644 --- a/src/services/command.ts +++ b/src/services/command.ts @@ -48,7 +48,7 @@ export interface ProfilesConfig { } export async function getProfiles() { - return invoke("get_profiles"); + return (await invoke("get_profiles")) ?? {}; } export async function setProfiles(current: number, profile: ProfileItem) {