Skip to content

The agent that grows with you

fix(desktop): memoize statusbar render-contributions so state survives re-renders

ce-dric3d5f2546a55f
diff --git a/apps/desktop/src/app/contrib/panes.test.tsx b/apps/desktop/src/app/contrib/panes.test.tsx
new file mode 100644
index 00000000000..e3e04afce3d
--- /dev/null
+++ b/apps/desktop/src/app/contrib/panes.test.tsx
@@ -0,0 +1,80 @@
+import { cleanup, fireEvent, render, screen } from '@testing-library/react'
+import { useState } from 'react'
+import { afterEach, describe, expect, it } from 'vitest'
+
+import { ContribRender } from '@/contrib/react/boundary'
+import { registry } from '@/contrib/registry'
+
+import { useStatusbarContributions } from './panes'
+
+// Registrations are global (module-level registry); each test captures its
+// own disposer and this undoes it, so tests don't bleed into each other.
+let dispose: (() => void) | undefined
+
+afterEach(() => {
+ cleanup()
+ dispose?.()
+ dispose = undefined
+})
+
+// A stateful contribution — a chip that opens a dialog-like panel and holds
+// that open/closed state itself, the same shape a real plugin uses.
+function StatefulChip() {
+ const [open, setOpen] = useState(false)
+
+ return (
+ <div>
+ <button onClick={() => setOpen(true)} type="button">
+ open
+ </button>
+ {open && <div data-testid="panel">panel open</div>}
+ </div>
+ )
+}
+
+// Hosts the contributed items and carries UNRELATED state of its own, so
+// clicking "bump" re-renders the host (and re-invokes useStatusbarContributions)
+// without touching the registry — exactly what a streaming message or a
+// session-timer tick does to the real statusbar.
+function Host() {
+ const [, setTick] = useState(0)
+ const items = useStatusbarContributions('right')
+
+ return (
+ <div>
+ <button onClick={() => setTick(t => t + 1)} type="button">
+ bump
+ </button>
+ {items.map(item =>
+ // Mirror the real consumer exactly (statusbar-controls.tsx): `item.render`
+ // is passed as a PROP and mounted via `createElement(render)` inside
+ // ContribRender — not invoked inline. Calling it directly here would
+ // dodge the bug entirely (the wrapper's own identity never gets used
+ // as a component type, so no remount could ever be observed).
+ item.render ? <ContribRender key={item.id} render={item.render} /> : null
+ )}
+ </div>
+ )
+}
+
+describe('useStatusbarContributions', () => {
+ it('keeps a render-contribution mounted (state intact) across an unrelated host re-render', () => {
+ dispose = registry.register({ area: 'statusBar.right', id: 'stateful-chip', render: () => <StatefulChip /> })
+
+ render(<Host />)
+
+ // Open the panel — this is the state that must survive.
+ fireEvent.click(screen.getByRole('button', { name: 'open' }))
+ expect(screen.getByTestId('panel')).toBeTruthy()
+
+ // Re-render the host for a reason that has nothing to do with the
+ // registry (a streaming tick, a session switch elsewhere in the tree).
+ fireEvent.click(screen.getByRole('button', { name: 'bump' }))
+
+ // #91603: before the fix, useStatusbarContributions handed back a new
+ // `render` closure identity every call, so ContribRender's
+ // createElement(render) read it as a different component type and
+ // remounted StatefulChip — dropping `open` back to false.
+ expect(screen.getByTestId('panel')).toBeTruthy()
+ })
+})
diff --git a/apps/desktop/src/app/contrib/panes.tsx b/apps/desktop/src/app/contrib/panes.tsx
index 428770b78a9..c4a737aa675 100644
--- a/apps/desktop/src/app/contrib/panes.tsx
+++ b/apps/desktop/src/app/contrib/panes.tsx
@@ -11,6 +11,7 @@
import { useStore } from '@nanostores/react'
import { useQuery } from '@tanstack/react-query'
import { atom } from 'nanostores'
+import { useMemo } from 'react'
import { RightSidebarPane } from '@/app/right-sidebar'
import { ReviewPane } from '@/app/right-sidebar/review'
@@ -118,24 +119,35 @@ export function ReviewPaneContent() {
/** Collect statusbar contributions for one side. A `render()` contribution
* becomes a render-item (arbitrary stateful node); otherwise the declarative
- * `data` payload is the StatusbarItem. */
+ * `data` payload is the StatusbarItem.
+ *
+ * Memoized on `items` (a stable reference from `useContributions` until the
+ * area actually changes — see `registry.getArea`'s snapshot cache): without
+ * this, every render-item's `render` wrapper was a brand-new arrow function,
+ * so `ContribRender`'s `createElement(render)` saw a different component
+ * TYPE on every statusbar re-render and remounted the whole contributed
+ * subtree — dropping any state it held (e.g. an open Dialog). See #91603. */
export function useStatusbarContributions(side: 'left' | 'right'): StatusbarItem[] {
const items = useContributions(`statusBar.${side}`)
- return items
- .map(c =>
- c.render
- ? ({
- id: c.id,
- render: () => (
- <ContribBoundary id={c.id} variant="chip">
- <ContribRender render={c.render!} />
- </ContribBoundary>
- )
- } satisfies StatusbarItem)
- : (c.data as StatusbarItem)
- )
- .filter(Boolean)
+ return useMemo(
+ () =>
+ items
+ .map(c =>
+ c.render
+ ? ({
+ id: c.id,
+ render: () => (
+ <ContribBoundary id={c.id} variant="chip">
+ <ContribRender render={c.render!} />
+ </ContribBoundary>
+ )
+ } satisfies StatusbarItem)
+ : (c.data as StatusbarItem)
+ )
+ .filter(Boolean),
+ [items]
+ )
}
/** Collect TitlebarTool data contributions for one side of the titlebar. */