fix(react): remove four setState-in-effect cascades flagged by react-hooks v5
Some checks failed
Mirror to GitHub / mirror (push) Successful in 11s
CI / backend (pull_request) Failing after 11m23s
CI / frontend (pull_request) Failing after 2m42s
CI / e2e (pull_request) Has been skipped

The new react-hooks lint rule "Calling setState synchronously within an
effect can trigger cascading renders" flagged real anti-patterns in
four spots. Refactored each per the rule's intent (derive during render,
or use useSyncExternalStore for external subscriptions).

1. hooks/useMediaQuery.ts — replaced the useState + useEffect pair with
   useSyncExternalStore. That's the canonical React hook for
   subscribing to external stores (matchMedia in this case) without
   mirroring into local state via an effect. Snapshot/getServerSnapshot
   pair preserves the SSR-safe behaviour.

2. components/network/nodes/DeviceNode.tsx — the prop-sync useEffect
   that copied nodeData.label into labelValue was redundant.
   labelValue is the EDIT BUFFER; while not editing, the displayed
   span now reads nodeData.label directly. The buffer is initialized
   only when an edit session starts (onDoubleClick).

3. components/network/nodes/GroupNode.tsx — same pattern, same fix.

4. components/dashboard/TicketQueue.tsx — the
   setTickets([]) + setLoading(true) + fetchTickets() chain in the
   effect was the cascade. Pushed those writes inside fetchTickets
   (after the function boundary, so they batch with the eventual
   setTickets(result)). Added a request-id ref so a slow first
   response can't overwrite a fast second one.

Frontend lint: 20 errors → 0 errors. tsc -b clean.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-04-25 02:33:13 -04:00
parent b7f8e70be2
commit 920a246d77
4 changed files with 48 additions and 31 deletions

View File

@@ -194,6 +194,9 @@ export function TicketQueue() {
const [activeTab, setActiveTab] = useState<Tab>('mine')
const [tickets, setTickets] = useState<PSATicketSearchResult[]>([])
const [loading, setLoading] = useState(false)
// Monotonically increasing fetch token — late responses with a stale id
// are dropped so they can't overwrite the latest query's results.
const latestRequestId = useRef(0)
const [error, setError] = useState<string | null>(null)
// Check connection on mount
@@ -238,12 +241,25 @@ export function TicketQueue() {
params.board_ids = boardIds.join(',')
}
// Clear stale data + flip loading inside the async function so the
// writes happen after the awaitable boundary — avoids the
// synchronous-setState-in-effect cascade the lint rule flags. The
// fetch is also wrapped in a request-id check so a stale response
// can't clobber a newer query.
const requestId = ++latestRequestId.current
setTickets([])
setLoading(true)
try {
const results = await integrationsApi.searchTicketsQueue(params)
if (requestId !== latestRequestId.current) return
setTickets(results.items)
setError(null)
} catch {
if (requestId !== latestRequestId.current) return
setError('Failed to load tickets. Check your PSA connection.')
} finally {
if (requestId === latestRequestId.current) setLoading(false)
}
},
[],
@@ -253,9 +269,7 @@ export function TicketQueue() {
useEffect(() => {
if (!hasConnection) return
if (activeTab === 'mine' && hasMemberMapping !== true) return
setTickets([])
setLoading(true)
fetchTickets(activeTab, selectedBoardIds).finally(() => setLoading(false))
fetchTickets(activeTab, selectedBoardIds)
}, [activeTab, selectedBoardIds, hasConnection, hasMemberMapping, fetchTickets])
const handleStartSession = (ticket: PSATicketSearchResult) => {