fix(logs): preserve logs when pausing (#6902)

This commit is contained in:
Tunglies
2026-04-26 18:53:16 +08:00
parent b85b435e00
commit 5b738a9d7a
2 changed files with 19 additions and 9 deletions
+8 -1
View File
@@ -53,6 +53,7 @@ export const useLogData = () => {
const enableLog = clashLog.enable
const logLevel = clashLog.logLevel
const allowedTypes = LOG_LEVEL_FILTERS[logLevel] ?? DEFAULT_LOG_TYPES
const hasLoadedInitialLogsRef = useRef(false)
const { response, refresh, subscriptionCacheKey } = useMihomoWsSubscription<
ILogItem[]
@@ -116,7 +117,11 @@ export const useLogData = () => {
}
},
async onConnected() {
if (hasLoadedInitialLogsRef.current) {
return
}
const logs = await getClashLogs()
hasLoadedInitialLogsRef.current = true
if (isMounted()) {
next(null, (current) => {
if (!current || current.length === 0) {
@@ -131,7 +136,7 @@ export const useLogData = () => {
},
})
const previousLogLevelRef = useRef<string | undefined>(undefined)
const previousLogLevelRef = useRef<LogLevel | undefined>(logLevel)
useEffect(() => {
if (!logLevel) {
@@ -144,6 +149,7 @@ export const useLogData = () => {
}
previousLogLevelRef.current = logLevel
hasLoadedInitialLogsRef.current = false
refresh()
}, [logLevel, refresh])
@@ -153,6 +159,7 @@ export const useLogData = () => {
queryClient.setQueryData<ILogItem[]>([subscriptionCacheKey], [])
}
} else {
hasLoadedInitialLogsRef.current = false
refresh()
}
}
+11 -8
View File
@@ -188,6 +188,12 @@ export const useMihomoWsSubscription = <T>(
const [date, setDate] = useLocalStorage(storageKey, Date.now())
const subscriptKey = buildSubscriptKey(date)
const subscriptionCacheKey = subscriptKey ? `$sub$${subscriptKey}` : null
const lastSubscriptionCacheKeyRef = useRef<string | null>(null)
if (subscriptionCacheKey) {
lastSubscriptionCacheKeyRef.current = subscriptionCacheKey
}
const responseCacheKey =
subscriptionCacheKey ?? lastSubscriptionCacheKeyRef.current
const queryClient = useQueryClient()
@@ -209,15 +215,12 @@ export const useMihomoWsSubscription = <T>(
)
const response = useQuery<T>({
queryKey: subscriptionCacheKey
? [subscriptionCacheKey]
: ['$sub$__disabled__'],
queryKey: responseCacheKey ? [responseCacheKey] : ['$sub$__disabled__'],
queryFn: () =>
queryClient.getQueryData<T>([subscriptionCacheKey!]) ?? fallbackData,
queryClient.getQueryData<T>([responseCacheKey!]) ?? fallbackData,
initialData: () =>
queryClient.getQueryData<T>([
subscriptionCacheKey ?? '$sub$__disabled__',
]) ?? fallbackData,
queryClient.getQueryData<T>([responseCacheKey ?? '$sub$__disabled__']) ??
fallbackData,
staleTime: Infinity,
gcTime: 30_000,
enabled: subscriptionCacheKey !== null,
@@ -354,5 +357,5 @@ export const useMihomoWsSubscription = <T>(
setDate(Date.now())
}, [queryClient, subscriptionCacheKey, setDate])
return { response, refresh, subscriptionCacheKey, wsRef }
return { response, refresh, subscriptionCacheKey: responseCacheKey, wsRef }
}