fix(home): unify last check update timestamp across settings and home page (#6605)

The settings page "Check for updates" did not update the homepage
"last check update time" because each page managed the timestamp
independently. Centralizes the timestamp in the useUpdate hook
via SWR + localStorage so both pages share a single data source.

Closes https://github.com/clash-verge-rev/clash-verge-rev/issues/6605#issuecomment-4147144987
This commit is contained in:
Tunglies
2026-03-29 01:56:34 +08:00
parent c3aba3fc79
commit 99bbd7ee5a
3 changed files with 63 additions and 90 deletions
+36 -1
View File
@@ -1,4 +1,4 @@
import useSWR, { SWRConfiguration } from 'swr'
import useSWR, { mutate as globalMutate, SWRConfiguration } from 'swr'
import { checkUpdateSafe } from '@/services/update'
@@ -12,6 +12,26 @@ export interface UpdateInfo {
downloadAndInstall: (onEvent?: any) => Promise<void>
}
// --- Last check timestamp (shared via SWR + localStorage) ---
const LAST_CHECK_KEY = 'last_check_update'
const readLastCheckFromStorage = (): number | null => {
const stored = localStorage.getItem(LAST_CHECK_KEY)
if (!stored) return null
const ts = parseInt(stored, 10)
return isNaN(ts) ? null : ts
}
export const updateLastCheckTime = (timestamp?: number): number => {
const now = timestamp ?? Date.now()
localStorage.setItem(LAST_CHECK_KEY, now.toString())
globalMutate(LAST_CHECK_KEY, now, false)
return now
}
// --- useUpdate hook ---
export const useUpdate = (
enabled: boolean = true,
options?: SWRConfiguration,
@@ -36,11 +56,26 @@ export const useUpdate = (
refreshInterval: 24 * 60 * 60 * 1000, // 24 hours
dedupingInterval: 60 * 60 * 1000, // 1 hour
...options,
onSuccess: (...args) => {
updateLastCheckTime()
options?.onSuccess?.(...args)
},
})
// Shared last check timestamp
const { data: lastCheckUpdate } = useSWR(
LAST_CHECK_KEY,
readLastCheckFromStorage,
{
revalidateOnFocus: false,
revalidateOnReconnect: false,
},
)
return {
updateInfo,
checkUpdate,
loading: isValidating,
lastCheckUpdate: lastCheckUpdate ?? null,
}
}