diff --git a/src/renderer/src/components/settings/ProxyPoolSection.tsx b/src/renderer/src/components/settings/ProxyPoolSection.tsx index 56388ce..102f53c 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 0000000..ce52433 --- /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([]) + }) +})