Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(console): keep the New agent modal's footer below its body (CHOO-2243) (#263)

Christian McDermott5c7b72398eb5
diff --git a/console/apps/switch-console-desktop/src/renderer/features/locations/components/add-agent-modal/add-agent-modal.tsx b/console/apps/switch-console-desktop/src/renderer/features/locations/components/add-agent-modal/add-agent-modal.tsx
index 64c19e491..3dfd151aa 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/locations/components/add-agent-modal/add-agent-modal.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/locations/components/add-agent-modal/add-agent-modal.tsx
@@ -352,7 +352,11 @@ export const AddAgentModal = observer(function AddAgentModal({ onClose }: AddLoc
</DialogFooter>
}
>
- <DialogContentArea data-autofocus tabIndex={-1} className="max-h-[calc(100dvh-13rem)] gap-4">
+ <DialogContentArea
+ data-autofocus
+ tabIndex={-1}
+ className="max-h-[calc(100dvh-2rem-var(--modal-chrome,8.5rem))] gap-4"
+ >
<AgentIdentityFields form={form} />
<Field>
diff --git a/console/apps/switch-console-desktop/src/renderer/lib/ui/animated-height.tsx b/console/apps/switch-console-desktop/src/renderer/lib/ui/animated-height.tsx
index bbb0dadbb..a9e196c5f 100644
--- a/console/apps/switch-console-desktop/src/renderer/lib/ui/animated-height.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/lib/ui/animated-height.tsx
@@ -5,10 +5,13 @@ import { cn } from '@renderer/utils/utils';
export function AnimatedHeight({
children,
className,
+ style,
onAnimatingChange,
}: {
children: React.ReactNode;
className?: string;
+ // React sets custom properties happily; only the type omits them.
+ style?: React.CSSProperties & Record<`--${string}`, string>;
onAnimatingChange?: (isAnimating: boolean) => void;
}) {
const contentRef = useRef<HTMLDivElement>(null);
@@ -34,14 +37,13 @@ export function AnimatedHeight({
useEffect(() => {
const el = contentRef.current;
if (!el) return;
- // Skip the ResizeObserver's initial callback — the height was already
- // captured synchronously above, so the first observation is never new.
- let isFirstCallback = true;
+ // The observer's first delivery is not redundant with the measurement
+ // above: content that grows in between — a section that opens itself once
+ // a query settles — is reported there and nowhere else. Discarding it
+ // leaves the wrapper pinned too short, and its content then paints outside
+ // the box, under whatever follows. `lastHeightRef` is what keeps an
+ // unchanged first observation from animating.
const ro = new ResizeObserver(() => {
- if (isFirstCallback) {
- isFirstCallback = false;
- return;
- }
const next = el.offsetHeight;
if (lastHeightRef.current === next) return;
lastHeightRef.current = next;
@@ -60,6 +62,7 @@ export function AnimatedHeight({
<motion.div
animate={{ height: height ?? 'auto' }}
transition={{ duration: 0.25, ease: 'easeInOut' }}
+ style={style}
className={cn('w-full', isAnimating ? 'overflow-hidden' : 'overflow-visible', className)}
onAnimationComplete={() => setIsAnimating(false)}
>
diff --git a/console/apps/switch-console-desktop/src/renderer/lib/ui/modal-layout.tsx b/console/apps/switch-console-desktop/src/renderer/lib/ui/modal-layout.tsx
index 6f9de4dbc..1565edb6d 100644
--- a/console/apps/switch-console-desktop/src/renderer/lib/ui/modal-layout.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/lib/ui/modal-layout.tsx
@@ -1,6 +1,19 @@
-import type { ReactNode } from 'react';
+import { type ReactNode, useLayoutEffect, useRef, useState } from 'react';
import { AnimatedHeight } from './animated-height';
+/**
+ * The height the body may not exceed is the popup's own cap less whatever the
+ * header and footer take, and only the header and footer know that. A constant
+ * standing in for them is wrong in both directions: too generous and the body
+ * pushes the footer past the popup's cap, which is how the New agent modal came
+ * to paint its warning callout underneath the footer (CHOO-2243); too mean and
+ * the modal is shorter than the window allows for no reason.
+ *
+ * So measure them and publish the total, rather than guess. Consumers cap their
+ * scroll area with `max-h-[calc(100dvh-2rem-var(--modal-chrome))]` — `2rem`
+ * being the popup's own margin, see `modal-renderer.tsx`. Until the first
+ * measurement lands the variable is unset, so a consumer's fallback applies.
+ */
export function ModalLayout({
header,
footer,
@@ -10,11 +23,42 @@ export function ModalLayout({
footer: ReactNode;
children: ReactNode;
}) {
+ const headerRef = useRef<HTMLDivElement>(null);
+ const footerRef = useRef<HTMLDivElement>(null);
+ const [chrome, setChrome] = useState<number | null>(null);
+
+ // Before paint: the body's cap depends on this, so a frame spent at the
+ // fallback would be a frame at the wrong height.
+ useLayoutEffect(() => {
+ const elements = [headerRef.current, footerRef.current].filter(
+ (el): el is HTMLDivElement => el !== null
+ );
+ if (elements.length === 0) return;
+
+ // `offsetHeight`, not a bounding rect: the popup zooms as it opens, and a
+ // rect measured mid-animation is scaled down with it — enough to under-
+ // reserve and clip the footer against the popup's own cap.
+ const measure = () => setChrome(elements.reduce((total, el) => total + el.offsetHeight, 0));
+
+ measure();
+ // The footer grows a row when it wraps, and again while it waits on a
+ // remote host, so this is not a one-off measurement.
+ const observer = new ResizeObserver(measure);
+ for (const el of elements) observer.observe(el);
+ return () => observer.disconnect();
+ }, []);
+
return (
<>
- {header}
- <AnimatedHeight>{children}</AnimatedHeight>
- {footer}
+ <div ref={headerRef} className="shrink-0">
+ {header}
+ </div>
+ <AnimatedHeight style={chrome === null ? undefined : { '--modal-chrome': `${chrome}px` }}>
+ {children}
+ </AnimatedHeight>
+ <div ref={footerRef} className="shrink-0">
+ {footer}
+ </div>
</>
);
}
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/animated-height-late-growth.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/animated-height-late-growth.test.tsx
new file mode 100644
index 000000000..611cc21de
--- /dev/null
+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/animated-height-late-growth.test.tsx
@@ -0,0 +1,147 @@
+import { useEffect, useState } from 'react';
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, describe, expect, it } from 'vitest';
+import { AnimatedHeight } from '@renderer/lib/ui/animated-height';
+import '@renderer/index.css';
+
+/**
+ * `AnimatedHeight` pins its wrapper to a measured pixel height and, while idle,
+ * lets content overflow that box visibly. It used to discard the
+ * ResizeObserver's first delivery on the theory that the synchronous mount
+ * measurement had already captured the size. Content that grows between those
+ * two moments — the New agent modal opens its Settings section by itself once
+ * the messaging-app lookup settles, which on a warm cache lands in the same
+ * commit as mount — is reported only in that discarded callback, so the wrapper
+ * stayed short and the body painted on underneath the modal's footer
+ * (CHOO-2243).
+ *
+ * These assertions are on measured geometry, not class strings: every class was
+ * correct while the bug was live, and only a real layout shows the content
+ * escaping its box.
+ */
+
+const CONTENT_WIDTH = 480;
+const INITIAL_HEIGHT = 200;
+const GROWN_HEIGHT = 420;
+const FOOTER_HEIGHT = 56;
+
+/**
+ * Sub-pixel residue from the height animation, which a loaded CI runner can
+ * leave a fraction short of its target. The regression this guards against is
+ * two orders of magnitude larger — the wrapper stuck at its pre-growth height —
+ * so a couple of pixels of slack costs the assertions nothing.
+ */
+const TOLERANCE = 2;
+
+let container: HTMLDivElement | null = null;
+let root: Root | null = null;
+
+/** Mirrors ModalLayout: header, AnimatedHeight-wrapped body, footer. */
+function GrowsOnMount({ grow }: { grow: boolean }) {
+ const [tall, setTall] = useState(false);
+
+ // Growing from an effect on mount is the timing that matters: it commits
+ // before the observer's first delivery, so that delivery is the only report
+ // of the new size.
+ useEffect(() => {
+ if (grow) setTall(true);
+ }, [grow]);
+
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', width: `${CONTENT_WIDTH}px` }}>
+ <div data-testid="header" style={{ flexShrink: 0, height: '40px' }} />
+ <AnimatedHeight>
+ <div data-testid="body" style={{ height: `${tall ? GROWN_HEIGHT : INITIAL_HEIGHT}px` }} />
+ </AnimatedHeight>
+ <div data-testid="footer" style={{ flexShrink: 0, height: `${FOOTER_HEIGHT}px` }} />
+ </div>
+ );
+}
+
+async function render(grow: boolean): Promise<HTMLDivElement> {
+ container = document.createElement('div');
+ document.body.appendChild(container);
+ root = createRoot(container);
+ await act(async () => root!.render(<GrowsOnMount grow={grow} />));
+ return container;
+}
+
+function wrapperOf(el: HTMLDivElement): HTMLElement {
+ const body = el.querySelector('[data-testid="body"]');
+ // body -> contentRef div -> motion.div
+ return body!.parentElement!.parentElement as HTMLElement;
+}
+
+/**
+ * The wrapper animates to each new height, so measure only once it has stopped
+ * moving. Polling rather than sleeping a fixed span keeps the assertions off
+ * the animation's timing: a wrapper that never tracks its content settles just
+ * as readily, at the wrong height.
+ */
+async function settle(el: HTMLDivElement): Promise<void> {
+ const wrapper = wrapperOf(el);
+ let previous = -1;
+ let stableReads = 0;
+ for (let i = 0; i < 60; i++) {
+ await act(async () => {
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ });
+ const current = Math.round(wrapper.getBoundingClientRect().height);
+ // Easing crawls at the end, so a single repeated read is not proof the
+ // animation is over — one starved frame looks the same.
+ stableReads = current === previous ? stableReads + 1 : 0;
+ previous = current;
+ if (stableReads >= 3) return;
+ }
+}
+
+afterEach(() => {
+ act(() => root?.unmount());
+ root = null;
+ container?.remove();
+ container = null;
+});
+
+describe('AnimatedHeight late growth', () => {
+ it('tracks content that grows in the same commit as mount', async () => {
+ const el = await render(true);
+ await settle(el);
+
+ const wrapper = wrapperOf(el);
+ const body = el.querySelector('[data-testid="body"]')!;
+
+ // Guard the guard: a body that never grew would satisfy the containment
+ // check below while proving nothing.
+ expect(body.getBoundingClientRect().height).toBeCloseTo(GROWN_HEIGHT, 0);
+ expect(Math.abs(wrapper.getBoundingClientRect().height - GROWN_HEIGHT)).toBeLessThanOrEqual(
+ TOLERANCE
+ );
+ });
+
+ it('keeps grown content inside its box, clear of what follows', async () => {
+ const el = await render(true);
+ await settle(el);
+
+ const wrapper = wrapperOf(el);
+ const footer = el.querySelector('[data-testid="footer"]')!;
+ const body = el.querySelector('[data-testid="body"]')!;
+
+ const bodyRect = body.getBoundingClientRect();
+ const footerRect = footer.getBoundingClientRect();
+
+ // The content must not spill past the box that positions the footer, and
+ // must not reach into the footer itself.
+ expect(bodyRect.bottom).toBeLessThanOrEqual(wrapper.getBoundingClientRect().bottom + TOLERANCE);
+ expect(bodyRect.bottom).toBeLessThanOrEqual(footerRect.top + TOLERANCE);
+ });
+
+ it('leaves a wrapper whose content never changed at its measured height', async () => {
+ const el = await render(false);
+ await settle(el);
+
+ expect(
+ Math.abs(wrapperOf(el).getBoundingClientRect().height - INITIAL_HEIGHT)
+ ).toBeLessThanOrEqual(TOLERANCE);
+ });
+});
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/modal-layout-chrome.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/modal-layout-chrome.test.tsx
new file mode 100644
index 000000000..0c3536556
--- /dev/null
+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/modal-layout-chrome.test.tsx
@@ -0,0 +1,147 @@
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, describe, expect, it } from 'vitest';
+import { ModalLayout } from '@renderer/lib/ui/modal-layout';
+import '@renderer/index.css';
+
+/**
+ * The body's cap used to be a constant standing in for the header and footer,
+ * which over-reserved by ~80px on a normal window and would have under-reserved
+ * had the footer ever grown a row — putting content back underneath it
+ * (CHOO-2243). `ModalLayout` measures them instead and publishes the total as
+ * `--modal-chrome`.
+ *
+ * Geometry, not class strings: the point is that the popup's own cap is
+ * respected, which only a real layout can show.
+ */
+
+const POPUP_HEIGHT = 400;
+const HEADER_HEIGHT = 40;
+const SHORT_FOOTER = 56;
+const TALL_FOOTER = 120;
+
+/**
+ * Sub-pixel residue from the body wrapper's height animation, which a loaded CI
+ * runner can leave a fraction short. The regression here is the footer pushed
+ * clean out of the popup, so a couple of pixels of slack costs nothing.
+ */
+const TOLERANCE = 2;
+
+let container: HTMLDivElement | null = null;
+let root: Root | null = null;
+
+function Harness({ footerHeight }: { footerHeight: number }) {
+ return (
+ <div
+ data-testid="popup"
+ style={{
+ display: 'flex',
+ flexDirection: 'column',
+ maxHeight: `${POPUP_HEIGHT}px`,
+ overflow: 'hidden',
+ width: '480px',
+ }}
+ >
+ <ModalLayout
+ header={<div data-testid="header" style={{ height: `${HEADER_HEIGHT}px` }} />}
+ footer={<div data-testid="footer" style={{ height: `${footerHeight}px` }} />}
+ >
+ <div
+ data-testid="body"
+ style={{
+ overflowY: 'auto',
+ maxHeight: `calc(${POPUP_HEIGHT}px - var(--modal-chrome, 136px))`,
+ }}
+ >
+ <div style={{ height: '2000px' }} />
+ </div>
+ </ModalLayout>
+ </div>
+ );
+}
+
+async function render(footerHeight: number): Promise<HTMLDivElement> {
+ container = document.createElement('div');
+ document.body.appendChild(container);
+ root = createRoot(container);
+ await act(async () => root!.render(<Harness footerHeight={footerHeight} />));
+ await settle();
+ return container;
+}
+
+async function rerender(footerHeight: number): Promise<void> {
+ await act(async () => root!.render(<Harness footerHeight={footerHeight} />));
+ await settle();
+}
+
+/** Wait for the body wrapper to stop animating rather than sleeping a fixed
+ * span: easing crawls at the end, so one repeated read is not proof it is over. */
+async function settle(): Promise<void> {
+ const footer = container?.querySelector('[data-testid="footer"]');
+ let previous = -1;
+ let stableReads = 0;
+ for (let i = 0; i < 60; i++) {
+ await act(async () => {
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ });
+ if (!footer) continue;
+ const current = Math.round(footer.getBoundingClientRect().top);
+ stableReads = current === previous ? stableReads + 1 : 0;
+ previous = current;
+ if (stableReads >= 3) return;
+ }
+}
+
+function bottomOf(el: Element): number {
+ return el.getBoundingClientRect().bottom;
+}
+
+afterEach(() => {
+ act(() => root?.unmount());
+ root = null;
+ container?.remove();
+ container = null;
+});
+
+describe('ModalLayout chrome reservation', () => {
+ it('reserves exactly what the header and footer measure', async () => {
+ const el = await render(SHORT_FOOTER);
+ const body = el.querySelector('[data-testid="body"]')!;
+
+ expect(getComputedStyle(body).getPropertyValue('--modal-chrome').trim()).toBe(
+ `${HEADER_HEIGHT + SHORT_FOOTER}px`
+ );
+ expect(getComputedStyle(body).maxHeight).toBe(
+ `${POPUP_HEIGHT - HEADER_HEIGHT - SHORT_FOOTER}px`
+ );
+ });
+
+ it('keeps the footer inside the popup when the body is taller than the space', async () => {
+ const el = await render(SHORT_FOOTER);
+ const popup = el.querySelector('[data-testid="popup"]')!;
+ const footer = el.querySelector('[data-testid="footer"]')!;
+ const body = el.querySelector('[data-testid="body"]')!;
+
+ // Guard the guard: a body that fits would satisfy this without the cap
+ // doing any work.
+ expect(body.scrollHeight).toBeGreaterThan(body.clientHeight);
+
+ expect(bottomOf(footer)).toBeLessThanOrEqual(bottomOf(popup) + TOLERANCE);
+ expect(bottomOf(body)).toBeLessThanOrEqual(footer.getBoundingClientRect().top + TOLERANCE);
+ });
+
+ it('follows the footer when it grows a row', async () => {
+ const el = await render(SHORT_FOOTER);
+ await rerender(TALL_FOOTER);
+
+ const popup = el.querySelector('[data-testid="popup"]')!;
+ const footer = el.querySelector('[data-testid="footer"]')!;
+ const body = el.querySelector('[data-testid="body"]')!;
+
+ expect(getComputedStyle(body).getPropertyValue('--modal-chrome').trim()).toBe(
+ `${HEADER_HEIGHT + TALL_FOOTER}px`
+ );
+ expect(bottomOf(footer)).toBeLessThanOrEqual(bottomOf(popup) + TOLERANCE);
+ expect(bottomOf(body)).toBeLessThanOrEqual(footer.getBoundingClientRect().top + TOLERANCE);
+ });
+});