Skip to content

The agent that grows with you

feat(desktop): add confirm() as the imperative front door to ConfirmDialog

Brooklyn Nicholson21198d940167
diff --git a/apps/desktop/src/app/contrib/wiring.tsx b/apps/desktop/src/app/contrib/wiring.tsx
index f996529eeb6..1b418400ea8 100644
--- a/apps/desktop/src/app/contrib/wiring.tsx
+++ b/apps/desktop/src/app/contrib/wiring.tsx
@@ -16,6 +16,7 @@ import { useLocation, useNavigate } from 'react-router'
import { graftRefreshedTailOntoBackfill } from '@/app/chat/transcript-backfill'
import { formatRefValue } from '@/components/assistant-ui/directive-text'
import { BootFailureOverlay } from '@/components/boot-failure-overlay'
+import { ConfirmHost } from '@/components/confirm-host'
import { DesktopInstallOverlay } from '@/components/desktop-install-overlay'
import { FindBar } from '@/components/find-bar'
import { GatewayConnectingOverlay } from '@/components/gateway-connecting-overlay'
@@ -1177,6 +1178,9 @@ export function ContribWiring({ children }: { children: ReactNode }) {
{/* Toasts above everything. */}
<NotificationStack />
+ {/* Backs confirm() from @/store/confirm — renders only while one is open. */}
+ <ConfirmHost />
+
{/* Petdex floating mascot — renders nothing unless installed + enabled.
Never in the HUD: that window is the chat bar and nothing else. */}
{!isHudWindow() && <FloatingPet />}
diff --git a/apps/desktop/src/components/confirm-host.test.tsx b/apps/desktop/src/components/confirm-host.test.tsx
new file mode 100644
index 00000000000..0aeff59273e
--- /dev/null
+++ b/apps/desktop/src/components/confirm-host.test.tsx
@@ -0,0 +1,81 @@
+import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
+import { afterEach, describe, expect, it } from 'vitest'
+
+import { $confirmRequest, confirm } from '@/store/confirm'
+
+import { ConfirmHost } from './confirm-host'
+
+afterEach(() => {
+ cleanup()
+ $confirmRequest.set(null)
+})
+
+/** Open a confirm and wait for the dialog, without awaiting the answer. */
+async function ask(title = 'Delete it?') {
+ let answer: boolean | undefined
+ const pending = confirm({ title }).then(ok => (answer = ok))
+
+ const dialog = await screen.findByRole('dialog')
+
+ return { dialog, pending, read: () => answer }
+}
+
+describe('confirm()', () => {
+ it('renders nothing until something asks', () => {
+ render(<ConfirmHost />)
+ expect(screen.queryByRole('dialog')).toBeNull()
+ })
+
+ it('resolves true when confirmed and false when cancelled', async () => {
+ render(<ConfirmHost />)
+
+ const yes = await ask()
+ fireEvent.click(screen.getByRole('button', { name: /confirm/i }))
+ await yes.pending
+ expect(yes.read()).toBe(true)
+
+ const no = await ask()
+ fireEvent.click(screen.getByRole('button', { name: /cancel/i }))
+ await no.pending
+ expect(no.read()).toBe(false)
+ })
+
+ it('confirms on Enter from wherever focus landed', async () => {
+ render(<ConfirmHost />)
+
+ const { dialog, pending, read } = await ask()
+
+ // eslint-disable-next-line no-restricted-globals -- asserting real focus requires the live document
+ await waitFor(() => expect(dialog.contains(document.activeElement)).toBe(true))
+ // eslint-disable-next-line no-restricted-globals -- asserting real focus requires the live document
+ fireEvent.keyDown(document.activeElement!, { key: 'Enter' })
+
+ await pending
+ expect(read()).toBe(true)
+ })
+
+ it('answers no to Escape', async () => {
+ render(<ConfirmHost />)
+
+ const { pending, read } = await ask()
+ fireEvent.keyDown(window.document, { key: 'Escape' })
+
+ await pending
+ expect(read()).toBe(false)
+ })
+
+ it('supersedes an open request, answering the one it replaces no', async () => {
+ render(<ConfirmHost />)
+
+ const first = await ask('First?')
+ const second = await act(async () => ask('Second?'))
+
+ await first.pending
+ expect(first.read()).toBe(false)
+ expect(screen.getByText('Second?')).toBeTruthy()
+
+ fireEvent.click(screen.getByRole('button', { name: /confirm/i }))
+ await second.pending
+ expect(second.read()).toBe(true)
+ })
+})
diff --git a/apps/desktop/src/components/confirm-host.tsx b/apps/desktop/src/components/confirm-host.tsx
new file mode 100644
index 00000000000..2b6f8752b97
--- /dev/null
+++ b/apps/desktop/src/components/confirm-host.tsx
@@ -0,0 +1,40 @@
+import { useStore } from '@nanostores/react'
+import { useEffect, useState } from 'react'
+
+import { ConfirmDialog } from '@/components/ui/confirm-dialog'
+import { $confirmRequest, type PendingConfirm, settleConfirm } from '@/store/confirm'
+
+// The one mount point for `confirm()` from @/store/confirm. Mounted once at the
+// shell, the way NotificationStack backs notify().
+export function ConfirmHost() {
+ const request = useStore($confirmRequest)
+ // The atom clears the moment the question is answered, but Radix still has a
+ // close animation to play — hold the copy so the dialog doesn't blank mid-fade.
+ const [shown, setShown] = useState<null | PendingConfirm>(request)
+
+ useEffect(() => {
+ if (request) {
+ setShown(request)
+ }
+ }, [request])
+
+ if (!shown) {
+ return null
+ }
+
+ return (
+ <ConfirmDialog
+ cancelLabel={shown.cancelLabel}
+ confirmLabel={shown.confirmLabel}
+ description={shown.description}
+ destructive={shown.destructive}
+ // The caller does the work once it has its answer, so there is nothing
+ // here to keep the dialog open for.
+ dismissOnConfirm
+ onClose={() => settleConfirm(false)}
+ onConfirm={() => settleConfirm(true)}
+ open={request !== null}
+ title={shown.title}
+ />
+ )
+}
diff --git a/apps/desktop/src/store/confirm.ts b/apps/desktop/src/store/confirm.ts
new file mode 100644
index 00000000000..6fd6fa46d0d
--- /dev/null
+++ b/apps/desktop/src/store/confirm.ts
@@ -0,0 +1,40 @@
+import { atom } from 'nanostores'
+
+export interface ConfirmRequest {
+ title: string
+ description?: string
+ confirmLabel?: string
+ cancelLabel?: string
+ destructive?: boolean
+}
+
+export interface PendingConfirm extends ConfirmRequest {
+ resolve: (confirmed: boolean) => void
+}
+
+export const $confirmRequest = atom<null | PendingConfirm>(null)
+
+// Imperative front door to ConfirmDialog, for handlers that want the answer
+// inline the way window.confirm gave it — `if (!ok) return`. A surface that
+// wants the busy → done beat or an inline error should mount <ConfirmDialog>
+// itself and hand it the async onConfirm.
+export function confirm(request: ConfirmRequest): Promise<boolean> {
+ // One modal at a time: a second ask supersedes the first, which answers no.
+ settleConfirm(false)
+
+ return new Promise<boolean>(resolve => {
+ $confirmRequest.set({ ...request, resolve })
+ })
+}
+
+/** Answer the open request, if there still is one. Idempotent. */
+export function settleConfirm(confirmed: boolean): void {
+ const pending = $confirmRequest.get()
+
+ if (!pending) {
+ return
+ }
+
+ $confirmRequest.set(null)
+ pending.resolve(confirmed)
+}