refactor(frontend): centralize tun availability guard

This commit is contained in:
Tunglies
2026-07-23 15:07:33 +08:00
parent 2c8b1a0607
commit 17fe92352e
5 changed files with 33 additions and 31 deletions
+1 -7
View File
@@ -3,8 +3,6 @@ import { useCallback } from 'react'
import { installService, restartCore } from '@/services/cmds'
import { showNotice } from '@/services/notice-service'
import { useSystemState } from './use-system-state'
const executeWithErrorHandling = async (
operation: () => Promise<void>,
loadingKey: string,
@@ -23,8 +21,6 @@ const executeWithErrorHandling = async (
}
export const useServiceInstaller = () => {
const { mutateSystemState } = useSystemState()
const installServiceAndRestartCore = useCallback(async () => {
await executeWithErrorHandling(
() => installService(),
@@ -37,8 +33,6 @@ export const useServiceInstaller = () => {
'settings.statuses.clash.restarting',
'settings.feedback.notifications.clash.restartSuccess',
)
await mutateSystemState()
}, [mutateSystemState])
}, [])
return { installServiceAndRestartCore }
}
+1 -6
View File
@@ -3,8 +3,6 @@ import { useCallback } from 'react'
import { restartCore, stopCore, uninstallService } from '@/services/cmds'
import { showNotice } from '@/services/notice-service'
import { useSystemState } from './use-system-state'
const executeWithErrorHandling = async (
operation: () => Promise<void>,
loadingKey: string,
@@ -23,8 +21,6 @@ const executeWithErrorHandling = async (
}
export const useServiceUninstaller = () => {
const { mutateSystemState } = useSystemState()
const uninstallServiceAndRestartCore = useCallback(async () => {
try {
await executeWithErrorHandling(
@@ -43,9 +39,8 @@ export const useServiceUninstaller = () => {
'settings.statuses.clash.restarting',
'settings.feedback.notifications.clash.restartSuccess',
)
await mutateSystemState()
}
}, [mutateSystemState])
}, [])
return { uninstallServiceAndRestartCore }
}
+18 -14
View File
@@ -27,9 +27,7 @@ const STARTUP_GRACE_MS = 10_000
* 包括运行模式、管理员状态、系统服务是否可用
*/
export function useSystemState() {
const { verge, patchVerge } = useVerge()
const pageVisible = useVisibility()
const disablingTunRef = useRef(false)
const [isStartingUp, setIsStartingUp] = useState(true)
useEffect(() => {
@@ -60,8 +58,25 @@ export function useSystemState() {
const isServiceMode = systemState.runningMode === 'Service'
const isTunModeAvailable = systemState.isAdminMode || systemState.isServiceOk
const enable_tun_mode = verge?.enable_tun_mode
return {
runningMode: systemState.runningMode,
isAdminMode: systemState.isAdminMode,
isServiceOk: systemState.isServiceOk,
isSidecarMode,
isServiceMode,
isTunModeAvailable,
mutateSystemState,
isLoading,
isStartingUp,
}
}
export function useTunAvailabilityGuard() {
const { verge, patchVerge } = useVerge()
const { isTunModeAvailable, isLoading, isStartingUp } = useSystemState()
const disablingTunRef = useRef(false)
const cooldownTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
const enable_tun_mode = verge?.enable_tun_mode
useEffect(() => {
if (enable_tun_mode === undefined) return
@@ -103,15 +118,4 @@ export function useSystemState() {
}
}
}, [enable_tun_mode, isTunModeAvailable, patchVerge, isLoading, isStartingUp])
return {
runningMode: systemState.runningMode,
isAdminMode: systemState.isAdminMode,
isServiceOk: systemState.isServiceOk,
isSidecarMode,
isServiceMode,
isTunModeAvailable,
mutateSystemState,
isLoading,
}
}