refactor: remove state-only React effects (#8437)

This commit is contained in:
Neil
2026-07-12 17:11:08 -07:00
committed by GitHub
parent d197c5b864
commit fb15d647c6
10 changed files with 347 additions and 23 deletions
@@ -105,6 +105,13 @@ export function LinearAgentSkillSetupPrompt({
const [localDismissed, setLocalDismissed] = useState(() =>
readLocalDismissed(localDismissStorageKey)
)
const [previousDismissStorageKey, setPreviousDismissStorageKey] = useState(localDismissStorageKey)
// Why: dismissal is scoped to the selected runtime, so read the new key before
// paint rather than briefly showing the previous runtime's prompt state.
if (localDismissStorageKey !== previousDismissStorageKey) {
setPreviousDismissStorageKey(localDismissStorageKey)
setLocalDismissed(readLocalDismissed(localDismissStorageKey))
}
const skill = useInstalledAgentSkillNames(LINEAR_AGENT_SKILL_NAMES, {
enabled: linked,
discoveryTarget: skillDiscoveryTarget,
@@ -127,10 +134,6 @@ export function LinearAgentSkillSetupPrompt({
settings,
agentRuntime
)
useEffect(() => {
setLocalDismissed(readLocalDismissed(localDismissStorageKey))
}, [localDismissStorageKey])
const writeCliStatusIfCurrent = useCallback(
(requestIdentity: string, requestGeneration: number, write: () => void): void => {
if (
@@ -88,6 +88,26 @@ afterEach(() => {
})
describe('TabBarCreateEntry keyboard navigation', () => {
it('publishes the query from the typing event without an extra effect commit', () => {
const onQueryChange = vi.fn()
mount(
<TabBarCreateEntry
worktreeId="wt"
groupId="g"
menuOpen
onOpenEntry={vi.fn()}
onQueryChange={onQueryChange}
/>
)
expect(onQueryChange).not.toHaveBeenCalled()
setQuery('src/app.ts')
expect(onQueryChange).toHaveBeenCalledTimes(1)
expect(onQueryChange).toHaveBeenCalledWith('src/app.ts')
})
it('intercepts ArrowDown on a single-option list so it does not leak (guards >0 vs >1)', () => {
entryOptionsMock.options = [fileOption('src/only-match.ts')]
const onOpenEntry = vi.fn().mockResolvedValue(undefined)
@@ -117,10 +117,6 @@ export default function TabBarCreateEntry({
[agentOptions, query]
)
useEffect(() => {
onQueryChange?.(query)
}, [onQueryChange, query])
if (selectedIndexQuery !== query) {
setSelectedIndexQuery(query)
if (selectedIndex !== 0) {
@@ -249,7 +245,11 @@ export default function TabBarCreateEntry({
ref={inputRef}
value={query}
onChange={(event) => {
setQuery(event.target.value)
const nextQuery = event.target.value
// Why: the parent query only changes in response to typing, so publish
// it in this event rather than a later effect after the render commits.
setQuery(nextQuery)
onQueryChange?.(nextQuery)
setError(null)
}}
disabled={disabled}
@@ -88,4 +88,32 @@ describe('CloseTerminalDialog', () => {
expect(onConfirm).toHaveBeenCalledWith(true)
})
it('resets the skip preference when the dialog closes and reopens', async () => {
const onConfirm = vi.fn()
const onCancel = vi.fn()
const container = document.createElement('div')
document.body.appendChild(container)
const root = createRoot(container)
mountedRoots.push(root)
const render = async (open: boolean): Promise<void> => {
await act(async () => {
root.render(<CloseTerminalDialog open={open} onCancel={onCancel} onConfirm={onConfirm} />)
})
}
await render(true)
const checkbox = document.body.querySelector<HTMLButtonElement>('[role="checkbox"]')
await act(async () => {
checkbox?.click()
})
expect(checkbox?.getAttribute('aria-checked')).toBe('true')
await render(false)
await render(true)
expect(document.body.querySelector('[role="checkbox"]')?.getAttribute('aria-checked')).toBe(
'false'
)
})
})
@@ -1,4 +1,4 @@
import { useEffect, useId, useState } from 'react'
import { useId, useState } from 'react'
import {
Dialog,
DialogContent,
@@ -27,12 +27,16 @@ export default function CloseTerminalDialog({
}): React.JSX.Element {
const checkboxId = useId()
const [dontAskAgain, setDontAskAgain] = useState(false)
const [previousOpen, setPreviousOpen] = useState(open)
useEffect(() => {
// Why: each reopen represents a fresh confirmation, so clear the old choice
// during render rather than briefly painting it while the dialog opens.
if (open !== previousOpen) {
setPreviousOpen(open)
if (open) {
setDontAskAgain(false)
}
}, [open])
}
const isAgent = copyKind === 'agent'
@@ -1,4 +1,4 @@
import { useEffect, useId, useState } from 'react'
import { useId, useState } from 'react'
import {
Dialog,
DialogContent,
@@ -23,14 +23,18 @@ export default function PinnedTabCloseDialog(): React.JSX.Element {
const dismissPinnedTabClose = useAppStore((state) => state.dismissPinnedTabClose)
const updateSettings = useAppStore((state) => state.updateSettings)
const [dontAskAgain, setDontAskAgain] = useState(false)
const [previousRequest, setPreviousRequest] = useState(request)
const tabLabel = request?.tabLabel.trim()
useEffect(() => {
// Why: a new store request is a new confirmation, so reset its checkbox before
// paint while keeping a cancelled request's state inert until the next open.
if (request !== previousRequest) {
setPreviousRequest(request)
if (request !== null) {
setDontAskAgain(false)
}
}, [request])
}
const handleConfirm = (): void => {
if (dontAskAgain) {