Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-app): r2-h1 recompute nested submenu after the parent menu commits its resize
Diego Zacarias322956cb1ccc
diff --git a/desktop-app/ui/src/components/agents/AgentTitleSelector.tsx b/desktop-app/ui/src/components/agents/AgentTitleSelector.tsxindex c0716b8d0..c4ad2e8c9 100644--- a/desktop-app/ui/src/components/agents/AgentTitleSelector.tsx+++ b/desktop-app/ui/src/components/agents/AgentTitleSelector.tsx@@ -88,6 +88,14 @@ export function AgentTitleSelector({// drawer tree; otherwise the submenu clamps to the viewport and can paint// over the native embed to the drawer's left.boundsAnchorRef: triggerRef,+ // The submenu anchor (a row's dots button) lives INSIDE the parent menu,+ // which is portaled and positioned by `menuFlyoutPosition`. When a resize+ // repositions the parent, React batches both flyouts' updates: the submenu+ // measures its anchor before the parent's new left/top commits, so it reads+ // the pre-move rect. The parent's move is position-only and fires no+ // ResizeObserver on the anchor, so without this the submenu keeps the stale+ // offset. Recompute once the parent's position commits.+ recomputeKey: menuFlyoutPosition,})// All three refs: the menu and its sub-menu are portaled, so a mousedowndiff --git a/desktop-app/ui/src/components/agents/__tests__/AgentTitleSelector.submenuResize.test.tsx b/desktop-app/ui/src/components/agents/__tests__/AgentTitleSelector.submenuResize.test.tsxnew file mode 100644index 000000000..a2eb9293a--- /dev/null+++ b/desktop-app/ui/src/components/agents/__tests__/AgentTitleSelector.submenuResize.test.tsx@@ -0,0 +1,127 @@+// @vitest-environment jsdom+import { afterEach, describe, expect, it, vi } from 'vitest'+import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'+import { AgentTitleSelector } from '../AgentTitleSelector'++;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true++const OPTIONS = [+ { id: 'alpha', label: 'Alpha' },+ { id: 'beta', label: 'Beta' },+]++// jsdom performs no layout, so every getBoundingClientRect is 0. Give each node+// the geometry the hook reads, from a function so the same stub reflects the+// current `geom.dx` (the resize) and, for the dots anchor, the parent menu's+// COMMITTED style.left — which is exactly how the anchor moves in production.+function setRect(+ el: Element,+ fn: () => { left: number; top: number; width: number; height: number }+) {+ el.getBoundingClientRect = () => {+ const r = fn()+ return {+ left: r.left,+ top: r.top,+ right: r.left + r.width,+ bottom: r.top + r.height,+ width: r.width,+ height: r.height,+ x: r.left,+ y: r.top,+ toJSON: () => ({}),+ } as DOMRect+ }+}++afterEach(() => {+ cleanup()+ vi.restoreAllMocks()+ document.body.innerHTML = ''+})++describe('AgentTitleSelector — nested submenu follows a resized parent menu', () => {+ it('re-anchors the submenu when a window resize repositions the portaled parent menu', () => {+ // Real viewport + a right-docked drawer; a zero-width bounds rect would make+ // the hook fall back to the viewport and mask the bug under test.+ window.innerWidth = 1200+ window.innerHeight = 1000++ // A window resize shifts the drawer + its trigger right by Δ; the parent+ // menu ('below', anchored to the in-drawer trigger) tracks the trigger, and+ // the dots anchor (a menu child) tracks the parent menu's committed left.+ const geom = { dx: 0 }+ const DELTA = 40+ const MENU_ANCHOR_OFFSET = 20 // dots.left = parentMenu.style.left + this++ const drawer = document.createElement('div')+ drawer.className = 'chat-drawer'+ document.body.appendChild(drawer)+ // Drawer docked to the right; area = [808+dx, 1192+dx] after the 8px inset.+ setRect(drawer, () => ({ left: 800 + geom.dx, top: 0, width: 400, height: 800 }))++ const onSelectAgent = vi.fn()+ const onOpenRoute = vi.fn()+ render(+ <AgentTitleSelector+ ariaLabel="Switch chat agent"+ emptyLabel="No agents"+ options={OPTIONS}+ selectedId="alpha"+ selectedLabel="Alpha"+ onSelectAgent={onSelectAgent}+ onOpenRoute={onOpenRoute}+ />,+ { container: drawer }+ )++ // Trigger lives in the drawer; the menu 'below' left tracks trigger.left.+ const trigger = screen.getByRole('button', { name: 'Switch chat agent' })+ setRect(trigger, () => ({ left: 850 + geom.dx, top: 50, width: 100, height: 30 }))++ // Open the parent menu. With dx=0: menu 'below' left = clamp(850, …) = 850.+ fireEvent.click(trigger)+ const menuEl = document.querySelector('.agent-title-selector-menu') as HTMLElement+ expect(menuEl).not.toBeNull()+ setRect(menuEl, () => ({+ left: parseFloat(menuEl.style.left) || 0,+ top: parseFloat(menuEl.style.top) || 0,+ width: 200,+ height: 250,+ }))+ expect(menuEl.style.left).toBe('850px')++ // The dots anchor (a menu child) is portaled with the menu; its on-screen+ // left DERIVES from the parent menu's committed style.left, so it "moves+ // with the parent" — and during the resize batch it reads the parent's OLD+ // (uncommitted) left, exactly as in production.+ const dots = screen.getByRole('menuitem', { name: 'Open Alpha sections' })+ setRect(dots, () => ({+ left: (parseFloat(menuEl.style.left) || 0) + MENU_ANCHOR_OFFSET,+ top: 100,+ width: 10,+ height: 16,+ }))++ // Open the submenu. 'right-of' from dots.right (880): left = 880 + 6 = 886.+ fireEvent.click(dots)+ const submenu = document.querySelector('.agent-title-selector-submenu') as HTMLElement+ expect(submenu).not.toBeNull()+ const submenuLeft0 = parseFloat(submenu.style.left)+ expect(submenuLeft0).toBe(886)++ // Resize the window so the parent menu repositions by Δ. Batched updates:+ // the submenu measures the dots anchor before the parent's new left commits.+ act(() => {+ geom.dx = DELTA+ window.dispatchEvent(new Event('resize'))+ })++ // Observable result: the parent moved by Δ (850 → 890), so the dots anchor+ // moved by Δ, so the submenu must follow — left = 886 + Δ = 926. Without the+ // recompute after the parent commits, the submenu keeps its pre-resize 886.+ expect(menuEl.style.left).toBe(`${850 + DELTA}px`)+ expect(submenu.style.left).toBe(`${submenuLeft0 + DELTA}px`)+ expect(submenu.style.left).toBe('926px')+ })+})diff --git a/desktop-app/ui/src/hooks/useFlyoutPosition.ts b/desktop-app/ui/src/hooks/useFlyoutPosition.tsindex 0ff7a1a09..a80e280c4 100644--- a/desktop-app/ui/src/hooks/useFlyoutPosition.ts+++ b/desktop-app/ui/src/hooks/useFlyoutPosition.ts@@ -22,6 +22,15 @@ type UseFlyoutPositionArgs = {// confining ancestor (e.g. the still-in-tree trigger). Defaults to// `anchorRef`, so omitting it preserves today's behavior exactly.boundsAnchorRef?: RefObject<HTMLElement | null>+ // Optional dependency that forces a reposition when its value changes, even+ // though no resize/scroll/ResizeObserver notification fired. Needed when this+ // flyout's anchor is a DOM child of ANOTHER portaled flyout: when that parent+ // repositions through a batched React commit, the anchor moves with it, but a+ // position-only change emits no ResizeObserver callback, so `update` never+ // re-runs and this flyout keeps the pre-move offset. Pass the parent's+ // committed `FlyoutPosition` here to re-measure after it commits. Omitting it+ // leaves behavior byte-for-byte unchanged — the recompute effect no-ops.+ recomputeKey?: unknowngap?: numberinset?: number}@@ -41,6 +50,7 @@ export function useFlyoutPosition({placement,boundsSelector = '.chat-drawer',boundsAnchorRef,+ recomputeKey,gap,inset,}: UseFlyoutPositionArgs): FlyoutPosition | null {@@ -98,5 +108,18 @@ export function useFlyoutPosition({}}, [anchorRef, boundsAnchorRef, boundsSelector, flyoutRef, open, update])+ // Recompute after a dependency that shifts the anchor's on-screen position+ // WITHOUT emitting a resize/scroll notification of its own — e.g. a PARENT+ // flyout that repositioned through a batched React commit. When this flyout's+ // anchor is a DOM child of that parent, the anchor moved, but a position-only+ // change fires no ResizeObserver, so `update` never re-runs. Passing the+ // parent's committed position as `recomputeKey` re-measures after it commits.+ // Guarded on `!== undefined` so callers that omit it are byte-for-byte+ // unchanged (no extra render). Kept separate from the observer effect above so+ // it never churns the resize/scroll subscription.+ useLayoutEffect(() => {+ if (open && recomputeKey !== undefined) update()+ }, [open, recomputeKey, update])+return position}