diff --git a/src/hooks/use-log-data.ts b/src/hooks/use-log-data.ts index 78fc0b983..80b6685fe 100644 --- a/src/hooks/use-log-data.ts +++ b/src/hooks/use-log-data.ts @@ -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(undefined) + const previousLogLevelRef = useRef(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([subscriptionCacheKey], []) } } else { + hasLoadedInitialLogsRef.current = false refresh() } } diff --git a/src/hooks/use-mihomo-ws-subscription.ts b/src/hooks/use-mihomo-ws-subscription.ts index 0f40625cf..2ff853e37 100644 --- a/src/hooks/use-mihomo-ws-subscription.ts +++ b/src/hooks/use-mihomo-ws-subscription.ts @@ -188,6 +188,12 @@ export const useMihomoWsSubscription = ( const [date, setDate] = useLocalStorage(storageKey, Date.now()) const subscriptKey = buildSubscriptKey(date) const subscriptionCacheKey = subscriptKey ? `$sub$${subscriptKey}` : null + const lastSubscriptionCacheKeyRef = useRef(null) + if (subscriptionCacheKey) { + lastSubscriptionCacheKeyRef.current = subscriptionCacheKey + } + const responseCacheKey = + subscriptionCacheKey ?? lastSubscriptionCacheKeyRef.current const queryClient = useQueryClient() @@ -209,15 +215,12 @@ export const useMihomoWsSubscription = ( ) const response = useQuery({ - queryKey: subscriptionCacheKey - ? [subscriptionCacheKey] - : ['$sub$__disabled__'], + queryKey: responseCacheKey ? [responseCacheKey] : ['$sub$__disabled__'], queryFn: () => - queryClient.getQueryData([subscriptionCacheKey!]) ?? fallbackData, + queryClient.getQueryData([responseCacheKey!]) ?? fallbackData, initialData: () => - queryClient.getQueryData([ - subscriptionCacheKey ?? '$sub$__disabled__', - ]) ?? fallbackData, + queryClient.getQueryData([responseCacheKey ?? '$sub$__disabled__']) ?? + fallbackData, staleTime: Infinity, gcTime: 30_000, enabled: subscriptionCacheKey !== null, @@ -354,5 +357,5 @@ export const useMihomoWsSubscription = ( setDate(Date.now()) }, [queryClient, subscriptionCacheKey, setDate]) - return { response, refresh, subscriptionCacheKey, wsRef } + return { response, refresh, subscriptionCacheKey: responseCacheKey, wsRef } }