Skip to content

Commit eceeab4

Browse files
fix(web): Cmd+F in the Files panel focuses its search (#34346)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 1264c20 commit eceeab4

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

‎apps/web/src/components/files/FileBrowserPanel.browser.test.tsx‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import { page, userEvent } from "vite-plus/test/browser";
44

55
import { COMPOSER_MENTION_DRAG_TYPE } from "~/components/chat/composerMentionDrag";
66
import { dismissContextMenu } from "~/contextMenuFallback";
7+
import { isMacPlatform } from "~/lib/utils";
78
import { render } from "../../../test/render";
89
import FileBrowserPanel from "./FileBrowserPanel";
10+
import { dispatchPreviewAction } from "../preview/previewActionBus";
911

1012
const entries: readonly ProjectEntry[] = [
1113
{ path: "src", kind: "directory" },
@@ -115,4 +117,31 @@ describe("FileBrowserPanel", () => {
115117
row.click();
116118
await vi.waitFor(() => expect(onOpenFile).toHaveBeenCalledTimes(1));
117119
});
120+
121+
it("mod+f from a tree row, or the panel find command, focuses the file search", async () => {
122+
await using _screen = await render(
123+
<FileBrowserPanel
124+
environmentId={EnvironmentId.make("env-files")}
125+
cwd="/repo"
126+
projectName="repo"
127+
selectedPath={null}
128+
selectedPathRevealId={0}
129+
onOpenFile={() => {}}
130+
workspaceMutationId={null}
131+
answersPanelFind
132+
/>,
133+
);
134+
await vi.waitFor(() => expect(findTreeRow("README.md")).not.toBeNull());
135+
findTreeRow("README.md")!.focus();
136+
137+
const modifier = isMacPlatform(navigator.platform) ? "Meta" : "Control";
138+
await userEvent.keyboard(`{${modifier}>}f{/${modifier}}`);
139+
140+
const search = page.getByRole("searchbox", { name: "Search repo files" });
141+
await expect.element(search).toHaveFocus();
142+
143+
(search.element() as HTMLElement).blur();
144+
dispatchPreviewAction("find");
145+
await expect.element(search).toHaveFocus();
146+
});
118147
});

‎apps/web/src/components/files/FileBrowserPanel.tsx‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger";
1010
import * as AsyncResult from "effect/reactivity/AsyncResult";
1111
import * as Cause from "effect/Cause";
1212
import { ChevronsDownUpIcon, ChevronsUpDownIcon } from "lucide-react";
13-
import { useEffect, useRef, useState } from "react";
13+
import { type Ref, useEffect, useEffectEvent, useRef, useState } from "react";
1414

1515
import { Button } from "~/components/ui/button";
1616
import { InputGroup, InputGroupInput } from "~/components/ui/input-group";
@@ -33,6 +33,8 @@ import { buildFileTreePathUpdates } from "./fileTreePathReconciliation";
3333
import { loadFileAncestors } from "./loadFileAncestors";
3434
import { useDirectoryEntries } from "./useDirectoryEntries";
3535
import { useProjectPathSearch } from "~/state/queries";
36+
import { isMacPlatform } from "~/lib/utils";
37+
import { type PreviewAction, subscribePreviewAction } from "../preview/previewActionBus";
3638

3739
interface FileBrowserPanelProps {
3840
environmentId: EnvironmentId;
@@ -46,6 +48,7 @@ interface FileBrowserPanelProps {
4648
hostFolder?: boolean;
4749
onRefreshSelectedFile?: () => void;
4850
workspaceMutationId: string | null;
51+
answersPanelFind?: boolean;
4952
}
5053

5154
function treePath(entry: ProjectEntry): string {
@@ -73,16 +76,21 @@ function RefreshFilesButton(props: { isPending: boolean; onRefresh: () => void }
7376
);
7477
}
7578

76-
function FileSearchField(props: {
79+
function FileSearchField({
80+
ref,
81+
...props
82+
}: {
7783
ariaLabel: string;
7884
name: string;
7985
onClose: () => void;
8086
onValueChange: (value: string) => void;
87+
ref: Ref<HTMLInputElement>;
8188
value: string;
8289
}) {
8390
return (
8491
<InputGroup variant="ghost" className="h-7 min-w-0 flex-1">
8592
<InputGroupInput
93+
ref={ref}
8694
type="search"
8795
name={props.name}
8896
size="sm"
@@ -142,6 +150,7 @@ export default function FileBrowserPanel({
142150
onRefreshSelectedFile,
143151
workspaceMutationId,
144152
hostFolder = false,
153+
answersPanelFind = false,
145154
}: FileBrowserPanelProps) {
146155
const panelPath = (relativePath: string) =>
147156
hostFolder ? joinHostPath(cwd, relativePath) : relativePath;
@@ -320,6 +329,20 @@ export default function FileBrowserPanel({
320329
setQuery("");
321330
search.close();
322331
};
332+
const searchInputRef = useRef<HTMLInputElement>(null);
333+
const focusSearch = () => {
334+
searchInputRef.current?.focus();
335+
searchInputRef.current?.select();
336+
};
337+
const onPanelAction = useEffectEvent((action: PreviewAction) => {
338+
// Hidden panel tabs stay mounted; only the visible Files tab answers.
339+
if (action === "find" && searchInputRef.current?.getClientRects().length) focusSearch();
340+
});
341+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to the panel find command, which fires while focus is anywhere in the right panel
342+
useEffect(() => {
343+
if (!answersPanelFind) return;
344+
return subscribePreviewAction(onPanelAction);
345+
}, [answersPanelFind]);
323346
const expandedPathsRef = useRef(new Set<string>());
324347
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to the Pierre tree model to load directories as they are expanded
325348
useEffect(() => {
@@ -482,13 +505,21 @@ export default function FileBrowserPanel({
482505
// Capture dragstart before the tree selects the row so selection sees the drag flag.
483506
onDragStartCapture={(event) => dragMention.handleDragStart(event.nativeEvent)}
484507
onDragEnd={() => dragMention.handleDragEnd((path) => model.getItem(path)?.deselect())}
508+
onKeyDown={(event) => {
509+
const isModPressed = isMacPlatform(navigator.platform) ? event.metaKey : event.ctrlKey;
510+
if (!isModPressed || event.altKey || event.shiftKey || event.key.toLowerCase() !== "f")
511+
return;
512+
event.preventDefault();
513+
focusSearch();
514+
}}
485515
>
486516
<div
487517
className="flex h-10 min-h-10 shrink-0 items-center gap-1 border-b border-border/60 bg-background px-2 in-data-[preview-panel-mode=inline]:mb-1 in-data-[preview-panel-mode=inline]:h-9 in-data-[preview-panel-mode=inline]:min-h-9 in-data-[preview-panel-mode=inline]:border-b-transparent"
488518
data-surface-subheader
489519
>
490520
<RefreshFilesButton isPending={isPending} onRefresh={handleRefresh} />
491521
<FileSearchField
522+
ref={searchInputRef}
492523
name="project-files-search"
493524
ariaLabel={`Search ${projectName} files`}
494525
value={search.value}

‎apps/web/src/components/files/FilePreviewPanel.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,7 @@ export default function FilePreviewPanel({
16381638
}
16391639
selectedPathRevealId={revealRequestId}
16401640
onOpenFile={onOpenFile}
1641+
answersPanelFind={!previewPath}
16411642
workspaceMutationId={workspaceMutationId}
16421643
{...(previewPath && !isMedia && !isPdf
16431644
? {

0 commit comments

Comments
 (0)