diff --git a/src/components/proxy/proxy-chain-model.ts b/src/components/proxy/proxy-chain-model.ts index 90ed18e6e..728069240 100644 --- a/src/components/proxy/proxy-chain-model.ts +++ b/src/components/proxy/proxy-chain-model.ts @@ -3,6 +3,7 @@ import * as yaml from 'js-yaml' import { getRecord, rebindNode, + sameSource, type ProxyNodeView, type ProxyViewV1, } from '@/types/proxy-view' @@ -20,22 +21,47 @@ export const rebindProxyChainItems = ( items: readonly ProxyChainItem[], candidates: readonly ProxyNodeView[], proxyView: ProxyViewV1, -): ProxyChainItem[] => - items.map((item) => { +): ProxyChainItem[] => { + // The proxy view refreshes on a fixed cadence; reusing item and array + // identities while the rebound values are equal keeps persistence effects + // and memoized chain rows from reprocessing identical content. + let reboundItems: ProxyChainItem[] | undefined + items.forEach((item, index) => { const rebound = rebindNode(candidates, { name: item.name, source: item.source, }) const record = rebound === undefined ? undefined : getRecord(proxyView, rebound.recordId) - return { + const source = rebound?.source ?? item.source + const type = rebound?.type ?? item.type + const delay = record?.history.at(-1)?.delay + const sourceReused = + item.source === source || + (item.source !== undefined && + source !== undefined && + sameSource(item.source, source)) + + if ( + item.recordId === rebound?.recordId && + sourceReused && + item.type === type && + item.delay === delay + ) { + return + } + + reboundItems ??= [...items] + reboundItems[index] = { ...item, recordId: rebound?.recordId, - source: rebound?.source ?? item.source, - type: rebound?.type ?? item.type, - delay: record?.history.at(-1)?.delay, + source, + type, + delay, } }) + return reboundItems ?? (items as ProxyChainItem[]) +} interface ParsedChainConfig { proxies?: Array<{ diff --git a/src/components/proxy/proxy-groups-chain.tsx b/src/components/proxy/proxy-groups-chain.tsx index 5b11fe2b0..b2891f87a 100644 --- a/src/components/proxy/proxy-groups-chain.tsx +++ b/src/components/proxy/proxy-groups-chain.tsx @@ -93,7 +93,7 @@ interface ProxyGroupsChainProps { // Shared callbacks onCheckAll: (groupName: string) => void onHeadState: (groupName: string, patch: Partial) => void - onLocation: (group: any) => void + onLocation: (groupName: string) => void onGroupSelect: (groupName: string) => void onScrollToTop: () => void } @@ -245,7 +245,7 @@ function ProxyVirtualList({ activeStickyIndex: number | null isChainMode?: boolean measureElement: (node: Element | null) => void - onLocation: (group: any) => void + onLocation: (groupName: string) => void onCheckAll: (groupName: string) => void onHeadState: (groupName: string, patch: Partial) => void onChangeProxy: (group: ProxyGroupView, member: ResolvedProxyMember) => void diff --git a/src/components/proxy/proxy-groups.tsx b/src/components/proxy/proxy-groups.tsx index 11e357387..c097b7816 100644 --- a/src/components/proxy/proxy-groups.tsx +++ b/src/components/proxy/proxy-groups.tsx @@ -324,16 +324,20 @@ function ChainProxyGroups(props: { scrollTopRef.current = 0 }, []) - const handleLocation = useStableCallback((group: ProxyGroupView) => { - if (!group) return - const { name, now } = group + const handleLocation = useStableCallback((groupName: string) => { + // Flat (type 1) headers such as GLOBAL carry the same live group. + const headerIndex = renderList.findIndex( + (e) => (e.type === 0 || e.type === 1) && e.group?.name === groupName, + ) + const now = renderList[headerIndex]?.group?.now + if (!now) return const index = renderList.findIndex( - (item) => - item.group?.name === name && - ((item.type === 2 && item.member?.member.ref.name === now) || - (item.type === 4 && - item.memberCol?.some(({ member }) => member.ref.name === now))), + (e) => + e.group?.name === groupName && + ((e.type === 2 && e.member?.member.ref.name === now) || + (e.type === 4 && + e.memberCol?.some(({ member }) => member.ref.name === now))), ) if (index >= 0) { @@ -496,13 +500,17 @@ function NormalProxyGroups(props: { mode: string }) { [globalGroupName, select, setHeadState], ) - const handleLocation = useStableCallback((group: ProxyGroupView) => { - if (!group) return - const { name, now } = group + const handleLocation = useStableCallback((groupName: string) => { + // Flat (type 1) headers such as GLOBAL carry the same live group. + const headerIndex = renderList.findIndex( + (e) => (e.type === 0 || e.type === 1) && e.group?.name === groupName, + ) + const now = renderList[headerIndex]?.group?.now + if (!now) return const index = renderList.findIndex( (e) => - e.group?.name === name && + e.group?.name === groupName && ((e.type === 2 && e.member?.member.ref.name === now) || (e.type === 4 && e.memberCol?.some(({ member }) => member.ref.name === now))), @@ -516,21 +524,20 @@ function NormalProxyGroups(props: { mode: string }) { } }) - const handleGroupLocationByName = useCallback( - (groupName: string) => { - const index = renderList.findIndex( - (item) => item.type === 0 && item.group?.name === groupName, - ) + // Stable identity: this feeds the sticky-header render callback; a + // [renderList] dep would re-create it on every list rebuild. + const handleGroupLocationByName = useStableCallback((groupName: string) => { + const index = renderList.findIndex( + (item) => item.type === 0 && item.group?.name === groupName, + ) - if (index >= 0) { - stickyListRef.current?.scrollToIndex(index, { - align: 'start', - behavior: 'smooth', - }) - } - }, - [renderList], - ) + if (index >= 0) { + stickyListRef.current?.scrollToIndex(index, { + align: 'start', + behavior: 'smooth', + }) + } + }) const proxyGroupNames = useMemo(() => { const names = renderList diff --git a/src/components/proxy/use-render-list.ts b/src/components/proxy/use-render-list.ts index 116f73972..f1149c150 100644 --- a/src/components/proxy/use-render-list.ts +++ b/src/components/proxy/use-render-list.ts @@ -175,15 +175,19 @@ export const useRenderList = ( chainOccurrencesRef.current = chainOccurrences const chainDelayGroup = mode === 'rule' && selectedGroup ? selectedGroup : CHAIN_DELAY_GROUP - const chainDelayKey = chainOccurrences - .map(({ member }) => { - if (member.kind !== 'node') return `${member.kind}:${member.ref.name}` - const source = member.node.source - return source.kind === 'provider' - ? `provider:${source.providerName}:${source.proxyName}` - : `core:${source.proxyName}` - }) - .join('\u0000') + const chainDelayKey = useMemo( + () => + chainOccurrences + .map(({ member }) => { + if (member.kind !== 'node') return `${member.kind}:${member.ref.name}` + const source = member.node.source + return source.kind === 'provider' + ? `provider:${source.providerName}:${source.proxyName}` + : `core:${source.proxyName}` + }) + .join('\u0000'), + [chainOccurrences], + ) useEffect(() => { if (!isChainMode || !chainDelayKey) return diff --git a/src/types/proxy-view.ts b/src/types/proxy-view.ts index cebd34f4f..9b68ac6d2 100644 --- a/src/types/proxy-view.ts +++ b/src/types/proxy-view.ts @@ -156,7 +156,7 @@ export const selectRuleChainMembers = ( }) } -const sameSource = ( +export const sameSource = ( left: ProxyNodeView['source'], right: ProxyNodeView['source'], ) =>