Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/renderer/src/components/settings/ProxyPoolSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined>()
const [confirmRemoveId, setConfirmRemoveId] = useState<string | undefined>()
const ruleSummary = useAutoRuleSummary()
const settingsRef = useRef<HTMLDivElement>(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<HTMLDivElement>(null)
const showAccountsOf = (poolId: string) => {
setFilterPoolId(poolId)
settingsRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' })
Expand Down
54 changes: 54 additions & 0 deletions src/renderer/src/hooksOrder.test.ts
Original file line number Diff line number Diff line change
@@ -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<string>()', ' 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([])
})
})