mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-25 23:15:51 +08:00
refactor(ui): extract useProxyDelayState hook and tighten types
- Extract shared delay state logic from ProxyItem and ProxyItemMini into `useProxyDelayState` hook (src/hooks/use-proxy-delay-state.ts); JSX display thresholds preserved per-component. - Deduplicate rAF/setTimeout/Promise scheduling in DelayManager via a private `scheduleOnNextFrame` helper. - Drop no-op `useMemo(() => MODES, [])` in proxies page. - Replace `any` on `setClashLog` prev params and layout-traffic sx objects with proper types (`pre!`, `Pick<BoxProps|...>`).
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
import { CheckCircleOutlineRounded } from '@mui/icons-material'
|
||||
import { alpha, Box, ListItemButton, styled, Typography } from '@mui/material'
|
||||
import { useLockFn } from 'ahooks'
|
||||
import { useCallback, useEffect, useReducer } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
import { BaseLoading } from '@/components/base'
|
||||
import { useVerge } from '@/hooks/use-verge'
|
||||
import delayManager, { DelayUpdate } from '@/services/delay'
|
||||
import { useProxyDelayState } from '@/hooks/use-proxy-delay-state'
|
||||
import delayManager from '@/services/delay'
|
||||
|
||||
interface Props {
|
||||
group: IProxyGroupItem
|
||||
@@ -22,70 +20,11 @@ export const ProxyItemMini = (props: Props) => {
|
||||
|
||||
const { t } = useTranslation()
|
||||
|
||||
const presetList = ['DIRECT', 'REJECT', 'REJECT-DROP', 'PASS', 'COMPATIBLE']
|
||||
const isPreset = presetList.includes(proxy.name)
|
||||
// -1/<=0 为不显示,-2 为 loading
|
||||
const [delayState, setDelayState] = useReducer(
|
||||
(_: DelayUpdate, next: DelayUpdate) => next,
|
||||
{
|
||||
delay: -1,
|
||||
updatedAt: 0,
|
||||
},
|
||||
const { delayValue, isPreset, timeout, onDelay } = useProxyDelayState(
|
||||
proxy,
|
||||
group.name,
|
||||
)
|
||||
const { verge } = useVerge()
|
||||
const timeout = verge?.default_latency_timeout || 10000
|
||||
|
||||
useEffect(() => {
|
||||
if (isPreset) return
|
||||
delayManager.setListener(proxy.name, group.name, setDelayState)
|
||||
|
||||
return () => {
|
||||
delayManager.removeListener(proxy.name, group.name)
|
||||
}
|
||||
}, [isPreset, proxy.name, group.name])
|
||||
|
||||
const updateDelay = useCallback(() => {
|
||||
if (!proxy) return
|
||||
const cachedUpdate = delayManager.getDelayUpdate(proxy.name, group.name)
|
||||
if (cachedUpdate) {
|
||||
setDelayState({ ...cachedUpdate })
|
||||
return
|
||||
}
|
||||
|
||||
const fallbackDelay = delayManager.getDelayFix(proxy, group.name)
|
||||
if (fallbackDelay === -1) {
|
||||
setDelayState({ delay: -1, updatedAt: 0 })
|
||||
return
|
||||
}
|
||||
|
||||
let updatedAt = 0
|
||||
const history = proxy.history
|
||||
if (history && history.length > 0) {
|
||||
const lastRecord = history[history.length - 1]
|
||||
const parsed = Date.parse(lastRecord.time)
|
||||
if (!Number.isNaN(parsed)) {
|
||||
updatedAt = parsed
|
||||
}
|
||||
}
|
||||
|
||||
setDelayState({
|
||||
delay: fallbackDelay,
|
||||
updatedAt,
|
||||
})
|
||||
}, [proxy, group.name])
|
||||
|
||||
useEffect(() => {
|
||||
updateDelay()
|
||||
}, [updateDelay])
|
||||
|
||||
const onDelay = useLockFn(async () => {
|
||||
setDelayState({ delay: -2, updatedAt: Date.now() })
|
||||
setDelayState(
|
||||
await delayManager.checkDelay(proxy.name, group.name, timeout),
|
||||
)
|
||||
})
|
||||
|
||||
const delayValue = delayState.delay
|
||||
|
||||
return (
|
||||
<ListItemButton
|
||||
|
||||
@@ -10,12 +10,10 @@ import {
|
||||
SxProps,
|
||||
Theme,
|
||||
} from '@mui/material'
|
||||
import { useLockFn } from 'ahooks'
|
||||
import { useCallback, useEffect, useReducer } from 'react'
|
||||
|
||||
import { BaseLoading } from '@/components/base'
|
||||
import { useVerge } from '@/hooks/use-verge'
|
||||
import delayManager, { DelayUpdate } from '@/services/delay'
|
||||
import { useProxyDelayState } from '@/hooks/use-proxy-delay-state'
|
||||
import delayManager from '@/services/delay'
|
||||
|
||||
interface Props {
|
||||
group: IProxyGroupItem
|
||||
@@ -47,70 +45,11 @@ const TypeBox = styled('span')(({ theme }) => ({
|
||||
export const ProxyItem = (props: Props) => {
|
||||
const { group, proxy, selected, showType = true, sx, onClick } = props
|
||||
|
||||
const presetList = ['DIRECT', 'REJECT', 'REJECT-DROP', 'PASS', 'COMPATIBLE']
|
||||
const isPreset = presetList.includes(proxy.name)
|
||||
// -1/<=0 为不显示,-2 为 loading
|
||||
const [delayState, setDelayState] = useReducer(
|
||||
(_: DelayUpdate, next: DelayUpdate) => next,
|
||||
{
|
||||
delay: -1,
|
||||
updatedAt: 0,
|
||||
},
|
||||
const { delayValue, isPreset, timeout, onDelay } = useProxyDelayState(
|
||||
proxy,
|
||||
group.name,
|
||||
)
|
||||
const { verge } = useVerge()
|
||||
const timeout = verge?.default_latency_timeout || 10000
|
||||
|
||||
useEffect(() => {
|
||||
if (isPreset) return
|
||||
delayManager.setListener(proxy.name, group.name, setDelayState)
|
||||
|
||||
return () => {
|
||||
delayManager.removeListener(proxy.name, group.name)
|
||||
}
|
||||
}, [proxy.name, group.name, isPreset])
|
||||
|
||||
const updateDelay = useCallback(() => {
|
||||
if (!proxy) return
|
||||
const cachedUpdate = delayManager.getDelayUpdate(proxy.name, group.name)
|
||||
if (cachedUpdate) {
|
||||
setDelayState({ ...cachedUpdate })
|
||||
return
|
||||
}
|
||||
|
||||
const fallbackDelay = delayManager.getDelayFix(proxy, group.name)
|
||||
if (fallbackDelay === -1) {
|
||||
setDelayState({ delay: -1, updatedAt: 0 })
|
||||
return
|
||||
}
|
||||
|
||||
let updatedAt = 0
|
||||
const history = proxy.history
|
||||
if (history && history.length > 0) {
|
||||
const lastRecord = history[history.length - 1]
|
||||
const parsed = Date.parse(lastRecord.time)
|
||||
if (!Number.isNaN(parsed)) {
|
||||
updatedAt = parsed
|
||||
}
|
||||
}
|
||||
|
||||
setDelayState({
|
||||
delay: fallbackDelay,
|
||||
updatedAt,
|
||||
})
|
||||
}, [proxy, group.name])
|
||||
|
||||
useEffect(() => {
|
||||
updateDelay()
|
||||
}, [updateDelay])
|
||||
|
||||
const onDelay = useLockFn(async () => {
|
||||
setDelayState({ delay: -2, updatedAt: Date.now() })
|
||||
setDelayState(
|
||||
await delayManager.checkDelay(proxy.name, group.name, timeout),
|
||||
)
|
||||
})
|
||||
|
||||
const delayValue = delayState.delay
|
||||
|
||||
return (
|
||||
<ListItem sx={sx}>
|
||||
|
||||
Reference in New Issue
Block a user