Skip to content

The agent that grows with you

fix(desktop): a layout pick stays picked when its preview remounts the card

alt-glitch4ee9227d98e5
diff --git a/apps/desktop/src/components/assistant-ui/theme-picker/look-preview.ts b/apps/desktop/src/components/assistant-ui/theme-picker/look-preview.ts
index d55bf8f8d20..b4ade706865 100644
--- a/apps/desktop/src/components/assistant-ui/theme-picker/look-preview.ts
+++ b/apps/desktop/src/components/assistant-ui/theme-picker/look-preview.ts
@@ -1,4 +1,4 @@
-import { useEffect, useRef, useState } from 'react'
+import { useEffect, useState } from 'react'
import { type PreviewUndo, snapshotChatLayout } from '@/components/onboarding/first-run/assembly'
import { applyDesktopLayoutPreset } from '@/store/pane-focus'
@@ -44,43 +44,79 @@ export interface LookPreview {
staged: null | StagedLook
}
+/** A look's open preview: the staged pick and the value from before the first preview. */
+interface Held {
+ mounts: number
+ settled: boolean
+ staged: null | StagedLook
+ undo: null | PreviewUndo
+}
+
+// A layout preview re-parents the chat, which remounts the card that staged it. The preview outlives the
+// card instance, so the remounted card shows the pick, and only a card that stays gone puts the old look back.
+const held = new Map<'accent' | 'layout', Held>()
+
/** Click previews a look live; a card that unmounts without Continue (cancel, timeout, Skip setup,
* words in the composer) puts back the value from before the preview. */
export function useLookPreview(kind: 'accent' | 'layout', dark: boolean): LookPreview {
const live = kind === 'accent' ? ACCENT : LAYOUT
- const [staged, setStaged] = useState<null | StagedLook>(null)
- // The value from before this card's first preview.
- const undo = useRef<null | PreviewUndo>(null)
- const settled = useRef(false)
-
- useEffect(
- () => () => {
- if (!settled.current) {
- undo.current?.revert()
+ const [staged, setStaged] = useState<null | StagedLook>(() => held.get(kind)?.staged ?? null)
+
+ useEffect(() => {
+ const entry = held.get(kind)
+
+ if (entry) {
+ entry.mounts += 1
+ }
+
+ return () => {
+ const left = held.get(kind)
+
+ if (!left) {
+ return
}
- },
- []
- )
+
+ left.mounts -= 1
+ // A remount mounts the new card in the same commit; wait past it before calling the card gone.
+ setTimeout(() => {
+ if (held.get(kind) === left && left.mounts === 0) {
+ held.delete(kind)
+
+ if (!left.settled) {
+ left.undo?.revert()
+ }
+ }
+ })
+ }
+ }, [kind])
return {
// Settled before sending: the answer can unmount the card before `send` returns.
commit: send => {
- if (!staged) {
+ const entry = held.get(kind)
+
+ if (!staged || !entry) {
return
}
- settled.current = true
- settled.current = undo.current ? undo.current.keep(() => send(staged.value)) : send(staged.value)
+ entry.settled = true
+ entry.settled = entry.undo ? entry.undo.keep(() => send(staged.value)) : send(staged.value)
- // A failed send keeps the card, so its preview goes back on screen.
- if (!settled.current && undo.current) {
+ // The sent value is the new truth. A failed send keeps the card, so its preview goes back on screen.
+ if (entry.settled) {
+ held.delete(kind)
+ } else if (entry.undo) {
live.apply(staged.value, dark)
}
},
stage: value => {
- undo.current ??= live.snapshot()
+ const entry = held.get(kind) ?? { mounts: 1, settled: false, staged: null, undo: null }
+
+ held.set(kind, entry)
+ entry.undo ??= live.snapshot()
+ entry.staged = { value }
live.apply(value, dark)
- setStaged({ value })
+ setStaged(entry.staged)
},
staged
}
diff --git a/apps/desktop/src/components/assistant-ui/theme-picker/theme-tool.test.tsx b/apps/desktop/src/components/assistant-ui/theme-picker/theme-tool.test.tsx
index f42bbf993d0..045ad167de7 100644
--- a/apps/desktop/src/components/assistant-ui/theme-picker/theme-tool.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/theme-picker/theme-tool.test.tsx
@@ -1,6 +1,7 @@
import type { ToolCallMessagePartProps } from '@assistant-ui/react'
-import { cleanup, fireEvent, render, screen } from '@testing-library/react'
+import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { atom } from 'nanostores'
+import { Fragment } from 'react'
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'
import { PRIMARY_SESSION_VIEW, type SessionView, SessionViewProvider } from '@/app/chat/session-view'
@@ -72,7 +73,7 @@ describe('ThemeTool', () => {
expect(screen.getByText('Ultraviolet').closest('[data-theme-settled]')).not.toBeNull()
})
- it('puts the previous accent back when the card settles without a pick', () => {
+ it('puts the previous accent back when the card settles without a pick', async () => {
const respond = park('accent')
const { rerender } = renderRow({ args: { picker: 'accent' } })
@@ -82,7 +83,7 @@ describe('ThemeTool', () => {
rerender(row({ args: { picker: 'accent' }, result: { cancelled: true } }))
expect(respond).not.toHaveBeenCalled()
- expect($accentOverride.get()).toBeNull()
+ await waitFor(() => expect($accentOverride.get()).toBeNull())
})
it('draws the layout card for a call made through the tool_call bridge', () => {
@@ -100,4 +101,20 @@ describe('ThemeTool', () => {
expect(respond).toHaveBeenCalledWith({ picked: { layout: 'terminal-deck' } })
})
+
+ it('keeps a staged layout when its preview remounts the card', () => {
+ const respond = park('layout')
+ const props = { args: { picker: 'layout' } }
+ // A layout preview re-parents the chat; a new key remounts the row the same way.
+ const { rerender } = render(<Fragment key="before">{row(props)}</Fragment>)
+
+ fireEvent.click(screen.getByRole('button', { name: /Elite/ }))
+ rerender(<Fragment key="after">{row(props)}</Fragment>)
+
+ expect(screen.getByRole('button', { name: /Elite/ }).getAttribute('aria-pressed')).toBe('true')
+
+ fireEvent.click(screen.getByRole('button', { name: 'Continue' }))
+
+ expect(respond).toHaveBeenCalledWith({ picked: { layout: 'terminal-deck' } })
+ })
})