Skip to content

Commit

Permalink
install latest version by default (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
domechn committed Jul 9, 2023
1 parent f0395c4 commit 4912263
Showing 1 changed file with 26 additions and 58 deletions.
84 changes: 26 additions & 58 deletions src/components/common/auto-updater/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,45 @@ import { useEffect } from "react";
import { checkUpdate, installUpdate } from "@tauri-apps/api/updater";
import { relaunch } from "@tauri-apps/api/process";
import toast from "react-hot-toast";
import loadingIcon from "../../../assets/icons/loading.png";
import "./index.css";

const i32Max = 2147483647;
const toastId = "auto-updater";

const App = () => {
useEffect(() => {
autoCheckUpdater();
autoInstallLatestVersion();
}, []);

// cannot use useState here
let buttonEnabled = true;

function autoCheckUpdater() {
checkUpdate().then((res) => {
if (res.shouldUpdate && res.manifest?.version) {
toast.custom(renderUpdater(res.manifest?.version), {
id: toastId,
duration: i32Max,
position: "bottom-right",
});
}
});
}

function installAndRelaunch() {
if (!buttonEnabled) {
return;
}

buttonEnabled = false;
const buttonLoading = document.getElementById("auto-updater-loading-icon");
buttonLoading!.style.display = "inline-block";
const loadingId = toastId + "-loading";

toast.loading("Downloading update...", {
id: loadingId,
duration: i32Max,
});
installUpdate()
.then(() => {
relaunch();
})
.catch((err) => {
toast.error(err);
function autoInstallLatestVersion() {
checkUpdate()
.then(async (res) => {
if (res.shouldUpdate && res.manifest?.version) {
await installUpdate();
return true
}
return false
})
.finally(() => {
toast.remove(loadingId);
buttonEnabled = true;
.then((installed) => {
if (installed) {
toast.custom(renderUpdater(), {
id: toastId,
duration: i32Max,
position: "bottom-right",
});
}
});
}

function reloadApp() {
return relaunch();
}

function onToastCloseClick() {
toast.remove(toastId);
}

function renderUpdater(version: string) {
function renderUpdater() {
return (
<div
className="auto-updater"
Expand All @@ -71,14 +52,14 @@ const App = () => {
willChange: "transform",
boxShadow:
"0 3px 10px rgba(0, 0, 0, 0.1), 0 3px 3px rgba(0, 0, 0, 0.05)",
width: "350px",
width: "250px",
pointerEvents: "auto",
padding: "8px 25px",
borderRadius: "8px",
}}
>
<div className="auto-updater-row title">
🔥 Update v{version} available
🔥 New version available!
</div>
<a
onClick={onToastCloseClick}
Expand Down Expand Up @@ -107,25 +88,12 @@ const App = () => {
</a>
<button
className="auto-updater-row"
onClick={installAndRelaunch}
onClick={reloadApp}
style={{
textAlign: "center",
}}
>
<img
id="auto-updater-loading-icon"
src={loadingIcon}
style={{
height: "0.8em",
width: "0.8em",
position: "relative",
top: "0.1em",
left: "-0.3em",
animation: "spin 1s linear infinite",
display: "none",
}}
/>
Install update and relaunch
Reload
</button>
</div>
);
Expand Down

0 comments on commit 4912263

Please sign in to comment.