mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-08 19:22:44 +08:00
Consolidate ordinary query snapshots on SWR and make Profiles own its query key, cache mutation, and invalidation policy. Impact: - remove the generic synchronous query Map while keeping dedicated Mihomo WebSocket snapshots - eliminate duplicate Profiles refreshes while preserving retries and the 800 ms event throttle - preserve preload-backed Verge updates and system proxy rollback behavior - scope uptime polling to the Home Clash info card instead of the app lifetime - leave RunState, Connections, Traffic, backend, and IPC contracts unchanged - reduce frontend code by 72 net lines without adding dependencies
55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
import { useCallback } from 'react'
|
|
|
|
import { getVergeConfig, patchVergeConfig } from '@/services/cmds'
|
|
import { getPreloadConfig, setPreloadConfig } from '@/services/preload'
|
|
import { setCacheData, useQuery } from '@/services/query-client'
|
|
|
|
export const useVerge = () => {
|
|
const initialVergeConfig = getPreloadConfig()
|
|
|
|
const { data: verge, refetch } = useQuery({
|
|
queryKey: ['getVergeConfig'],
|
|
queryFn: async () => {
|
|
const config = await getVergeConfig()
|
|
setPreloadConfig(config)
|
|
return config
|
|
},
|
|
initialData: initialVergeConfig ?? undefined,
|
|
revalidateOnMount: initialVergeConfig ? false : undefined,
|
|
staleTime: 5000,
|
|
})
|
|
|
|
const mutateVerge = (
|
|
updaterOrData?:
|
|
| IVergeConfig
|
|
| ((prev: IVergeConfig | undefined) => IVergeConfig | undefined)
|
|
| undefined,
|
|
_revalidate?: boolean,
|
|
) => {
|
|
if (updaterOrData === undefined) {
|
|
void refetch()
|
|
return
|
|
}
|
|
void setCacheData<IVergeConfig>(
|
|
['getVergeConfig'],
|
|
typeof updaterOrData === 'function'
|
|
? (current) => updaterOrData(current ?? verge)
|
|
: updaterOrData,
|
|
)
|
|
}
|
|
|
|
const patchVerge = useCallback(
|
|
async (value: Partial<IVergeConfig>) => {
|
|
await patchVergeConfig(value)
|
|
await refetch()
|
|
},
|
|
[refetch],
|
|
)
|
|
|
|
return {
|
|
verge,
|
|
mutateVerge,
|
|
patchVerge,
|
|
}
|
|
}
|