Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions apps/web/src/components/ChatView.agents.browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1823,6 +1823,117 @@ describe("ChatView (full app): agent browser tabs and rooms", () => {
}
});

it("opens a pill's menu on a long press in the phone drawer, and keeps it open when the finger lifts", async () => {
const astraId = "7a0b1c2d-3e4f-4a5b-8c6d-7e8f9a0b1c2d" as ThreadParticipantId;
const base = createSnapshotForTargetUser({
targetMessageId: "msg-user-room-hold" as MessageId,
targetText: "room hold target",
});
// What a phone sends for a finger held on an element and lifted: touch
// pointer events on it, then a mouse press and a click on whatever is
// under the finger by then. For a held pill that is the menu's backdrop,
// which came up under the finger. (Replayed by hand: real touches would
// put the whole test browser in touch mode.)
const touch = async (element: Element, holdMs: number) => {
const box = element.getBoundingClientRect();
const at = {
clientX: box.left + box.width / 2,
clientY: box.top + box.height / 2,
bubbles: true,
cancelable: true,
composed: true,
};
const finger = { ...at, pointerId: 7, pointerType: "touch", isPrimary: true };
element.dispatchEvent(new PointerEvent("pointerdown", finger));
await new Promise((resolve) => setTimeout(resolve, holdMs));
element.dispatchEvent(new PointerEvent("pointerup", finger));
const under = document.elementFromPoint(at.clientX, at.clientY) ?? element;
for (const type of ["mousedown", "mouseup", "click"]) {
under.dispatchEvent(new MouseEvent(type, at));
}
// Let what those did reach the page.
await new Promise((resolve) => setTimeout(resolve, 100));
};
const mounted = await mountChatView({
viewport: PHONE_VIEWPORT,
configureFixture: withCodexModels,
snapshot: {
...base,
threads: base.threads.map((thread) =>
thread.id === THREAD_ID
? {
...thread,
participants: [
{
id: astraId,
handle: "agent-2",
modelSelection: {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-6-astra",
},
joinedAt: NOW_ISO,
leftAt: null,
},
],
}
: thread,
),
},
});

try {
const pill = (id: string) =>
document.querySelector<HTMLElement>(`[data-room-agent-row="${id}"]`);
const sheet = () => document.querySelector('[data-model-picker-sheet="true"]');
const menuItem = (text: string) =>
[...document.querySelectorAll<HTMLElement>('[role="menuitem"]')].find(
(item) => item.textContent === text,
) ?? null;
const picker = await waitForElement(
() => document.querySelector<HTMLButtonElement>('[data-chat-provider-model-picker="true"]'),
"Unable to find the composer's model picker.",
);
picker.click();
await waitForElement(sheet, "A phone should open the picker as a drawer.");
const astra = await waitForElement(() => pill(astraId), "Unable to find astra's pill.");
// The drawer slides in: a touch is aimed at where the pill comes to rest.
await vi.waitFor(async () => {
const top = astra.getBoundingClientRect().top;
await nextFrames(2);
expect(astra.getBoundingClientRect().top).toBe(top);
});

// The hold opens the menu, and lifting the finger leaves it open,
// without picking the pill or closing the drawer.
await touch(astra, 700);
expect(astra.hasAttribute("data-menu-open")).toBe(true);
expect(menuItem("Rename…")).not.toBeNull();
expect(pill(astraId)?.getAttribute("aria-checked")).toBe("false");
expect(sheet()).not.toBeNull();

// A tap outside the menu still closes it, and only it.
await new Promise((resolve) => setTimeout(resolve, 500));
await touch(document.querySelector('[data-slot="menu-backdrop"]')!, 40);
expect(astra.hasAttribute("data-menu-open")).toBe(false);
await vi.waitFor(() => expect(menuItem("Rename…")).toBeNull());
expect(sheet()).not.toBeNull();

// A tap on Rename, even a slow one, opens the name box, focused, in the
// open drawer: it is not taken for another hold on the pill.
await touch(astra, 700);
await touch(menuItem("Rename…")!, 600);
const nameBox = await waitForElement(
() => pill(astraId)!.querySelector<HTMLInputElement>("input"),
"Rename should open the pill's name box.",
);
await vi.waitFor(() => expect(document.activeElement).toBe(nameBox));
expect(sheet()).not.toBeNull();
} finally {
useRoomRecipientStore.getState().choose(THREAD_REF, null);
await mounted.cleanup();
}
});

it("stacks a room's agents on a phone's model button, and drops idle ones first as it narrows", async () => {
const agentIds = [
"7a0b1c2d-3e4f-4a5b-8c6d-7e8f9a0b1c2d",
Expand Down
55 changes: 45 additions & 10 deletions apps/web/src/components/chat/RoomAgentsBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,25 +209,50 @@ function AgentPill(props: {
const onPill = (event: React.SyntheticEvent) =>
event.currentTarget.contains(event.target as Node);

const openMenu = useCallback(() => setMenuOpen(true), []);
// A held finger opened the menu, which came up under it: when it lifts, the
// browser's mouse press at that spot lands outside the menu and would close
// it. Set from the hold until the finger has been up for a moment.
const hold = useRef<{ liftedAt: number | null } | null>(null);
const holdStillDown = () => {
const liftedAt = hold.current?.liftedAt;
return liftedAt === null || (liftedAt !== undefined && performance.now() - liftedAt < 400);
};
const openFromHold = useCallback(() => {
hold.current = { liftedAt: null };
setMenuOpen(true);
}, []);
const onContextMenu = useCallback(
(event: React.MouseEvent<HTMLDivElement>) => {
if (!event.currentTarget.contains(event.target as Node)) return;
event.preventDefault();
if (canEdit) openMenu();
if (!canEdit) return;
// Android sends this for a held finger, ahead of the hold's own timer.
hold.current =
(event.nativeEvent as PointerEvent).pointerType === "touch" ? { liftedAt: null } : null;
setMenuOpen(true);
},
[canEdit, openMenu],
[canEdit],
);
const touchHold = useTouchHoldMenu<HTMLDivElement>({ open: openMenu, onContextMenu });
// A press on the open menu is the menu's: as a hold on the pill it would
// swallow the tap on Rename or Remove.
const touchHold = useTouchHoldMenu<HTMLDivElement>({ open: openFromHold, onContextMenu });
const lift = () => {
if (hold.current?.liftedAt === null) hold.current.liftedAt = performance.now();
};
const pillTouchHold = {
...touchHold,
// A new press ends the hold. One on the open menu is the menu's own: timed
// as another hold on the pill, it would swallow the tap on Rename or Remove.
onPointerDownCapture: (event: React.PointerEvent<HTMLDivElement>) => {
if (onPill(event)) touchHold.onPointerDownCapture(event);
hold.current = null;
touchHold.onPointerDownCapture(event);
if (!onPill(event)) touchHold.onPointerCancel();
},
onPointerUp: () => {
lift();
touchHold.onPointerUp();
},
onClickCapture: (event: React.MouseEvent<HTMLDivElement>) => {
if (onPill(event)) touchHold.onClickCapture(event);
onPointerCancel: () => {
lift();
touchHold.onPointerCancel();
},
};

Expand Down Expand Up @@ -305,7 +330,17 @@ function AgentPill(props: {
{/* Mounted while the name box is open too: a menu that unmounted
as Rename opened it would pull focus back from the box. */}
{hasMenu ? (
<Menu open={menuOpen} onOpenChange={setMenuOpen}>
<Menu
open={menuOpen}
onOpenChange={(open, details) => {
// The finger that held the pill, lifting.
if (!open && details.reason === "outside-press" && holdStillDown()) {
details.cancel();
return;
}
setMenuOpen(open);
}}
>
<MenuTrigger
render={
<button
Expand Down
Loading