diff --git a/apps/web/src/components/ChatView.agents.browser.tsx b/apps/web/src/components/ChatView.agents.browser.tsx index a92bdc3ad..99e120dc9 100644 --- a/apps/web/src/components/ChatView.agents.browser.tsx +++ b/apps/web/src/components/ChatView.agents.browser.tsx @@ -1445,6 +1445,80 @@ describe("ChatView (full app): agent browser tabs and rooms", () => { } }); + it("keeps short-titled tabs on the row where they fit, instead of behind +N", async () => { + const titles = ["UI", "DB", "QA", "CI", "Ops", "Docs", "Perf", "Auth"]; + const children = titles.map((_, index) => `thread-short-${index}` as ThreadId); + const base = children.reduce( + (snapshot, child) => addThreadToSnapshot(snapshot, child), + createSnapshotForTargetUser({ + targetMessageId: "msg-user-short-family" as MessageId, + targetText: "short family target", + }), + ); + const mounted = await mountChatView({ + viewport: DEFAULT_VIEWPORT, + snapshot: { + ...base, + projects: base.projects.map((project) => ({ ...project, leadThreadId: THREAD_ID })), + threads: base.threads.map((thread) => { + const index = children.indexOf(thread.id); + return index === -1 + ? thread + : { + ...thread, + title: titles[index]!, + parentThreadId: THREAD_ID, + attachedToParent: true, + createdAt: isoAt(10 + index), + }; + }), + }, + }); + const row = () => document.querySelector('[data-testid="family-tabs"]'); + try { + await vi.waitFor( + () => expect(row()?.querySelectorAll('[role="tab"]')).toHaveLength(1 + titles.length), + { timeout: 10_000 }, + ); + // Nine tabs at the 96px floor would not fit; at their own widths they do, none cut. + const onRow = () => [ + ...row()!.querySelectorAll( + '[role="tab"], [data-testid="family-tabs-overflow"]', + ), + ]; + const unclipped = () => + onRow().every((item) => { + const label = item.querySelector("span.truncate") ?? item; + return label.scrollWidth <= label.clientWidth; + }); + const inside = () => + onRow().every( + (item) => + item.getBoundingClientRect().right <= row()!.getBoundingClientRect().right + 0.5, + ); + expect(row()!.clientWidth).toBeLessThan((1 + titles.length) * 96); + expect(row()!.querySelector('[data-testid="family-tabs-overflow"]')).toBeNull(); + expect(unclipped()).toBe(true); + expect(inside()).toBe(true); + + // A larger browser font: the row measures what it draws, so what shows still fits whole. + document.documentElement.style.fontSize = "24px"; + try { + await vi.waitFor( + () => expect(row()!.querySelector('[data-testid="family-tabs-overflow"]')).not.toBeNull(), + { timeout: 5_000 }, + ); + await nextFrames(3); + expect(unclipped()).toBe(true); + expect(inside()).toBe(true); + } finally { + document.documentElement.style.fontSize = ""; + } + } finally { + await mounted.cleanup(); + } + }); + it("names the thread that started this one, in the header and on its request", async () => { const base = createSnapshotForTargetUser({ targetMessageId: "msg-user-started-by" as MessageId, diff --git a/apps/web/src/components/chat/FamilyTabs.tsx b/apps/web/src/components/chat/FamilyTabs.tsx index e067dd891..c29c7a215 100644 --- a/apps/web/src/components/chat/FamilyTabs.tsx +++ b/apps/web/src/components/chat/FamilyTabs.tsx @@ -28,6 +28,7 @@ import { buildThreadRouteParams } from "../../threadRoutes"; import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; import { LeadChatCard, LeadChatSuggestion } from "./LeadChatHints"; import { + FAMILY_TAB_MIN_WIDTH_PX, type FamilyTab, type FamilyTabs as FamilyTabsModel, familyTabsOf, @@ -50,8 +51,85 @@ export function useFamilyTabsShown(threadKey: string | null): boolean { ); } -/** What "+N" takes on the row, its own width. */ -const OVERFLOW_PX = 44; +/** A tab's box, shared by the drawn tabs and their measured copies so the two match. */ +const TAB_BOX_CLASS = "flex h-6 items-center gap-1.5 rounded-md px-2 text-xs"; +/** The "+N" button's box, shared the same way. */ +const OVERFLOW_BOX_CLASS = "relative flex h-6 shrink-0 items-center rounded-md px-2 text-xs"; + +/** What a tab shows as its title: the lead's is "Lead". */ +function tabText(tab: FamilyTab): string { + return tab.kind === "parent" && tab.lead ? "Lead" : tab.shortTitle; +} + +/** The row as drawn: its width inside the padding, the gap between tabs, and each tab's own width. */ +interface RowMeasure { + readonly rowPx: number; + readonly gapPx: number; + /** Each tab at its title's full length, up to the 200px cap, by key. */ + readonly tabPx: ReadonlyMap; + /** "+N" with the most digits it can need. */ + readonly overflowPx: number; +} + +function sameRowMeasure(left: RowMeasure | null, right: RowMeasure): boolean { + return ( + left !== null && + left.rowPx === right.rowPx && + left.gapPx === right.gapPx && + left.overflowPx === right.overflowPx && + left.tabPx.size === right.tabPx.size && + [...right.tabPx].every(([key, px]) => left.tabPx.get(key) === px) + ); +} + +/** + * Reads the row and the hidden copies of its tabs (`measureRef`): what fits + * is decided before a tab is drawn, and one behind "+N" isn't drawn at all. + * The copies are the tabs' own markup and classes, so a larger browser font, + * a theme's font, or a font that loads late shows up as a resize the + * observer reports. Null until the row is on screen, and on a phone. + */ +function useRowMeasure( + rowRef: React.RefObject, + measureRef: React.RefObject, + enabled: boolean, + tabKeys: string, +): RowMeasure | null { + const [measure, setMeasure] = useState(null); + useLayoutEffect(() => { + const row = rowRef.current; + const layer = measureRef.current; + if (!enabled || row === null || layer === null) return; + // The copies of these tabs, by key, and the copy of "+N". + const copies = tabKeys.split("\n").flatMap((key) => { + const copy = layer.querySelector(`[data-measure-key="${CSS.escape(key)}"]`); + return copy === null ? [] : [[key, copy] as const]; + }); + const overflowCopy = layer.querySelector("[data-measure-overflow]"); + const read = () => { + const style = getComputedStyle(row); + const next: RowMeasure = { + rowPx: + row.getBoundingClientRect().width - + parseFloat(style.paddingLeft) - + parseFloat(style.paddingRight) - + parseFloat(style.borderLeftWidth) - + parseFloat(style.borderRightWidth), + gapPx: parseFloat(style.columnGap) || 0, + tabPx: new Map(copies.map(([key, copy]) => [key, copy.getBoundingClientRect().width])), + overflowPx: overflowCopy?.getBoundingClientRect().width ?? 0, + }; + setMeasure((previous) => (sameRowMeasure(previous, next) ? previous : next)); + }; + read(); + const observer = new ResizeObserver(read); + observer.observe(row); + for (const [, copy] of copies) observer.observe(copy); + if (overflowCopy !== null) observer.observe(overflowCopy); + return () => observer.disconnect(); + }, [enabled, measureRef, rowRef, tabKeys]); + return enabled ? measure : null; +} /** * The row for `threadRef`'s family, or nothing when it has none. While it @@ -127,17 +205,13 @@ const FamilyTabRow = memo(function FamilyTabRow({ }) { const isMobile = useIsMobile(); const rowRef = useRef(null); - const [rowPx, setRowPx] = useState(null); - useLayoutEffect(() => { - const row = rowRef.current; - if (row === null || isMobile) return; - setRowPx(row.clientWidth); - const observer = new ResizeObserver(([entry]) => { - if (entry) setRowPx(Math.round(entry.contentRect.width)); - }); - observer.observe(row); - return () => observer.disconnect(); - }, [isMobile]); + const measureRef = useRef(null); + const measure = useRowMeasure( + rowRef, + measureRef, + !isMobile, + family.tabs.map((tab) => tab.key).join("\n"), + ); // On a phone the row scrolls sideways instead, with the open tab in view. const openTabRef = useRef(null); @@ -149,13 +223,19 @@ const FamilyTabRow = memo(function FamilyTabRow({ }, [openKey, isMobile]); const { shown, hidden } = - isMobile || rowPx === null + measure === null ? { shown: family.tabs, hidden: [] as readonly FamilyTab[] } : fitFamilyTabs({ tabs: family.tabs, openKey: family.openKey, - rowPx, - overflowPx: OVERFLOW_PX, + rowPx: measure.rowPx, + overflowPx: measure.overflowPx, + gapPx: measure.gapPx, + // A short tab costs its own width; a long one the shared floor; the lead never shrinks. + floorPx: (tab) => { + const px = measure.tabPx.get(tab.key) ?? FAMILY_TAB_MIN_WIDTH_PX; + return tab.kind === "parent" && tab.lead ? px : Math.min(px, FAMILY_TAB_MIN_WIDTH_PX); + }, }); // The parent, open and not leading yet, may be offered as the lead (LeadChatSuggestion). @@ -178,10 +258,32 @@ const FamilyTabRow = memo(function FamilyTabRow({ data-testid="family-tabs" data-family-key={family.familyKey} className={cn( - "flex min-w-0 shrink-0 items-center gap-0.5 border-b border-border px-3 py-1 sm:px-5", + "relative flex min-w-0 shrink-0 items-center gap-0.5 border-b border-border px-3 py-1 sm:px-5", isMobile && "overflow-x-auto [scrollbar-width:none]", )} > + {isMobile ? null : ( + // Every tab and "+N" at full width, unseen, for useRowMeasure. + + )} {shown.map((tab) => ( + + + ); + return isLeadTab && tab.threadRef !== null ? ( + + ) : ( + tabButton(null) + ); +}); + +/** A tab's mark (the lead's crown, a child's status dot or check) and title. */ +function FamilyTabFace({ tab }: { tab: FamilyTab }) { + return ( + <> {tab.kind === "parent" ? ( tab.lead ? (