From 01e28608efcde44771f0510b78ab601f9e0e7b52 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:45:19 +0800 Subject: [PATCH] perf(profile): derive editor policy names and dedupe pasted proxies The groups editor re-read both profile files and re-parsed three YAML documents on every prepend/append/delete edit. The origin names are now read once per open and folded into a memoized policy list, and batch pasted proxy names dedupe through a Set instead of a linear scan. --- .../profile/groups-editor-viewer.tsx | 34 +++++++++++++------ .../profile/proxies-editor-viewer.tsx | 6 ++-- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/components/profile/groups-editor-viewer.tsx b/src/components/profile/groups-editor-viewer.tsx index ddacd7971..f1c1f16af 100644 --- a/src/components/profile/groups-editor-viewer.tsx +++ b/src/components/profile/groups-editor-viewer.tsx @@ -176,7 +176,10 @@ export const GroupsEditorViewer = (props: Props) => { }, }) const [groupList, setGroupList] = useState([]) - const [proxyPolicyList, setProxyPolicyList] = useState([]) + const [originPolicy, setOriginPolicy] = useState<{ + proxyNames: string[] + groupNames: string[] + }>({ proxyNames: [], groupNames: [] }) const [proxyProviderList, setProxyProviderList] = useState([]) const [prependSeq, setPrependSeq] = useState([]) const [appendSeq, setAppendSeq] = useState([]) @@ -353,6 +356,9 @@ export const GroupsEditorViewer = (props: Props) => { } }, [prependSeq, appendSeq, deleteSeq]) + // Origin policy names come from profile files that this dialog never + // rewrites while open, so they are read once per open; the live seq states + // are folded in by the derived proxyPolicyList below. const fetchProxyPolicy = useCallback(async () => { const data = await readProfileFile(profileUid) const proxiesData = await readProfileFile(proxiesUid) @@ -388,17 +394,13 @@ export const GroupsEditorViewer = (props: Props) => { (name): name is string => typeof name === 'string' && name.length > 0, ) - const computedPolicyList = builtinProxyPolicies.concat( - prependSeq.map((group: IProxyGroupConfig) => group.name), - (originGroupsObj?.['proxy-groups'] || []) - .map((group: IProxyGroupConfig) => group.name) - .filter((name) => !deleteSeq.includes(name)), - appendSeq.map((group: IProxyGroupConfig) => group.name), + setOriginPolicy({ proxyNames, - ) - - setProxyPolicyList(Array.from(new Set(computedPolicyList))) - }, [appendSeq, deleteSeq, prependSeq, profileUid, proxiesUid]) + groupNames: (originGroupsObj?.['proxy-groups'] || []).map( + (group) => group.name, + ), + }) + }, [profileUid, proxiesUid]) const fetchProfile = useCallback(async () => { const data = await readProfileFile(profileUid) const mergeData = await readProfileFile(mergeUid) @@ -442,6 +444,16 @@ export const GroupsEditorViewer = (props: Props) => { fetchProxyPolicy() }, [fetchProxyPolicy, open]) + const proxyPolicyList = useMemo(() => { + const computedPolicyList = builtinProxyPolicies.concat( + prependSeq.map((group) => group.name), + originPolicy.groupNames.filter((name) => !deleteSeq.includes(name)), + appendSeq.map((group) => group.name), + originPolicy.proxyNames, + ) + return Array.from(new Set(computedPolicyList)) + }, [appendSeq, deleteSeq, originPolicy, prependSeq]) + useEffect(() => { if (!open) return fetchContent() diff --git a/src/components/profile/proxies-editor-viewer.tsx b/src/components/profile/proxies-editor-viewer.tsx index 23fc0398e..6823cddcd 100644 --- a/src/components/profile/proxies-editor-viewer.tsx +++ b/src/components/profile/proxies-editor-viewer.tsx @@ -180,7 +180,7 @@ export const ProxiesEditorViewer = (props: Props) => { // 优化:异步分片解析,避免主线程阻塞,解析完成后批量setState const handleParseAsync = (cb: (proxies: IProxyConfig[]) => void) => { const proxies: IProxyConfig[] = [] - const names: string[] = [] + const names = new Set() let uris: string try { uris = atob(proxyUri) @@ -198,9 +198,9 @@ export const ProxiesEditorViewer = (props: Props) => { const uri = lines[idx] try { const proxy = parseUri(uri.trim()) - if (!names.includes(proxy.name)) { + if (!names.has(proxy.name)) { proxies.push(proxy) - names.push(proxy.name) + names.add(proxy.name) } } catch (err) { console.warn(