mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-16 16:32:11 +08:00
perf(proxy): consolidate proxy data into a single IPC (#7542)
* feat(proxy): build deterministic proxy view * feat(proxy): expose ordered proxy view command * feat(proxy): add proxy view client contract * fix(lint): allow Node test files * feat(proxy): switch UI to ordered proxy view * fix(proxy): harden ordered proxy view consumers * fix(proxy): compare chain duplicates by current id * fix(proxy): release mihomo guard after fetch * fix(proxy): preserve semantic selections across refresh * fix(proxy): block global chain without GLOBAL * fix(proxy): handle proxy view edge states
This commit is contained in:
@@ -21,24 +21,24 @@ import {
|
||||
} from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
import { useProxiesData } from '@/providers/app-data-context'
|
||||
import { updateProxyChainConfigInRuntime } from '@/services/cmds'
|
||||
import {
|
||||
isInteractableMember,
|
||||
type ProxyGroupView,
|
||||
type ResolvedProxyMember,
|
||||
} from '@/types/proxy-view'
|
||||
|
||||
import { ScrollTopButton } from '../layout/scroll-top-button'
|
||||
|
||||
import { ProxyChain } from './proxy-chain'
|
||||
import { type ProxyChainItem, rebindProxyChainItems } from './proxy-chain-model'
|
||||
import { ProxyRender } from './proxy-render'
|
||||
import type { HeadState } from './use-head-state'
|
||||
import type { IRenderItem } from './use-render-list'
|
||||
|
||||
// ---- Types ----
|
||||
|
||||
interface ProxyChainItem {
|
||||
id: string
|
||||
name: string
|
||||
type?: string
|
||||
delay?: number
|
||||
}
|
||||
|
||||
type VirtualListItem = {
|
||||
key: Key
|
||||
index: number
|
||||
@@ -46,11 +46,7 @@ type VirtualListItem = {
|
||||
end: number
|
||||
}
|
||||
|
||||
interface ProxyGroupOption {
|
||||
name: string
|
||||
type: string
|
||||
all?: unknown[]
|
||||
}
|
||||
type ProxyGroupOption = ProxyGroupView
|
||||
|
||||
// ---- Props ----
|
||||
|
||||
@@ -201,7 +197,7 @@ function GroupSelectMenu({
|
||||
{group.name}
|
||||
</Typography>
|
||||
<Typography variant="caption" color="text.secondary">
|
||||
{group.type} · {group.all?.length ?? 0} 节点
|
||||
{group.type} · {group.members.length} 节点
|
||||
</Typography>
|
||||
</Box>
|
||||
</MenuItem>
|
||||
@@ -243,7 +239,7 @@ function ProxyVirtualList({
|
||||
onLocation: (group: any) => void
|
||||
onCheckAll: (groupName: string) => void
|
||||
onHeadState: (groupName: string, patch: Partial<HeadState>) => void
|
||||
onChangeProxy: (group: IProxyGroupItem, proxy: IProxyItem) => void
|
||||
onChangeProxy: (group: ProxyGroupView, member: ResolvedProxyMember) => void
|
||||
}) {
|
||||
const theme = useTheme()
|
||||
const stickyBackground =
|
||||
@@ -316,6 +312,7 @@ export function ProxyGroupsChain(props: ProxyGroupsChainProps) {
|
||||
onGroupSelect,
|
||||
onScrollToTop,
|
||||
} = props
|
||||
const { proxyView } = useProxiesData()
|
||||
|
||||
// Chain-specific state
|
||||
const [proxyChain, setProxyChain] = useState<ProxyChainItem[]>(() => {
|
||||
@@ -330,13 +327,44 @@ export function ProxyGroupsChain(props: ProxyGroupsChainProps) {
|
||||
return []
|
||||
})
|
||||
|
||||
const candidateNodes = useMemo(
|
||||
() =>
|
||||
renderList.flatMap((item) => {
|
||||
const occurrences = item.memberCol ?? (item.member ? [item.member] : [])
|
||||
return occurrences.flatMap(({ member }) =>
|
||||
member.kind === 'node' ? [member.node] : [],
|
||||
)
|
||||
}),
|
||||
[renderList],
|
||||
)
|
||||
|
||||
const currentProxyChain = useMemo(
|
||||
() =>
|
||||
proxyView
|
||||
? rebindProxyChainItems(proxyChain, candidateNodes, proxyView)
|
||||
: proxyChain.map((item) => ({
|
||||
...item,
|
||||
recordId: undefined,
|
||||
delay: undefined,
|
||||
})),
|
||||
[candidateNodes, proxyChain, proxyView],
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (proxyChain.length > 0) {
|
||||
localStorage.setItem('proxy-chain-items', JSON.stringify(proxyChain))
|
||||
if (currentProxyChain.length > 0) {
|
||||
const persistedChain = currentProxyChain.map(
|
||||
({ id, name, type, delay }) => ({
|
||||
id,
|
||||
name,
|
||||
type,
|
||||
delay,
|
||||
}),
|
||||
)
|
||||
localStorage.setItem('proxy-chain-items', JSON.stringify(persistedChain))
|
||||
} else {
|
||||
localStorage.removeItem('proxy-chain-items')
|
||||
}
|
||||
}, [proxyChain])
|
||||
}, [currentProxyChain])
|
||||
|
||||
const [ruleMenuAnchor, setRuleMenuAnchor] = useState<null | HTMLElement>(null)
|
||||
const [duplicateWarning, setDuplicateWarning] = useState<{
|
||||
@@ -349,7 +377,7 @@ export function ProxyGroupsChain(props: ProxyGroupsChainProps) {
|
||||
if (!activeSelectedGroup) return null
|
||||
return (
|
||||
availableGroups.find(
|
||||
(group: any) => group.name === activeSelectedGroup,
|
||||
(group: ProxyGroupView) => group.name === activeSelectedGroup,
|
||||
) ?? null
|
||||
)
|
||||
}, [activeSelectedGroup, availableGroups])
|
||||
@@ -381,11 +409,19 @@ export function ProxyGroupsChain(props: ProxyGroupsChainProps) {
|
||||
}, [])
|
||||
|
||||
const handleChangeProxy = useCallback(
|
||||
(_group: IProxyGroupItem, proxy: IProxyItem) => {
|
||||
// 使用函数式更新来避免状态延迟问题
|
||||
(_group: ProxyGroupView, member: ResolvedProxyMember) => {
|
||||
if (!isInteractableMember(member) || member.kind !== 'node') return
|
||||
const { node } = member
|
||||
setProxyChain((prev) => {
|
||||
// 检查是否已经存在相同名称的代理,防止重复添加
|
||||
if (prev.some((item) => item.name === proxy.name)) {
|
||||
const current = proxyView
|
||||
? rebindProxyChainItems(prev, candidateNodes, proxyView)
|
||||
: prev
|
||||
if (
|
||||
current.some(
|
||||
(item) =>
|
||||
item.recordId !== undefined && item.recordId === node.recordId,
|
||||
)
|
||||
) {
|
||||
const warningMessage = t('proxies.page.chain.duplicateNode')
|
||||
setDuplicateWarning({
|
||||
open: true,
|
||||
@@ -396,21 +432,23 @@ export function ProxyGroupsChain(props: ProxyGroupsChainProps) {
|
||||
|
||||
// 安全获取延迟数据,如果没有延迟数据则设为 undefined
|
||||
const delay =
|
||||
proxy.history && proxy.history.length > 0
|
||||
? proxy.history[proxy.history.length - 1].delay
|
||||
node.history.length > 0
|
||||
? node.history[node.history.length - 1].delay
|
||||
: undefined
|
||||
|
||||
const chainItem: ProxyChainItem = {
|
||||
id: `${proxy.name}_${Date.now()}`,
|
||||
name: proxy.name,
|
||||
type: proxy.type,
|
||||
id: `${node.name}_${Date.now()}`,
|
||||
name: node.name,
|
||||
recordId: node.recordId,
|
||||
source: node.source,
|
||||
type: node.type,
|
||||
delay,
|
||||
}
|
||||
|
||||
return [...prev, chainItem]
|
||||
return [...current, chainItem]
|
||||
})
|
||||
},
|
||||
[t],
|
||||
[candidateNodes, proxyView, t],
|
||||
)
|
||||
|
||||
// Render virtual list for chain mode
|
||||
@@ -455,7 +493,7 @@ export function ProxyGroupsChain(props: ProxyGroupsChainProps) {
|
||||
|
||||
<Box sx={{ width: '400px', minWidth: '300px' }}>
|
||||
<ProxyChain
|
||||
proxyChain={proxyChain}
|
||||
proxyChain={currentProxyChain}
|
||||
onUpdateChain={setProxyChain}
|
||||
chainConfigData={chainConfigData}
|
||||
mode={mode}
|
||||
|
||||
Reference in New Issue
Block a user