mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-16 16:32:11 +08:00
fix(updater): refresh changed update offers before installation
This commit is contained in:
@@ -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 })
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -24,7 +24,10 @@ export const commands = {
|
||||
getSystemHostname: () => __TAURI_INVOKE<string>('get_system_hostname'),
|
||||
restartApp: () => __TAURI_INVOKE<null>('restart_app'),
|
||||
installUpdate: (version: string, onEvent: Channel<DownloadEvent>) =>
|
||||
__TAURI_INVOKE<boolean>('install_update', { version, onEvent }),
|
||||
__TAURI_INVOKE<InstallUpdateOutcome>('install_update', {
|
||||
version,
|
||||
onEvent,
|
||||
}),
|
||||
cancelUpdateDownload: () => __TAURI_INVOKE<void>('cancel_update_download'),
|
||||
restartCore: () => __TAURI_INVOKE<null>('restart_core'),
|
||||
/** Replaces the managed core binary atomically instead of letting mihomo overwrite itself. */
|
||||
@@ -536,6 +539,8 @@ export type IconInfo = {
|
||||
current_t: string
|
||||
}
|
||||
|
||||
export type InstallUpdateOutcome = 'installed' | 'cancelled' | 'update_changed'
|
||||
|
||||
export type InterfaceAddress =
|
||||
| ({ V4: IpAddress } & { V6?: never })
|
||||
| ({ V6: IpAddress } & { V4?: never })
|
||||
|
||||
+1
-45
@@ -7,11 +7,6 @@ import { compareVersions as compareSemver } from 'compare-versions'
|
||||
|
||||
import { version as appVersion } from '@root/package.json'
|
||||
|
||||
const SEMVER_FULL_REGEX =
|
||||
/^\d+(?:\.\d+){1,2}(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/
|
||||
const SEMVER_SEARCH_REGEX =
|
||||
/v?\d+(?:\.\d+){1,2}(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?/i
|
||||
|
||||
const normalizeVersion = (input: string | null | undefined): string | null => {
|
||||
if (typeof input !== 'string') return null
|
||||
const trimmed = input.trim()
|
||||
@@ -19,19 +14,6 @@ const normalizeVersion = (input: string | null | undefined): string | null => {
|
||||
return trimmed.replace(/^v/i, '')
|
||||
}
|
||||
|
||||
const ensureSemver = (input: string | null | undefined): string | null => {
|
||||
const normalized = normalizeVersion(input)
|
||||
if (!normalized) return null
|
||||
return SEMVER_FULL_REGEX.test(normalized) ? normalized : null
|
||||
}
|
||||
|
||||
const extractSemver = (input: string | null | undefined): string | null => {
|
||||
if (typeof input !== 'string') return null
|
||||
const match = input.match(SEMVER_SEARCH_REGEX)
|
||||
if (!match) return null
|
||||
return normalizeVersion(match[0])
|
||||
}
|
||||
|
||||
const compareVersions = (a: string | null, b: string | null): number | null => {
|
||||
if (!a || !b) return null
|
||||
try {
|
||||
@@ -41,32 +23,6 @@ const compareVersions = (a: string | null, b: string | null): number | null => {
|
||||
}
|
||||
}
|
||||
|
||||
const resolveRemoteVersion = (update: Update): string | null => {
|
||||
const primary = ensureSemver(update.version)
|
||||
if (primary) return primary
|
||||
|
||||
const fallbackPrimary = extractSemver(update.version)
|
||||
if (fallbackPrimary) return fallbackPrimary
|
||||
|
||||
const raw = update.rawJson ?? {}
|
||||
const rawVersion = ensureSemver(
|
||||
typeof raw.version === 'string' ? raw.version : null,
|
||||
)
|
||||
if (rawVersion) return rawVersion
|
||||
|
||||
const tagVersion = extractSemver(
|
||||
typeof raw.tag_name === 'string' ? raw.tag_name : null,
|
||||
)
|
||||
if (tagVersion) return tagVersion
|
||||
|
||||
const nameVersion = extractSemver(
|
||||
typeof raw.name === 'string' ? raw.name : null,
|
||||
)
|
||||
if (nameVersion) return nameVersion
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
const localVersionNormalized = normalizeVersion(appVersion)
|
||||
|
||||
export const checkUpdateSafe = async (
|
||||
@@ -75,7 +31,7 @@ export const checkUpdateSafe = async (
|
||||
const result = await check(options ?? {})
|
||||
if (!result) return null
|
||||
|
||||
const remoteVersion = resolveRemoteVersion(result)
|
||||
const remoteVersion = result.version
|
||||
const comparison = compareVersions(remoteVersion, localVersionNormalized)
|
||||
|
||||
const isBuildToStable =
|
||||
|
||||
Reference in New Issue
Block a user