Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { msg, T, useGT, useMessages } from "gt-react";
3import { useState, type ReactNode } from "react";
4
5import type { MessageResolver } from "~/i18n/messages";
6
7import { useActiveProjectTarget, type ActiveProjectTarget } from "~/hooks/useActiveProjectTarget";
8import { useTheme } from "~/hooks/useTheme";
9import { useRightPanelStore } from "~/rightPanelStore";
10import { primaryServerKeybindingsAtom } from "~/state/server";
11
12import { PierreEntryIcon } from "../chat/PierreEntryIcon";
13import { CommandPaletteContent } from "../CommandPaletteContent";
14import { type CommandPaletteActionItem } from "../CommandPalette.logic";
15import { CommandPaletteResults } from "../CommandPaletteResults";
16import {
17 getProjectFilePickerMatches,
18 groupMatchRanges,
19 PROJECT_FILE_PICKER_RESULT_LIMIT,
20} from "./ProjectFilePicker.logic";
21import { useProjectFilePickerQuery } from "./projectFilesQueryState";
22
23interface ProjectFilePickerProps {
24 readonly setOpen: (open: boolean) => void;
25}
26
27function HighlightedFuzzyText(props: {
28 readonly active: boolean;
29 readonly indices: ReadonlyArray<number>;
30 readonly value: string;
31}) {
32 if (!props.active) return props.value;
33
34 const parts: ReactNode[] = [];
35 let cursor = 0;
36 for (const [start, end] of groupMatchRanges(props.indices)) {
37 if (cursor < start) parts.push(props.value.slice(cursor, start));
38 parts.push(
39 <strong className="font-semibold text-foreground" key={start}>
40 {props.value.slice(start, end)}
41 </strong>,
42 );
43 cursor = end;
44 }
45 if (cursor < props.value.length) parts.push(props.value.slice(cursor));
46
47 return <span className="text-muted-foreground">{parts}</span>;
48}
49
50function getEmptyStateMessage(
51 query: string,
52 error: string | null,
53 isPending: boolean,
54 resolve: MessageResolver,
55): string {
56 if (error) return error;
57 const isSearching = query.trim().length > 0;
58 if (isPending)
59 return isSearching
60 ? resolve(msg("Searching workspace files…"))
61 : resolve(msg("Indexing workspace files…"));
62 return isSearching ? resolve(msg("No matching files.")) : resolve(msg("No files found."));
63}
64
65function EmptyProjectFilePicker() {
66 const gt = useGT();
67 return (
68 <CommandPaletteContent
69 aria-label={gt`File picker`}
70 escapeLabel={gt({ context: "button, leaves the file picker" })`Back`}
71 footerActionLabel={gt`Open file`}
72 inputProps={{ disabled: true, placeholder: gt`Search files…` }}
73 mode="none"
74 testId="project-file-picker"
75 value=""
76 >
77 <div className="py-10 text-center text-sm text-muted-foreground">
78 <T>Open a project to search its files.</T>
79 </div>
80 </CommandPaletteContent>
81 );
82}
83
84function OpenProjectFilePicker(props: ProjectFilePickerProps & { target: ActiveProjectTarget }) {
85 const { target } = props;
86 const gt = useGT();
87 const messages = useMessages();
88 const [query, setQuery] = useState("");
89 const [highlightedItemValue, setHighlightedItemValue] = useState<string | null>(null);
90 const result = useProjectFilePickerQuery(
91 target.environmentId,
92 target.cwd,
93 query,
94 PROJECT_FILE_PICKER_RESULT_LIMIT,
95 undefined,
96 messages,
97 );
98 const { resolvedTheme } = useTheme();
99 const keybindings = useAtomValue(primaryServerKeybindingsAtom);
100 const matches = getProjectFilePickerMatches(result.entries, result.matchedQuery);
101 const hasMatchedQuery = /\S/.test(result.matchedQuery);
102 const items: CommandPaletteActionItem[] = matches.map((match) => ({
103 kind: "action",
104 value: `file:${match.path}`,
105 searchTerms: [match.name, match.path],
106 title: (
107 <HighlightedFuzzyText
108 active={hasMatchedQuery}
109 value={match.name}
110 indices={match.nameMatchIndices}
111 />
112 ),
113 description: (
114 <HighlightedFuzzyText
115 active={hasMatchedQuery}
116 value={match.path}
117 indices={match.pathMatchIndices}
118 />
119 ),
120 icon: <PierreEntryIcon pathValue={match.path} kind="file" theme={resolvedTheme} />,
121 run: async () => {
122 useRightPanelStore.getState().openFile(target.threadRef, match.path);
123 },
124 }));
125
126 // Kept rows never re-trigger the primitive's auto-highlight event, so default to the first row.
127 const activeItemValue = items.some((item) => item.value === highlightedItemValue)
128 ? highlightedItemValue
129 : (items[0]?.value ?? null);
130
131 const emptyStateMessage = getEmptyStateMessage(query, result.error, result.isPending, messages);
132
133 return (
134 <CommandPaletteContent
135 aria-label={gt`File picker`}
136 autoHighlight="always"
137 escapeLabel={gt({ context: "button, leaves the file picker" })`Back`}
138 footerActionLabel={gt`Open file`}
139 inputProps={{ placeholder: gt`Search files…` }}
140 mode="none"
141 items={items.map((item) => item.value)}
142 onItemHighlighted={(value) => {
143 setHighlightedItemValue(typeof value === "string" ? value : null);
144 }}
145 onValueChange={(value) => {
146 setHighlightedItemValue(null);
147 setQuery(value);
148 }}
149 panelSize="tall-list"
150 testId="project-file-picker"
151 value={query}
152 >
153 <CommandPaletteResults
154 groups={
155 items.length > 0 ? [{ value: "project-files", label: target.projectName, items }] : []
156 }
157 highlightedItemValue={activeItemValue}
158 isActionsOnly={false}
159 keybindings={keybindings}
160 onExecuteItem={(item) => {
161 if (item.kind !== "action" || result.isPending) return;
162 props.setOpen(false);
163 void item.run();
164 }}
165 emptyStateMessage={emptyStateMessage}
166 />
167 </CommandPaletteContent>
168 );
169}
170
171export function ProjectFilePicker(props: ProjectFilePickerProps) {
172 const target = useActiveProjectTarget();
173
174 if (!target) {
175 return <EmptyProjectFilePicker />;
176 }
177
178 return <OpenProjectFilePicker setOpen={props.setOpen} target={target} />;
179}