mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-28 16:30:03 +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:
@@ -4,6 +4,7 @@ import {
|
||||
MemoryRounded,
|
||||
} from '@mui/icons-material'
|
||||
import { Box, Typography } from '@mui/material'
|
||||
import type { BoxProps, SvgIconProps, TypographyProps } from '@mui/material'
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
@@ -52,21 +53,21 @@ export const LayoutTraffic = () => {
|
||||
const [down, downUnit] = parseTraffic(traffic?.down || 0)
|
||||
const [inuse, inuseUnit] = parseTraffic(memory?.inuse || 0)
|
||||
|
||||
const boxStyle: any = {
|
||||
const boxStyle: Pick<BoxProps, 'sx'> = {
|
||||
sx: {
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
whiteSpace: 'nowrap',
|
||||
},
|
||||
}
|
||||
const iconStyle: any = {
|
||||
const iconStyle: Pick<SvgIconProps, 'sx'> = {
|
||||
sx: { mr: '8px', fontSize: 16 },
|
||||
}
|
||||
const valStyle: any = {
|
||||
const valStyle: Pick<TypographyProps, 'component' | 'sx'> = {
|
||||
component: 'span',
|
||||
sx: { flex: '1 1 56px', userSelect: 'none', textAlign: 'center' },
|
||||
}
|
||||
const unitStyle: any = {
|
||||
const unitStyle: Pick<TypographyProps, 'component' | 'color' | 'sx'> = {
|
||||
component: 'span',
|
||||
color: 'grey.500',
|
||||
sx: {
|
||||
|
||||
@@ -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}>
|
||||
|
||||
@@ -191,7 +191,7 @@ const SettingClash = ({ onError }: Props) => {
|
||||
onFormat={(e: any) => e.target.value}
|
||||
onChange={(e) => onChangeData({ 'log-level': e })}
|
||||
onGuard={(e) => {
|
||||
setClashLog((pre: any) => ({ ...pre, logLevel: e }))
|
||||
setClashLog((pre) => ({ ...pre!, logLevel: e }))
|
||||
return patchClash({ 'log-level': e })
|
||||
}}
|
||||
>
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import { useLockFn } from 'ahooks'
|
||||
import { useCallback, useEffect, useReducer } from 'react'
|
||||
|
||||
import { useVerge } from '@/hooks/use-verge'
|
||||
import delayManager, { type DelayUpdate } from '@/services/delay'
|
||||
|
||||
const PRESET_PROXY_NAMES = [
|
||||
'DIRECT',
|
||||
'REJECT',
|
||||
'REJECT-DROP',
|
||||
'PASS',
|
||||
'COMPATIBLE',
|
||||
]
|
||||
|
||||
const identity = (_: DelayUpdate, next: DelayUpdate): DelayUpdate => next
|
||||
|
||||
const INITIAL_DELAY: DelayUpdate = { delay: -1, updatedAt: 0 }
|
||||
|
||||
export interface UseProxyDelayState {
|
||||
delayState: DelayUpdate
|
||||
delayValue: number
|
||||
isPreset: boolean
|
||||
timeout: number
|
||||
onDelay: () => Promise<void>
|
||||
}
|
||||
|
||||
export function useProxyDelayState(
|
||||
proxy: IProxyItem,
|
||||
groupName: string,
|
||||
): UseProxyDelayState {
|
||||
const isPreset = PRESET_PROXY_NAMES.includes(proxy.name)
|
||||
const [delayState, setDelayState] = useReducer(identity, INITIAL_DELAY)
|
||||
const { verge } = useVerge()
|
||||
const timeout = verge?.default_latency_timeout || 10000
|
||||
|
||||
useEffect(() => {
|
||||
if (isPreset) return
|
||||
delayManager.setListener(proxy.name, groupName, setDelayState)
|
||||
return () => {
|
||||
delayManager.removeListener(proxy.name, groupName)
|
||||
}
|
||||
}, [proxy.name, groupName, isPreset])
|
||||
|
||||
const updateDelay = useCallback(() => {
|
||||
if (!proxy) return
|
||||
const cachedUpdate = delayManager.getDelayUpdate(proxy.name, groupName)
|
||||
if (cachedUpdate) {
|
||||
setDelayState({ ...cachedUpdate })
|
||||
return
|
||||
}
|
||||
|
||||
const fallbackDelay = delayManager.getDelayFix(proxy, groupName)
|
||||
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, groupName])
|
||||
|
||||
useEffect(() => {
|
||||
updateDelay()
|
||||
}, [updateDelay])
|
||||
|
||||
const onDelay = useLockFn(async () => {
|
||||
setDelayState({ delay: -2, updatedAt: Date.now() })
|
||||
setDelayState(await delayManager.checkDelay(proxy.name, groupName, timeout))
|
||||
})
|
||||
|
||||
return {
|
||||
delayState,
|
||||
delayValue: delayState.delay,
|
||||
isPreset,
|
||||
timeout,
|
||||
onDelay,
|
||||
}
|
||||
}
|
||||
+6
-6
@@ -71,18 +71,18 @@ const LogPage = () => {
|
||||
}
|
||||
}, [filteredLogs.length, isDescending])
|
||||
|
||||
const handleLogLevelChange = (newLevel: string) => {
|
||||
setClashLog((pre: any) => ({ ...pre, logFilter: newLevel }))
|
||||
const handleLogLevelChange = (newLevel: LogFilter) => {
|
||||
setClashLog((pre) => ({ ...pre!, logFilter: newLevel }))
|
||||
}
|
||||
|
||||
const handleToggleLog = async () => {
|
||||
setClashLog((pre: any) => ({ ...pre, enable: !enableLog }))
|
||||
setClashLog((pre) => ({ ...pre!, enable: !enableLog }))
|
||||
}
|
||||
|
||||
const handleToggleOrder = () => {
|
||||
setClashLog((pre: any) => ({
|
||||
...pre,
|
||||
logOrder: pre.logOrder === 'desc' ? 'asc' : 'desc',
|
||||
setClashLog((pre) => ({
|
||||
...pre!,
|
||||
logOrder: pre!.logOrder === 'desc' ? 'asc' : 'desc',
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { LanOutlined, LanRounded } from '@mui/icons-material'
|
||||
import { Box, Button, ButtonGroup } from '@mui/material'
|
||||
import { useLockFn } from 'ahooks'
|
||||
import { useCallback, useEffect, useMemo, useReducer, useState } from 'react'
|
||||
import { useCallback, useEffect, useReducer, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { closeAllConnections } from 'tauri-plugin-mihomo-api'
|
||||
|
||||
@@ -48,8 +48,6 @@ const ProxyPage = () => {
|
||||
}, [])
|
||||
const { verge } = useVerge()
|
||||
|
||||
const modeList = useMemo(() => MODES, [])
|
||||
|
||||
const normalizedMode = clashConfig?.mode?.toLowerCase()
|
||||
const curMode = isMode(normalizedMode) ? normalizedMode : undefined
|
||||
|
||||
@@ -141,7 +139,7 @@ const ProxyPage = () => {
|
||||
<ProviderButton />
|
||||
|
||||
<ButtonGroup size="small">
|
||||
{modeList.map((mode) => (
|
||||
{MODES.map((mode) => (
|
||||
<Button
|
||||
key={mode}
|
||||
variant={mode === curMode ? 'contained' : 'outlined'}
|
||||
|
||||
+19
-30
@@ -27,11 +27,26 @@ class DelayManager {
|
||||
private itemFlushScheduled = false
|
||||
private groupFlushScheduled = false
|
||||
|
||||
private scheduleOnNextFrame(run: () => void): void {
|
||||
if (typeof window !== 'undefined') {
|
||||
if (typeof window.requestAnimationFrame === 'function') {
|
||||
window.requestAnimationFrame(run)
|
||||
return
|
||||
}
|
||||
if (typeof window.setTimeout === 'function') {
|
||||
window.setTimeout(run, 0)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Promise.resolve().then(run)
|
||||
}
|
||||
|
||||
private scheduleItemFlush() {
|
||||
if (this.itemFlushScheduled) return
|
||||
this.itemFlushScheduled = true
|
||||
|
||||
const run = () => {
|
||||
this.scheduleOnNextFrame(() => {
|
||||
this.itemFlushScheduled = false
|
||||
const updates = this.pendingItemUpdates
|
||||
this.pendingItemUpdates = new Map()
|
||||
@@ -51,27 +66,14 @@ class DelayManager {
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
if (typeof window.requestAnimationFrame === 'function') {
|
||||
window.requestAnimationFrame(run)
|
||||
return
|
||||
}
|
||||
if (typeof window.setTimeout === 'function') {
|
||||
window.setTimeout(run, 0)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Promise.resolve().then(run)
|
||||
})
|
||||
}
|
||||
|
||||
private scheduleGroupFlush() {
|
||||
if (this.groupFlushScheduled) return
|
||||
this.groupFlushScheduled = true
|
||||
|
||||
const run = () => {
|
||||
this.scheduleOnNextFrame(() => {
|
||||
this.groupFlushScheduled = false
|
||||
const groups = this.pendingGroupUpdates
|
||||
this.pendingGroupUpdates = new Set()
|
||||
@@ -88,20 +90,7 @@ class DelayManager {
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
if (typeof window.requestAnimationFrame === 'function') {
|
||||
window.requestAnimationFrame(run)
|
||||
return
|
||||
}
|
||||
if (typeof window.setTimeout === 'function') {
|
||||
window.setTimeout(run, 0)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Promise.resolve().then(run)
|
||||
})
|
||||
}
|
||||
|
||||
private queueGroupNotification(group: string) {
|
||||
|
||||
Reference in New Issue
Block a user