mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-30 18:10:04 +08:00
refactor(react): replace avoidable effects with derived state and handlers
Also drops a duplicate WebDAV history request on open and the per-keystroke DNS YAML dump.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { arrayMove } from '@dnd-kit/helpers'
|
||||
import type { DragEndEvent } from '@dnd-kit/react'
|
||||
import { isSortable } from '@dnd-kit/react/sortable'
|
||||
import { useCallback, useEffect, useMemo, useReducer } from 'react'
|
||||
import { useCallback, useMemo, useReducer, useState } from 'react'
|
||||
|
||||
type MenuOrderAction = { type: 'sync'; payload: string[] }
|
||||
|
||||
@@ -79,9 +79,11 @@ export const useNavMenuOrder = <T extends { path: string }>({
|
||||
configMenuOrder,
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
const [syncedConfigOrder, setSyncedConfigOrder] = useState(configMenuOrder)
|
||||
if (!areOrdersEqual(syncedConfigOrder, configMenuOrder)) {
|
||||
setSyncedConfigOrder(configMenuOrder)
|
||||
dispatchMenuOrder({ type: 'sync', payload: configMenuOrder })
|
||||
}, [configMenuOrder])
|
||||
}
|
||||
|
||||
const isDefaultOrder = useMemo(
|
||||
() => areOrdersEqual(menuOrder, defaultOrder),
|
||||
|
||||
+17
-18
@@ -1,11 +1,15 @@
|
||||
import { LanOutlined, LanRounded, WarningRounded } from '@mui/icons-material'
|
||||
import { Box, Button, ButtonGroup, Typography } from '@mui/material'
|
||||
import { useLockFn } from 'ahooks'
|
||||
import { useCallback, useEffect, useReducer, useState } from 'react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
import { BaseDialog, BasePage, TooltipIcon } from '@/components/base'
|
||||
import { ProviderButton } from '@/components/proxy/provider-button'
|
||||
import {
|
||||
parseChainConfigItems,
|
||||
type ProxyChainItem,
|
||||
} from '@/components/proxy/proxy-chain-model'
|
||||
import { ProxyGroups } from '@/components/proxy/proxy-groups'
|
||||
import {
|
||||
useAppRefreshers,
|
||||
@@ -40,18 +44,13 @@ const ProxyPage = () => {
|
||||
})
|
||||
const [chainConfirmOpen, setChainConfirmOpen] = useState(false)
|
||||
|
||||
const [chainConfigData, dispatchChainConfigData] = useReducer(
|
||||
(_: string | null, action: string | null) => action,
|
||||
null as string | null,
|
||||
)
|
||||
const [chainConfigItems, setChainConfigItems] = useState<
|
||||
ProxyChainItem[] | null
|
||||
>(null)
|
||||
|
||||
const { clashConfig } = useClashConfigData()
|
||||
const { refreshClashConfig } = useAppRefreshers()
|
||||
|
||||
const updateChainConfigData = useCallback((value: string | null) => {
|
||||
dispatchChainConfigData(value)
|
||||
}, [])
|
||||
|
||||
const normalizedMode = clashConfig?.mode?.toLowerCase()
|
||||
const curMode = isMode(normalizedMode) ? normalizedMode : undefined
|
||||
const chainWarning = t('proxies.page.chain.warning')
|
||||
@@ -71,6 +70,7 @@ const ProxyPage = () => {
|
||||
|
||||
const applyChainMode = (enabled: boolean) => {
|
||||
setIsChainMode(enabled)
|
||||
if (!enabled) setChainConfigItems(null)
|
||||
localStorage.setItem('proxy-chain-mode-enabled', enabled.toString())
|
||||
}
|
||||
|
||||
@@ -99,10 +99,7 @@ const ProxyPage = () => {
|
||||
|
||||
// 当开启链式代理模式时,获取配置数据
|
||||
useEffect(() => {
|
||||
if (!isChainMode) {
|
||||
updateChainConfigData(null)
|
||||
return
|
||||
}
|
||||
if (!isChainMode) return
|
||||
|
||||
let cancelled = false
|
||||
|
||||
@@ -113,19 +110,21 @@ const ProxyPage = () => {
|
||||
if (!exitNode) {
|
||||
console.error('No proxy chain exit node found in localStorage')
|
||||
if (!cancelled) {
|
||||
updateChainConfigData('')
|
||||
setChainConfigItems([])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const configData = await getRuntimeProxyChainConfig(exitNode)
|
||||
if (!cancelled) {
|
||||
updateChainConfigData(configData || '')
|
||||
setChainConfigItems(
|
||||
configData ? parseChainConfigItems(configData) : [],
|
||||
)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to get runtime proxy chain config:', error)
|
||||
if (!cancelled) {
|
||||
updateChainConfigData('')
|
||||
setChainConfigItems([])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -135,7 +134,7 @@ const ProxyPage = () => {
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [isChainMode, updateChainConfigData])
|
||||
}, [isChainMode])
|
||||
|
||||
useEffect(() => {
|
||||
if (normalizedMode && !isMode(normalizedMode)) {
|
||||
@@ -204,7 +203,7 @@ const ProxyPage = () => {
|
||||
<ProxyGroups
|
||||
mode={curMode ?? 'rule'}
|
||||
isChainMode={isChainMode}
|
||||
chainConfigData={chainConfigData}
|
||||
chainConfigItems={chainConfigItems}
|
||||
onExitChainMode={onExitChainMode}
|
||||
/>
|
||||
<BaseDialog
|
||||
|
||||
Reference in New Issue
Block a user