update download + apply flow
This commit is contained in:
+96
-15
@@ -1,25 +1,69 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from "svelte"
|
||||
import { api, type UpdateInfo } from "./api"
|
||||
import { api, formatBytes, type UpdateInfo, type UpdateDownloadProgress } from "./api"
|
||||
|
||||
type Phase = "info" | "downloading" | "ready-to-apply" | "error"
|
||||
|
||||
let info: UpdateInfo | null = $state(null)
|
||||
let phase: Phase = $state("info")
|
||||
let progress: UpdateDownloadProgress | null = $state(null)
|
||||
let errMsg: string | null = $state(null)
|
||||
|
||||
onMount(() => {
|
||||
const unlistenPromise = api.onUpdateAvailable((i) => {
|
||||
info = i
|
||||
})
|
||||
let unlistenInfo: (() => void) | null = null
|
||||
let unlistenProgress: (() => void) | null = null
|
||||
void (async () => {
|
||||
unlistenInfo = await api.onUpdateAvailable((i) => {
|
||||
info = i
|
||||
phase = "info"
|
||||
})
|
||||
unlistenProgress = await api.onUpdateDownloadProgress((p) => {
|
||||
progress = p
|
||||
})
|
||||
if (await api.pendingUpdate()) {
|
||||
phase = "ready-to-apply"
|
||||
}
|
||||
})()
|
||||
return () => {
|
||||
unlistenPromise.then((fn) => fn())
|
||||
unlistenInfo?.()
|
||||
unlistenProgress?.()
|
||||
}
|
||||
})
|
||||
|
||||
function dismiss() {
|
||||
info = null
|
||||
phase = "info"
|
||||
errMsg = null
|
||||
}
|
||||
|
||||
function viewRelease() {
|
||||
if (info) {
|
||||
void api.openUrl(info.release_url)
|
||||
if (info) void api.openUrl(info.release_url)
|
||||
}
|
||||
|
||||
async function download() {
|
||||
if (!info?.download_url) {
|
||||
errMsg = "this release has no downloadable exe asset"
|
||||
phase = "error"
|
||||
return
|
||||
}
|
||||
phase = "downloading"
|
||||
progress = null
|
||||
errMsg = null
|
||||
try {
|
||||
await api.downloadUpdate(info.download_url)
|
||||
phase = "ready-to-apply"
|
||||
} catch (e) {
|
||||
errMsg = String(e)
|
||||
phase = "error"
|
||||
}
|
||||
}
|
||||
|
||||
async function apply() {
|
||||
try {
|
||||
await api.applyUpdate()
|
||||
} catch (e) {
|
||||
errMsg = String(e)
|
||||
phase = "error"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,19 +74,51 @@
|
||||
})
|
||||
</script>
|
||||
|
||||
{#if info}
|
||||
{#if info || phase === "ready-to-apply"}
|
||||
<div class="banner">
|
||||
<div class="text">
|
||||
<strong>update available</strong>
|
||||
<span class="ver">v{info.version}</span>
|
||||
<span class="from">(you have v{info.current_version})</span>
|
||||
{#if bodyPreview}
|
||||
<p class="body">{bodyPreview}{info.body.length > 140 ? "..." : ""}</p>
|
||||
{#if phase === "ready-to-apply"}
|
||||
<strong>update ready to apply</strong>
|
||||
{#if info}
|
||||
<span class="ver">v{info.version}</span>
|
||||
{/if}
|
||||
<p class="body">click apply to swap and restart, or dismiss to keep the current version.</p>
|
||||
{:else if phase === "downloading"}
|
||||
<strong>downloading update...</strong>
|
||||
{#if info}<span class="ver">v{info.version}</span>{/if}
|
||||
{#if progress}
|
||||
<p class="body">
|
||||
{formatBytes(progress.downloaded)}
|
||||
{#if progress.total} / {formatBytes(progress.total)}{/if}
|
||||
</p>
|
||||
{/if}
|
||||
{:else if phase === "error"}
|
||||
<strong>update error</strong>
|
||||
<p class="body">{errMsg}</p>
|
||||
{:else if info}
|
||||
<strong>update available</strong>
|
||||
<span class="ver">v{info.version}</span>
|
||||
<span class="from">(you have v{info.current_version})</span>
|
||||
{#if bodyPreview}
|
||||
<p class="body">{bodyPreview}{info.body.length > 140 ? "..." : ""}</p>
|
||||
{/if}
|
||||
{/if}
|
||||
</div>
|
||||
<div class="actions">
|
||||
<button class="primary" onclick={viewRelease}>view release</button>
|
||||
<button onclick={dismiss}>dismiss</button>
|
||||
{#if phase === "ready-to-apply"}
|
||||
<button class="primary" onclick={apply}>apply now</button>
|
||||
<button onclick={dismiss}>dismiss</button>
|
||||
{:else if phase === "downloading"}
|
||||
<span class="muted">working...</span>
|
||||
{:else if phase === "error"}
|
||||
<button onclick={dismiss}>dismiss</button>
|
||||
{:else if info}
|
||||
{#if info.download_url}
|
||||
<button class="primary" onclick={download}>download</button>
|
||||
{/if}
|
||||
<button onclick={viewRelease}>view release</button>
|
||||
<button onclick={dismiss}>dismiss</button>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
@@ -83,6 +159,11 @@
|
||||
display: flex;
|
||||
gap: 0.4rem;
|
||||
flex-shrink: 0;
|
||||
align-items: center;
|
||||
}
|
||||
.muted {
|
||||
color: #7a8e88;
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
button {
|
||||
font: inherit;
|
||||
|
||||
Reference in New Issue
Block a user