Skip to content

Commit

Permalink
feat: auto update profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi committed Dec 21, 2021
1 parent 81aef73 commit f72536b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/pages/rules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand All @@ -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);
Expand Down Expand Up @@ -76,7 +84,7 @@ const RulesPage = () => {
<Button
disabled={!url || disabled}
variant="contained"
onClick={onClick}
onClick={onImport}
>
Import
</Button>
Expand Down
2 changes: 1 addition & 1 deletion src/services/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface ProfilesConfig {
}

export async function getProfiles() {
return invoke<ProfilesConfig | null>("get_profiles");
return (await invoke<ProfilesConfig>("get_profiles")) ?? {};
}

export async function setProfiles(current: number, profile: ProfileItem) {
Expand Down

0 comments on commit f72536b

Please sign in to comment.