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
45 changes: 45 additions & 0 deletions src/renderer/src/components/settings/EphemeralVmsPane.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,51 @@ describe('EphemeralVmsPane', () => {
})
})

it('does not schedule a reset when clipboard completion arrives after unmount', async () => {
let finishClipboard!: () => void
vi.mocked(window.api.ui.writeClipboardText).mockReturnValueOnce(
new Promise<void>((resolve) => {
finishClipboard = resolve
})
)
const container = await renderPane()
const setTimeout = vi.spyOn(window, 'setTimeout')
try {
await act(async () => {
container.querySelector<HTMLButtonElement>('button[aria-label="Copy"]')?.click()
})
await act(async () => roots.pop()?.unmount())
setTimeout.mockClear()
await act(async () => {
finishClipboard()
await Promise.resolve()
})
expect(setTimeout.mock.calls.filter(([, delay]) => delay === 1500)).toHaveLength(0)
} finally {
setTimeout.mockRestore()
}
})

it('shows copied feedback while mounted and releases its reset on unmount', async () => {
const container = await renderPane()
const setTimeout = vi.spyOn(window, 'setTimeout')
const clearTimeout = vi.spyOn(window, 'clearTimeout')
try {
await act(async () => {
container.querySelector<HTMLButtonElement>('button[aria-label="Copy"]')?.click()
})
expect(container.querySelector('button[aria-label="Copy"]')?.textContent).toBe('Copied')
const timerIndex = setTimeout.mock.calls.findIndex(([, delay]) => delay === 1500)
expect(timerIndex).toBeGreaterThanOrEqual(0)
const timer = setTimeout.mock.results[timerIndex].value
await act(async () => roots.pop()?.unmount())
expect(clearTimeout).toHaveBeenCalledWith(timer)
} finally {
setTimeout.mockRestore()
clearTimeout.mockRestore()
}
})

it('refreshes the catalog when plugin content changes', async () => {
const listRecipeCatalog = window.api.ephemeralVm.listRecipeCatalog as ReturnType<typeof vi.fn>
const container = await renderPane()
Expand Down
20 changes: 19 additions & 1 deletion src/renderer/src/components/settings/EphemeralVmsPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ export function EphemeralVmsPane(): React.JSX.Element {
const [promptCopied, setPromptCopied] = useState(false)
const mountedRef = useMountedRef()
const refreshGenerationRef = useRef(0)
const promptResetTimerRef = useRef<number | null>(null)

useEffect(() => {
return () => {
if (promptResetTimerRef.current !== null) {
window.clearTimeout(promptResetTimerRef.current)
}
}
}, [])

// Why: an absent runtime still resolves to the local host, which is what the
// seven sibling panes rely on to reach the Windows npx preflight.
Expand Down Expand Up @@ -126,8 +135,17 @@ export function EphemeralVmsPane(): React.JSX.Element {
try {
await window.api.ui.writeClipboardText(AGENT_PROMPT)
useAppStore.getState().recordFeatureInteraction('ephemeral-vm-setup')
if (!mountedRef.current) {
return
}
setPromptCopied(true)
setTimeout(() => setPromptCopied(false), 1500)
if (promptResetTimerRef.current !== null) {
window.clearTimeout(promptResetTimerRef.current)
}
promptResetTimerRef.current = window.setTimeout(() => {
promptResetTimerRef.current = null
setPromptCopied(false)
}, 1500)
Comment on lines +142 to +148

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reimplements the timer coalescing and unmount-safe reset that useClipboardTextCopyFeedback already provides (and tests). This button is the same inline copy affordance the hook was written for, so reuse would drop promptResetTimerRef, the new effect, and this manual timer.

Technical details
# Reuse `useClipboardTextCopyFeedback` for the copy-prompt button

## Affected sites
- `src/renderer/src/components/settings/EphemeralVmsPane.tsx:48` — new `promptResetTimerRef`
- `src/renderer/src/components/settings/EphemeralVmsPane.tsx:50-56` — new unmount cleanup effect
- `src/renderer/src/components/settings/EphemeralVmsPane.tsx:139-145` — manual clear/reschedule timer
- `src/renderer/src/hooks/use-clipboard-text-copy-feedback.ts` — existing hook; `scheduleReset` coalesces and `isMountedRef` guards setState after unmount (covered by `use-clipboard-text-copy-feedback.test.ts`)

## Required outcome
- One clipboard-feedback implementation for this button, preserving the `recordFeatureInteraction('ephemeral-vm-setup')` call on success and the `toast.error` on failure.

## Suggested approach (optional)
- `const { copyText, status } = useClipboardTextCopyFeedback(AGENT_PROMPT)`; in `copyPrompt`, `if (await copyText()) recordFeatureInteraction(...) else toast.error(...)`; render on `status === 'copied'`.

} catch {
toast.error(
translate(
Expand Down
Loading