Skip to content

Commit

Permalink
fix: update state
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi committed Mar 21, 2022
1 parent 9910f6b commit dd605e2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/components/layout/update-dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import useSWR from "swr";
import snarkdown from "snarkdown";
import { useState, useMemo } from "react";
import { useMemo } from "react";
import { useRecoilState } from "recoil";
import {
Box,
Button,
Expand All @@ -13,6 +14,7 @@ import {
import { relaunch } from "@tauri-apps/api/process";
import { checkUpdate, installUpdate } from "@tauri-apps/api/updater";
import { killSidecars, restartSidecar } from "../../services/cmds";
import { atomUpdateState } from "../../services/states";
import Notice from "../base/base-notice";

interface Props {
Expand All @@ -24,31 +26,29 @@ const UpdateLog = styled(Box)(() => ({
"h1,h2,h3,ul,ol,p": { margin: "0.5em 0", color: "inherit" },
}));

let uploadingState = false;

const UpdateDialog = (props: Props) => {
const { open, onClose } = props;
const { data: updateInfo } = useSWR("checkUpdate", checkUpdate, {
errorRetryCount: 2,
revalidateIfStale: false,
focusThrottleInterval: 36e5, // 1 hour
});
const [uploading, setUploading] = useState(uploadingState);

const [updateState, setUpdateState] = useRecoilState(atomUpdateState);

const onUpdate = async () => {
setUploading(true);
uploadingState = true;
if (updateState) return;
setUpdateState(true);

try {
await installUpdate();
await killSidecars();
await installUpdate();
await relaunch();
} catch (err: any) {
await restartSidecar();
Notice.error(err?.message || err.toString());
} finally {
setUploading(false);
uploadingState = false;
setUpdateState(false);
}
};

Expand All @@ -73,7 +73,7 @@ const UpdateDialog = (props: Props) => {
<Button
autoFocus
variant="contained"
disabled={uploading}
disabled={updateState}
onClick={onUpdate}
>
Update
Expand Down
6 changes: 6 additions & 0 deletions src/services/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ export const atomLoadingCache = atom<Record<string, boolean>>({
key: "atomLoadingCache",
default: {},
});

// save update state
export const atomUpdateState = atom<boolean>({
key: "atomUpdateState",
default: false,
});

0 comments on commit dd605e2

Please sign in to comment.