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
13 changes: 11 additions & 2 deletions apps/web/src/components/chat/ChatHeader.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { expect, it, vi } from "vite-plus/test";
import { userEvent } from "vite-plus/test/browser";

const persisted = vi.hoisted(() => ({ title: "" }));
const openMenu = vi.hoisted(() => vi.fn());
vi.mock(import("../../state/session"), async (importOriginal) => ({
...(await importOriginal()),
useEnvironmentScope: () => true,
Expand All @@ -17,10 +18,10 @@ vi.mock(import("../../state/use-orchestration-command"), async (importOriginal)
}) as never,
}));
vi.mock(import("~/hooks/useThreadActionMenu"), () => ({
useThreadActionMenu: () => ({ openMenu: () => {}, closeMenu: () => {} }),
useThreadActionMenu: () => ({ openMenu, closeMenu: () => {} }),
}));

import { render } from "../../../test/render";
import { render, stubGlobal } from "../../../test/render";
import { ChatHeader } from "./ChatHeader";

function header(title: string) {
Expand Down Expand Up @@ -71,3 +72,11 @@ it.each(["blur", "Enter"] as const)(
expect(persisted.title).toBe("My edited title");
},
);

it("opens the thread menu on a desktop title click without waiting", async () => {
openMenu.mockClear();
using _bridge = stubGlobal("desktopBridge", {});
await using screen = await render(header("New thread"));
await screen.getByRole("button", { name: "Thread actions for New thread" }).click();
expect(openMenu).toHaveBeenCalledOnce();
});
43 changes: 3 additions & 40 deletions apps/web/src/components/chat/ChatHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ function resolveRenameCommit(input: {
return { action: "commit", title: trimmed };
}

// Delay the input-swallowing native menu through the double-click rename interval.
const TITLE_MENU_OPEN_DELAY_MS = 500;
export const ChatHeader = memo(function ChatHeader({
activeThreadEnvironmentId,
activeThreadId,
Expand Down Expand Up @@ -205,54 +203,21 @@ export const ChatHeader = memo(function ChatHeader({
onFind: onFindInThread,
canShareWithGuest,
});
const titleButtonRef = useRef<HTMLButtonElement | null>(null);
const titleMenuTimerRef = useRef<number | null>(null);
const cancelPendingTitleMenu = () => {
if (titleMenuTimerRef.current === null) return;
clearTimeout(titleMenuTimerRef.current);
titleMenuTimerRef.current = null;
};
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears the pending title-menu timer when the thread switches or the header unmounts
useEffect(
() => () => {
if (titleMenuTimerRef.current === null) return;
clearTimeout(titleMenuTimerRef.current);
titleMenuTimerRef.current = null;
},
// oxlint-disable-next-line react/exhaustive-effect-dependencies -- A pending title menu belongs to the thread that was clicked, so switching threads drops it.
[activeThreadEnvironmentId, activeThreadId],
);
const openTitleMenuNow = () => {
cancelPendingTitleMenu();
const rect = titleButtonRef.current?.getBoundingClientRect();
if (!rect) return;
openMenu({ x: rect.left, y: rect.bottom + 4 });
};
const openMenuFromTitle = (event: ReactMouseEvent<HTMLButtonElement>) => {
if (isTrailingDoubleClick(event.detail)) return;
const clickedChevron =
(event.target as HTMLElement).closest("[data-thread-title-chevron]") !== null;
if (event.detail === 0 || clickedChevron || window.desktopBridge === undefined) {
openTitleMenuNow();
return;
}
cancelPendingTitleMenu();
titleMenuTimerRef.current = window.setTimeout(() => {
titleMenuTimerRef.current = null;
openTitleMenuNow();
}, TITLE_MENU_OPEN_DELAY_MS);
const rect = event.currentTarget.getBoundingClientRect();
openMenu({ x: rect.left, y: rect.bottom + 4 });
};
// A native menu takes the second click, so only the in-page menu lets a double-click reach here.
const handleTitleDoubleClick = (event: ReactMouseEvent) => {
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
if ((event.target as HTMLElement).closest("[data-thread-title-chevron]") !== null) return;
cancelPendingTitleMenu();
closeMenu();
startRename();
};
const handleHeaderContextMenu = (event: ReactMouseEvent) => {
if (renamingTitle !== null) return;
if (!isServerThread && onOpenProjectSettings === undefined) return;
cancelPendingTitleMenu();
event.preventDefault();
if (!isServerThread) {
const api = readLocalApi();
Expand Down Expand Up @@ -380,13 +345,11 @@ export const ChatHeader = memo(function ChatHeader({
<TooltipTrigger
render={
<button
ref={titleButtonRef}
type="button"
aria-label={`Thread actions for ${activeThreadTitle}`}
aria-haspopup="menu"
onClick={openMenuFromTitle}
onDoubleClick={canOperateThread ? handleTitleDoubleClick : undefined}
onBlur={cancelPendingTitleMenu}
className="group/thread-title inline-flex min-w-0 max-w-full cursor-pointer items-center gap-1 rounded-sm text-left focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
/>
}
Expand Down
Loading