Files
clash-verge-rev/src/hooks/use-system-state.ts
T
Tunglies 4ed474706a refactor(frontend): consolidate query state ownership
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
2026-09-06 04:32:22 +08:00

71 lines
1.8 KiB
TypeScript

import {
getAppUptime,
getRuntimeState,
type RunState,
type RunningMode,
} from '@/services/cmds'
import { useQuery } from '@/services/query-client'
import { useVisibility } from './use-visibility'
export const runStateQueryKey = ['getRuntimeState'] as const
const appUptimeQueryKey = ['appUptime'] as const
/** Fail closed until the first snapshot so TUN never flashes as available. */
const unknownRunState: RunState = {
mode: 'NotRunning',
service: 'unknown',
serviceUnavailableReason: null,
pendingAction: null,
sidecarAllowed: false,
isAdmin: false,
opInFlight: false,
serviceUsable: false,
tunCapable: false,
serviceNeedsAttention: false,
}
/** Event-driven run state; Rust owns all derived availability decisions. */
export function useSystemState() {
const pageVisible = useVisibility()
const {
data: runState = unknownRunState,
refetch: mutateSystemState,
isLoading,
} = useQuery({
queryKey: runStateQueryKey,
queryFn: getRuntimeState,
// A safety net only; transitions normally arrive by event.
refetchInterval: pageVisible ? 30000 : false,
refetchOnWindowFocus: true,
refetchOnReconnect: true,
})
return {
runState,
runningMode: runState.mode as RunningMode,
isAdminMode: runState.isAdmin,
isSidecarMode: runState.mode === 'Sidecar',
isServiceMode: runState.mode === 'Service',
isTunModeAvailable: runState.tunCapable,
serviceNeedsAttention: runState.serviceNeedsAttention,
mutateSystemState,
isLoading,
}
}
export function useAppUptime() {
const { data: uptime = 0 } = useQuery({
queryKey: appUptimeQueryKey,
queryFn: getAppUptime,
staleTime: 5000,
refetchInterval: 3000,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
retry: 1,
})
return uptime
}