Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { createRef, type RefObject } from "react";
import { describe, expect, it } from "vite-plus/test";

import { render } from "../../../test/render";
import { PullRequestsColumn } from "./PullRequestsColumn";

function createColumn(maximizedAway: boolean, scrollRef: RefObject<HTMLDivElement | null>) {
return (
<PullRequestsColumn
refreshing={false}
onRefresh={() => {}}
searchValue=""
involvement="all"
state="open"
host={undefined}
hostMenuOptions={[]}
onInvolvement={() => {}}
onState={() => {}}
onHost={() => {}}
searchInput={<input aria-label="Search pull requests" />}
sortMenu={null}
filtersMenu={null}
savedViewsMenu={null}
rightPanelControl={null}
titlebarControls={null}
rightPanelOpen={maximizedAway}
maximizedAway={maximizedAway}
listBody={null}
scrollRef={scrollRef}
/>
);
}

function dispatchFindShortcut() {
const event = new KeyboardEvent("keydown", {
key: "f",
ctrlKey: true,
bubbles: true,
cancelable: true,
});
window.dispatchEvent(event);
return event;
}

describe("PullRequestsColumn find shortcut", () => {
it("leaves Cmd/Ctrl+F to the browser while the detail panel is maximized", async () => {
const scrollRef = createRef<HTMLDivElement>();
await using screen = await render(createColumn(false, scrollRef));

await screen.rerender(createColumn(true, scrollRef));
expect(dispatchFindShortcut().defaultPrevented).toBe(false);

await screen.rerender(createColumn(false, scrollRef));
expect(dispatchFindShortcut().defaultPrevented).toBe(true);
await expect
.element(screen.getByRole("textbox", { name: "Search pull requests" }))
.toHaveFocus();
});
});
3 changes: 2 additions & 1 deletion apps/web/src/components/pullRequest/PullRequestsColumn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export function PullRequestsColumn({
const searchExpanded = searchOpen || searchValue.length > 0;
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- window keydown listener focuses the visible search input on Cmd/Ctrl+F
useEffect(() => {
if (maximizedAway) return;
const onKeyDown = (event: KeyboardEvent) => {
if (event.defaultPrevented) return;
if (event.key.toLowerCase() !== "f" || !(event.metaKey || event.ctrlKey)) return;
Expand All @@ -151,7 +152,7 @@ export function PullRequestsColumn({
};
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [condensed]);
}, [condensed, maximizedAway]);
if (!condensed && searchOpen) setSearchOpen(false);
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- moves DOM focus to the in-flow search when the topbar search it was typed in unmounts
useEffect(() => {
Expand Down
17 changes: 17 additions & 0 deletions apps/web/src/shortcuts/chatRouteCommands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,21 @@ describe("preview commands", () => {

expect(dispatch).toHaveBeenCalledWith(action);
});

it("preview.find leaves the key unclaimed on non-thread chat routes", () => {
using dispatch = vi.spyOn(previewActions, "dispatchPreviewAction").mockImplementation(() => {});
using register = vi.spyOn(dispatcher, "registerCommands").mockReturnValue(() => {});
const router = {
state: { matches: [{ routeId: "/_chat/pull-requests", params: {} }] },
} as unknown as AnyRouter;
using stack = new DisposableStack();
stack.defer(installChatRouteCommands(router));
const event = { preventDefault: vi.fn(), stopPropagation: vi.fn() } as unknown as KeyboardEvent;

const result = register.mock.calls[0]![0]["preview.find"]!(event);

expect(result).toBe(false);
expect(event.preventDefault).not.toHaveBeenCalled();
expect(dispatch).not.toHaveBeenCalled();
});
});
8 changes: 7 additions & 1 deletion apps/web/src/shortcuts/chatRouteCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,13 @@ export function installChatRouteCommands(router: AnyRouter): () => void {
"preview.zoomIn": zoomCommand("zoom-in"),
"preview.zoomOut": zoomCommand("zoom-out"),
"preview.resetZoom": zoomCommand("reset-zoom"),
"preview.find": previewCommand("find"),
// Unclaimed, the browser's own find runs when no thread handles panel find.
"preview.find": (event) => {
const environmentId = readRouteEnvironmentId();
if (!environmentId || !canOperatePreview(environmentId)) return false;
claim(event);
dispatchPreviewAction("find");
},
},
{
when: () =>
Expand Down
Loading