From b4c4e316a9aad1c47ab0e80b2cf0363b93c06e03 Mon Sep 17 00:00:00 2001 From: GOXF <131693427+GOXF@users.noreply.github.com> Date: Thu, 3 Dec 2026 03:11:21 +0000 Subject: [PATCH] =?UTF-8?q?fix(proxy-pool):=20=E4=BF=AE=E5=A4=8D=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E6=B1=A0=E9=A1=B5=20React=20#310=EF=BC=88=E6=8F=90?= =?UTF-8?q?=E5=89=8D=20return=20=E5=90=8E=E8=BF=98=E8=B0=83=E7=94=A8=20hoo?= =?UTF-8?q?k=EF=BC=89=E5=B9=B6=E5=8A=A0=20hooks=20=E9=A1=BA=E5=BA=8F?= =?UTF-8?q?=E5=AE=88=E5=8D=AB=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/settings/ProxyPoolSection.tsx | 8 +-- src/renderer/src/hooksOrder.test.ts | 54 +++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/renderer/src/hooksOrder.test.ts diff --git a/src/renderer/src/components/settings/ProxyPoolSection.tsx b/src/renderer/src/components/settings/ProxyPoolSection.tsx index 56388cea..102f53cc 100644 --- a/src/renderer/src/components/settings/ProxyPoolSection.tsx +++ b/src/renderer/src/components/settings/ProxyPoolSection.tsx @@ -157,15 +157,17 @@ export function ProxyPoolSection({ setTestingAll(false) }, [credentialRevision]) - if (!active) return null - + // GOXF fix: every hook must run before the early return (React #310). const [filterPoolId, setFilterPoolId] = useState() const [confirmRemoveId, setConfirmRemoveId] = useState() const ruleSummary = useAutoRuleSummary() + const settingsRef = useRef(null) + + if (!active) return null + const defaultChosen = pool.find((p) => p.id === defaultProxy.poolId) const defaultLabel = defaultProxy.value.choice === 'direct' ? '直连' : defaultChosen ? poolShortLabel(defaultChosen, pool.indexOf(defaultChosen)) : '系统代理' - const settingsRef = useRef(null) const showAccountsOf = (poolId: string) => { setFilterPoolId(poolId) settingsRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' }) diff --git a/src/renderer/src/hooksOrder.test.ts b/src/renderer/src/hooksOrder.test.ts new file mode 100644 index 00000000..ce524332 --- /dev/null +++ b/src/renderer/src/hooksOrder.test.ts @@ -0,0 +1,54 @@ +// GOXF: guard against React #310 ("Rendered more hooks than during the previous render"). +// Flags a top-level component whose body calls a hook after an early `if (...) return`. +// Heuristic (indent-based, 2-space top-level body); kept simple on purpose. +import { readdirSync, readFileSync, statSync } from 'node:fs' +import { join, relative } from 'node:path' +import { describe, expect, it } from 'vitest' + +const ROOT = join(__dirname) +const COMPONENT_START = /^(export (default )?)?function [A-Z]\w*\(|^export const [A-Z]\w* = / +const EARLY_RETURN = /^ {2}if \(.*\) return\b/ +const FINAL_RETURN = /^ {2}return\b/ +const HOOK_CALL = /^ {2}(?:(?:const|let)\s[^=]*=\s*)?use[A-Z]\w*[<(]/ + +function listTsx(dir: string, out: string[] = []): string[] { + for (const name of readdirSync(dir)) { + if (name === 'node_modules' || name === 'mock') continue + const full = join(dir, name) + if (statSync(full).isDirectory()) listTsx(full, out) + else if (name.endsWith('.tsx')) out.push(full) + } + return out +} + +export function findHooksAfterEarlyReturn(source: string): number[] { + const lines = source.split('\n') + const hits: number[] = [] + for (let i = 0; i < lines.length; i++) { + if (!COMPONENT_START.test(lines[i])) continue + let earlyReturn = -1 + let j = i + 1 + for (; j < lines.length && lines[j].trimEnd() !== '}'; j++) { + const line = lines[j] + if (earlyReturn < 0 && EARLY_RETURN.test(line)) earlyReturn = j + else if (earlyReturn >= 0 && FINAL_RETURN.test(line)) break + else if (earlyReturn >= 0 && HOOK_CALL.test(line)) hits.push(j + 1) + } + i = j + } + return hits +} + +describe('hooks order', () => { + it('detects a hook after an early return', () => { + const bad = ['export function A({ on }) {', ' const x = useT()', ' if (!on) return null', ' const [v] = useState()', ' return null', '}'].join('\n') + expect(findHooksAfterEarlyReturn(bad)).toEqual([4]) + }) + + it('no renderer component calls a hook after an early return', () => { + const offenders = listTsx(ROOT).flatMap((file) => + findHooksAfterEarlyReturn(readFileSync(file, 'utf8')).map((line) => `${relative(ROOT, file)}:${line}`) + ) + expect(offenders).toEqual([]) + }) +})