mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-21 03:32:09 +08:00
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:
@@ -5,23 +5,22 @@ import {
|
||||
VisibilityOutlined,
|
||||
} from '@mui/icons-material'
|
||||
import { Box, Button, IconButton, Skeleton, Typography } from '@mui/material'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow'
|
||||
import { useEffect } from 'foxact/use-abortable-effect'
|
||||
import { useIntersection } from 'foxact/use-intersection'
|
||||
import type { XOR } from 'foxts/ts-xor'
|
||||
import {
|
||||
forwardRef,
|
||||
memo,
|
||||
useCallback,
|
||||
useState,
|
||||
useEffectEvent,
|
||||
useMemo,
|
||||
forwardRef,
|
||||
useState,
|
||||
} from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import useSWRImmutable from 'swr/immutable'
|
||||
|
||||
import { getIpInfo } from '@/services/api'
|
||||
import { SWR_EXTERNAL_API } from '@/services/config'
|
||||
|
||||
import { EnhancedCard } from './enhanced-card'
|
||||
|
||||
@@ -78,7 +77,7 @@ type CountDownState = XOR<
|
||||
const IPInfoCardContainer = forwardRef<HTMLElement, React.PropsWithChildren>(
|
||||
({ children }, ref) => {
|
||||
const { t } = useTranslation()
|
||||
const { mutate } = useIPInfo()
|
||||
const { refetch: mutate } = useIPInfo()
|
||||
|
||||
return (
|
||||
<EnhancedCard
|
||||
@@ -117,7 +116,7 @@ export const IpInfoCard = () => {
|
||||
remainingSeconds: IP_REFRESH_SECONDS,
|
||||
})
|
||||
|
||||
const { data: ipInfo, error, isLoading, mutate } = useIPInfo()
|
||||
const { data: ipInfo, error, isLoading, refetch: mutate } = useIPInfo()
|
||||
|
||||
// function useEffectEvent
|
||||
const onCountdownTick = useEffectEvent(async () => {
|
||||
@@ -422,5 +421,14 @@ export const IpInfoCard = () => {
|
||||
}
|
||||
|
||||
function useIPInfo() {
|
||||
return useSWRImmutable(IP_INFO_CACHE_KEY, getIpInfo, SWR_EXTERNAL_API)
|
||||
return useQuery({
|
||||
queryKey: [IP_INFO_CACHE_KEY],
|
||||
queryFn: getIpInfo,
|
||||
staleTime: Infinity,
|
||||
gcTime: Infinity,
|
||||
refetchOnWindowFocus: false,
|
||||
refetchOnReconnect: false,
|
||||
retry: 1,
|
||||
retryDelay: 30_000,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -101,7 +101,8 @@ export const SystemInfoCard = () => {
|
||||
// 检查更新
|
||||
const onCheckUpdate = useLockFn(async () => {
|
||||
try {
|
||||
const info = await triggerCheckUpdate()
|
||||
const result = await triggerCheckUpdate()
|
||||
const info = result.data
|
||||
if (!info?.available) {
|
||||
showNotice.success(
|
||||
'settings.components.verge.advanced.notifications.latestVersion',
|
||||
|
||||
@@ -21,7 +21,6 @@ import { useLockFn } from 'ahooks'
|
||||
import dayjs from 'dayjs'
|
||||
import { useCallback, useEffect, useReducer, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { mutate } from 'swr'
|
||||
|
||||
import { ConfirmViewer } from '@/components/profile/confirm-viewer'
|
||||
import { EditorViewer } from '@/components/profile/editor-viewer'
|
||||
@@ -53,6 +52,7 @@ interface Props {
|
||||
selected: boolean
|
||||
activating: boolean
|
||||
itemData: IProfileItem
|
||||
mutateProfiles: () => Promise<void>
|
||||
onSelect: (force: boolean) => void
|
||||
onEdit: () => void
|
||||
onSave?: (prev?: string, curr?: string) => void
|
||||
@@ -68,6 +68,7 @@ export const ProfileItem = (props: Props) => {
|
||||
selected,
|
||||
activating,
|
||||
itemData,
|
||||
mutateProfiles,
|
||||
onSelect,
|
||||
onEdit,
|
||||
onSave,
|
||||
@@ -383,7 +384,7 @@ export const ProfileItem = (props: Props) => {
|
||||
await updateProfile(itemData.uid, payload)
|
||||
|
||||
// 更新成功,刷新列表
|
||||
mutate('getProfiles')
|
||||
void mutateProfiles()
|
||||
} catch {
|
||||
// 更新完全失败(包括后端的回退尝试)
|
||||
// 不需要做处理,后端会通过事件通知系统发送错误
|
||||
@@ -579,7 +580,7 @@ export const ProfileItem = (props: Props) => {
|
||||
if (customEvent.detail?.uid === itemData.uid) {
|
||||
setLoadingCache((cache) => ({ ...cache, [itemData.uid]: false }))
|
||||
// 刷新 profile 数据以获取最新的 updated 时间戳
|
||||
mutate('getProfiles')
|
||||
void mutateProfiles()
|
||||
// 更新完成后刷新显示
|
||||
if (showNextUpdate) {
|
||||
fetchNextUpdateTime()
|
||||
@@ -599,7 +600,13 @@ export const ProfileItem = (props: Props) => {
|
||||
handleUpdateCompleted,
|
||||
)
|
||||
}
|
||||
}, [fetchNextUpdateTime, itemData.uid, setLoadingCache, showNextUpdate])
|
||||
}, [
|
||||
fetchNextUpdateTime,
|
||||
itemData.uid,
|
||||
mutateProfiles,
|
||||
setLoadingCache,
|
||||
showNextUpdate,
|
||||
])
|
||||
|
||||
const handleSaveProfileDocument = useLockFn(async () => {
|
||||
const currentValue = profileDocument.value
|
||||
|
||||
@@ -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) {
|
||||
// 如果出错,恢复原始状态
|
||||
|
||||
Reference in New Issue
Block a user