refactor: migrate SWR to TanStack Query v5 (#6713)

Replace swr with @tanstack/react-query v5 across all hooks, providers,
and components. Introduce singleton QueryClient, WS subscription pattern
via useQuery+useEffect, and enforce component-layer cache access contract.
This commit is contained in:
Tunglies
2026-04-03 08:15:51 +00:00
committed by GitHub
parent 6f4ddb6db3
commit a73fafaf9f
27 changed files with 745 additions and 590 deletions
@@ -15,10 +15,10 @@ import { useLockFn } from 'ahooks'
import type { Ref } from 'react'
import { useImperativeHandle, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { mutate } from 'swr'
import { closeAllConnections, upgradeCore } from 'tauri-plugin-mihomo-api'
import { BaseDialog, DialogRef } from '@/components/base'
import { useClash, useClashInfo } from '@/hooks/use-clash'
import { useVerge } from '@/hooks/use-verge'
import { changeClashCore, restartCore } from '@/services/cmds'
import { showNotice } from '@/services/notice-service'
@@ -40,6 +40,8 @@ export function ClashCoreViewer({ ref }: { ref?: Ref<DialogRef> }) {
const { t } = useTranslation()
const { verge, mutateVerge } = useVerge()
const { mutateVersion } = useClash()
const { invalidateClashConfig } = useClashInfo()
const [open, setOpen] = useState(false)
const [upgrading, setUpgrading] = useState(false)
@@ -69,8 +71,8 @@ export function ClashCoreViewer({ ref }: { ref?: Ref<DialogRef> }) {
mutateVerge()
setTimeout(async () => {
mutate('getClashConfig')
mutate('getVersion')
invalidateClashConfig()
mutateVersion()
setChangingCore(null)
}, 500)
} catch (err) {
@@ -22,7 +22,6 @@ import {
useState,
} from 'react'
import { useTranslation } from 'react-i18next'
import { mutate } from 'swr'
import {
BaseDialog,
@@ -110,7 +109,8 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
const [hostOptions, setHostOptions] = useState<string[]>([])
const { clashConfig } = useAppData()
const { indicator: isProxyReallyEnabled } = useSystemProxyState()
const { indicator: isProxyReallyEnabled, invalidateProxyState } =
useSystemProxyState()
const {
enable_system_proxy: enabled,
@@ -166,10 +166,7 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
await patchVergeConfig({ enable_system_proxy: false })
await sleep(200)
await patchVergeConfig({ enable_system_proxy: true })
await Promise.all([
mutate('getSystemProxy'),
mutate('getAutotemProxy'),
])
await invalidateProxyState()
}
} catch (err) {
showNotice.error(err)
@@ -177,7 +174,7 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
}
updateProxy()
}, [clashConfig?.mixedPort, value.pac])
}, [clashConfig?.mixedPort, value.pac, invalidateProxyState])
const { systemProxyAddress } = useAppData()
@@ -410,10 +407,7 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
}
setTimeout(async () => {
try {
await Promise.all([
mutate('getSystemProxy'),
mutate('getAutotemProxy'),
])
await invalidateProxyState()
// 如果需要重置代理且代理当前启用
if (needResetProxy && enabled) {
@@ -430,10 +424,7 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
await patchVergeConfig({ enable_system_proxy: false })
await new Promise((resolve) => setTimeout(resolve, 50))
await patchVergeConfig({ enable_system_proxy: true })
await Promise.all([
mutate('getSystemProxy'),
mutate('getAutotemProxy'),
])
await invalidateProxyState()
}
}
} catch (err) {
@@ -1,6 +1,5 @@
import React, { useRef } from 'react'
import { useTranslation } from 'react-i18next'
import { mutate } from 'swr'
import { DialogRef, Switch, TooltipIcon } from '@/components/base'
import ProxyControlSwitches from '@/components/shared/proxy-control-switches'
@@ -62,7 +61,6 @@ const SettingSystem = ({ onError }: Props) => {
// 先触发UI更新立即看到反馈
onChangeData({ enable_auto_launch: e })
await patchVerge({ enable_auto_launch: e })
await mutate('getAutoLaunchStatus')
return Promise.resolve()
} catch (error) {
// 如果出错,恢复原始状态