mirror of
https://github.com/priyanshujain/margin.git
synced 2026-10-02 11:07:04 +00:00
send app store builds to the store for updates
This commit is contained in:
1 parent
e8772e6699
commit
e8555c7ef9
10 files changed
+199
-54
No files matched your search
+67
-14
@@ -1,8 +1,38 @@
|
||||
import { check } from "@tauri-apps/plugin-updater";
|
||||
import { relaunch } from "@tauri-apps/plugin-process";
|
||||
import { ask } from "@tauri-apps/plugin-dialog";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { getVersion } from "@tauri-apps/api/app";
|
||||
import { useUpdater } from "./store/useUpdater";
|
||||
|
||||
const DECLINED = "margin.update.declined";
|
||||
|
||||
type Channel = "direct" | "appstore" | "none";
|
||||
|
||||
let checking = false;
|
||||
let channel: Channel | null = null;
|
||||
|
||||
async function updateChannel() {
|
||||
if (!channel) channel = await invoke<Channel>("update_channel");
|
||||
return channel;
|
||||
}
|
||||
|
||||
function declined(version: string) {
|
||||
return localStorage.getItem(DECLINED) === version;
|
||||
}
|
||||
|
||||
function decline(version: string) {
|
||||
localStorage.setItem(DECLINED, version);
|
||||
}
|
||||
|
||||
function offer(version: string, current: string, okLabel: string) {
|
||||
return ask(`margin ${version} is available. You have ${current}.`, {
|
||||
title: "A new version of margin is available",
|
||||
kind: "info",
|
||||
okLabel,
|
||||
cancelLabel: "Later",
|
||||
});
|
||||
}
|
||||
|
||||
export async function checkForUpdates(silent: boolean) {
|
||||
if (checking) return;
|
||||
@@ -10,20 +40,10 @@ export async function checkForUpdates(silent: boolean) {
|
||||
const store = useUpdater.getState();
|
||||
if (!silent) store.set({ phase: "checking", error: "" });
|
||||
try {
|
||||
const update = await check();
|
||||
if (!update) {
|
||||
store.set(silent ? { phase: "idle" } : { phase: "uptodate" });
|
||||
return;
|
||||
}
|
||||
store.set({
|
||||
phase: "available",
|
||||
update,
|
||||
version: update.version,
|
||||
notes: update.body ?? "",
|
||||
downloaded: 0,
|
||||
total: 0,
|
||||
error: "",
|
||||
});
|
||||
const target = await updateChannel();
|
||||
if (target === "direct") await checkDirect(silent);
|
||||
else if (target === "appstore") await checkAppStore(silent);
|
||||
else store.set(silent ? { phase: "idle" } : { phase: "uptodate" });
|
||||
} catch (err) {
|
||||
store.set(silent ? { phase: "idle" } : { phase: "error", error: String(err) });
|
||||
} finally {
|
||||
@@ -31,6 +51,39 @@ export async function checkForUpdates(silent: boolean) {
|
||||
}
|
||||
}
|
||||
|
||||
async function checkDirect(silent: boolean) {
|
||||
const store = useUpdater.getState();
|
||||
const update = await check();
|
||||
if (!update) {
|
||||
store.set(silent ? { phase: "idle" } : { phase: "uptodate" });
|
||||
return;
|
||||
}
|
||||
if (silent && declined(update.version)) {
|
||||
store.set({ phase: "idle" });
|
||||
return;
|
||||
}
|
||||
store.set({ phase: "idle", update });
|
||||
if (await offer(update.version, update.currentVersion, "Install Update")) await installUpdate();
|
||||
else decline(update.version);
|
||||
}
|
||||
|
||||
async function checkAppStore(silent: boolean) {
|
||||
const store = useUpdater.getState();
|
||||
const release = await invoke<{ version: string; trackId: number } | null>("appstore_latest");
|
||||
if (!release) {
|
||||
store.set(silent ? { phase: "idle" } : { phase: "uptodate" });
|
||||
return;
|
||||
}
|
||||
if (silent && declined(release.version)) {
|
||||
store.set({ phase: "idle" });
|
||||
return;
|
||||
}
|
||||
store.set({ phase: "idle" });
|
||||
if (await offer(release.version, await getVersion(), "Open App Store"))
|
||||
await invoke("open_appstore", { trackId: release.trackId });
|
||||
else decline(release.version);
|
||||
}
|
||||
|
||||
export async function installUpdate() {
|
||||
const { update, phase } = useUpdater.getState();
|
||||
if (!update || phase === "downloading" || phase === "installing") return;
|
||||
|
||||
Reference in new issue
Block a user