fix(updater): refresh changed update offers before installation

This commit is contained in:
Tunglies
2026-11-03 18:57:05 +08:00
parent 40fd1d5012
commit 2b7b7cc0e7
9 changed files with 87 additions and 148 deletions
+40 -42
View File
@@ -1,14 +1,8 @@
import { alpha, Box, Button, LinearProgress } from '@mui/material'
import type { Update } from '@tauri-apps/plugin-updater'
import { useLockFn } from 'ahooks'
import type { Ref } from 'react'
import {
lazy,
Suspense,
useImperativeHandle,
useMemo,
useRef,
useState,
} from 'react'
import { lazy, Suspense, useImperativeHandle, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import type { Options as ReactMarkdownOptions } from 'react-markdown'
@@ -180,12 +174,16 @@ export function UpdateViewer({ ref }: { ref?: Ref<DialogRef> }) {
const updateState = useUpdateState()
const setUpdateState = useSetUpdateState()
const { updateInfo } = useUpdate()
const { updateInfo: detectedUpdate, checkUpdate } = useUpdate()
const [refreshedUpdate, setRefreshedUpdate] = useState<Update | null>()
const updateInfo =
refreshedUpdate === undefined ? detectedUpdate : refreshedUpdate
const [downloaded, setDownloaded] = useState(0)
const [total, setTotal] = useState(0)
const downloadedRef = useRef(0)
const totalRef = useRef(0)
const [download, setDownload] = useState({
downloaded: 0,
total: 0,
})
const { downloaded, total } = download
const openUrlWithNotice = (url: string) => {
void openExternalUrl(url).catch(showNotice.error)
@@ -197,7 +195,10 @@ export function UpdateViewer({ ref }: { ref?: Ref<DialogRef> }) {
}, [downloaded, total])
useImperativeHandle(ref, () => ({
open: () => setOpen(true),
open: () => {
setRefreshedUpdate(undefined)
setOpen(true)
},
close: () => setOpen(false),
}))
@@ -242,32 +243,21 @@ export function UpdateViewer({ ref }: { ref?: Ref<DialogRef> }) {
}
if (updateState) return
setUpdateState(true)
setDownloaded(0)
setTotal(0)
downloadedRef.current = 0
totalRef.current = 0
setDownload({ downloaded: 0, total: 0 })
const onDownloadEvent = (event: DownloadEvent) => {
if (event.event === 'Started') {
const contentLength = event.data.contentLength ?? 0
totalRef.current = contentLength
setTotal(contentLength)
setDownloaded(0)
downloadedRef.current = 0
return
}
if (event.event === 'Progress') {
setDownloaded((prev) => {
const next = prev + event.data.chunkLength
downloadedRef.current = next
return next
})
}
if (event.event === 'Finished' && totalRef.current === 0) {
totalRef.current = downloadedRef.current
setTotal(downloadedRef.current)
setDownload({ downloaded: 0, total: event.data.contentLength ?? 0 })
} else if (event.event === 'Progress') {
setDownload((prev) => ({
...prev,
downloaded: prev.downloaded + event.data.chunkLength,
}))
} else if (event.event === 'Finished') {
setDownload((prev) => ({
...prev,
total: prev.total || prev.downloaded,
}))
}
}
@@ -279,7 +269,18 @@ export function UpdateViewer({ ref }: { ref?: Ref<DialogRef> }) {
errorNotice: false,
},
)
if (result.ok && result.value) {
if (result.ok && result.value === 'update_changed') {
const { data } = await checkUpdate()
setRefreshedUpdate(data)
if (data) {
showNotice.info('settings.modals.update.messages.available')
} else {
setOpen(false)
showNotice.info(
'settings.components.verge.advanced.notifications.latestVersion',
)
}
} else if (result.ok && result.value === 'installed') {
await mutate(() => restartApp(), {
id: 'restart-app',
errorNotice: false,
@@ -289,10 +290,7 @@ export function UpdateViewer({ ref }: { ref?: Ref<DialogRef> }) {
showNotice.error(err)
} finally {
setUpdateState(false)
setDownloaded(0)
setTotal(0)
downloadedRef.current = 0
totalRef.current = 0
setDownload({ downloaded: 0, total: 0 })
}
})