Open History to see the last commit
1import { Spinner } from "~/components/ui/spinner";
2import { AuthFilesystemReadScope, type ProjectContentMatch } from "@t3tools/contracts";
3
4import { useEffect, useState, type ReactNode } from "react";
5
6import { useActiveProjectTarget, type ActiveProjectTarget } from "~/hooks/useActiveProjectTarget";
7import { useTheme } from "~/hooks/useTheme";
8import { cn } from "~/lib/utils";
9import { useRightPanelStore } from "~/rightPanelStore";
10import { useProjectContentSearch } from "~/state/queries";
11import { readEnvironmentScope } from "~/state/session";
12
13import { PierreEntryIcon } from "../chat/PierreEntryIcon";
14import { CommandPaletteContent } from "../CommandPaletteContent";
15import { ScrollArea } from "../ui/scroll-area";
16import { Toggle } from "../ui/toggle";
17import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
18import { HighlightedSearchLine } from "./HighlightedSearchLine";
19
20interface ProjectContentSearchDialogProps {
21 readonly onOpenChange: (open: boolean) => void;
22 /** Text typed while the dialog loaded. */
23 readonly initialQuery?: string;
24}
25
26/**
27 * Result rows are syntax highlighted, so mounting all 500 possible rows at once
28 * stalls the UI.
29 */
30const VISIBLE_MATCH_WINDOW = 100;
31
32interface MatchGroup {
33 readonly path: string;
34 readonly matches: ReadonlyArray<ProjectContentMatch & { readonly resultIndex: number }>;
35}
36
37function splitPath(path: string): { readonly name: string; readonly directory: string } {
38 const separator = path.lastIndexOf("/");
39 return separator === -1
40 ? { name: path, directory: "" }
41 : { name: path.slice(separator + 1), directory: path.slice(0, separator) };
42}
43
44function groupMatches(matches: ReadonlyArray<ProjectContentMatch>): MatchGroup[] {
45 const groups = new Map<string, Array<ProjectContentMatch & { readonly resultIndex: number }>>();
46 matches.forEach((match, resultIndex) => {
47 const group = groups.get(match.path);
48 const indexedMatch = { ...match, resultIndex };
49 if (group) {
50 group.push(indexedMatch);
51 } else {
52 groups.set(match.path, [indexedMatch]);
53 }
54 });
55 return [...groups].map(([path, groupedMatches]) => ({ path, matches: groupedMatches }));
56}
57
58function SearchOptionButton(props: {
59 readonly active: boolean;
60 readonly disabled: boolean;
61 readonly label: string;
62 readonly onClick: () => void;
63 readonly children: ReactNode;
64}) {
65 return (
66 <Tooltip>
67 <TooltipTrigger
68 render={
69 <Toggle
70 aria-label={props.label}
71 disabled={props.disabled}
72 pressed={props.active}
73 size="segmented"
74 variant="segmented"
75 onClick={props.onClick}
76 />
77 }
78 >
79 <span className="font-mono">{props.children}</span>
80 </TooltipTrigger>
81 <TooltipPopup side="top">{props.label}</TooltipPopup>
82 </Tooltip>
83 );
84}
85
86function EmptyContentSearchDialog() {
87 return (
88 <CommandPaletteContent
89 aria-label="Search project contents"
90 escapeLabel="Back"
91 footerActionLabel="Open file"
92 inputProps={{ disabled: true, placeholder: "Search project contents…" }}
93 mode="none"
94 panelSize="fill"
95 testId="project-content-search"
96 value=""
97 >
98 <p className="m-auto px-6 text-center text-muted-foreground text-sm">
99 Open a project to search its files.
100 </p>
101 </CommandPaletteContent>
102 );
103}
104
105function OpenContentSearchDialog(props: {
106 readonly onOpenChange: (open: boolean) => void;
107 readonly target: ActiveProjectTarget;
108 readonly initialQuery?: string;
109}) {
110 const { target } = props;
111 const { resolvedTheme } = useTheme();
112 const [query, setQuery] = useState(props.initialQuery ?? "");
113 const [caseSensitive, setCaseSensitive] = useState(false);
114 const [wholeWord, setWholeWord] = useState(false);
115 const [useRegex, setUseRegex] = useState(false);
116 const [selectedIndex, setSelectedIndex] = useState(0);
117
118 const [visibleCount, setVisibleCount] = useState(VISIBLE_MATCH_WINDOW);
119
120 const search = useProjectContentSearch({
121 environmentId: target.environmentId,
122 cwd: target.cwd,
123 query,
124 caseSensitive,
125 wholeWord,
126 useRegex,
127 });
128 const canOpenMatches = search.canReadFiles && !search.isPending && search.error === null;
129 const matches = search.matches;
130 // New results restart the selection and the rendered window.
131 const [resultsFor, setResultsFor] = useState(matches);
132 if (resultsFor !== matches) {
133 setResultsFor(matches);
134 setSelectedIndex(0);
135 setVisibleCount(VISIBLE_MATCH_WINDOW);
136 }
137 // Keyboard selection past the window grows it before the row has to scroll into view.
138 if (selectedIndex >= visibleCount) {
139 setVisibleCount(selectedIndex + VISIBLE_MATCH_WINDOW);
140 }
141 const visibleMatches = matches.slice(0, visibleCount);
142 const groups = groupMatches(visibleMatches);
143
144 useEffect(() => {
145 document
146 .querySelector<HTMLElement>(`[data-content-search-result="${selectedIndex}"]`)
147 ?.scrollIntoView({ block: "nearest" });
148 }, [selectedIndex]);
149
150 const observeLoadMoreSentinel = (sentinel: HTMLElement | null) => {
151 if (!sentinel) return;
152 const observer = new IntersectionObserver((entries) => {
153 if (entries.some((entry) => entry.isIntersecting)) {
154 setVisibleCount((current) => current + VISIBLE_MATCH_WINDOW);
155 }
156 });
157 observer.observe(sentinel);
158 return () => observer.disconnect();
159 };
160
161 const openMatch = (match: ProjectContentMatch) => {
162 if (!canOpenMatches || !readEnvironmentScope(target.environmentId, AuthFilesystemReadScope)) {
163 return;
164 }
165 props.onOpenChange(false);
166 useRightPanelStore.getState().openFile(target.threadRef, match.path, match.lineNumber);
167 };
168 const fileCount = new Set(matches.map((match) => match.path)).size;
169 const showSearchStatus =
170 search.canReadFiles &&
171 (search.hasQuery || search.isPending || search.error !== null || search.invalidRegex);
172
173 return (
174 <CommandPaletteContent
175 aria-label={`Search file contents in ${target.projectName}`}
176 escapeLabel="Back"
177 footerActionLabel="Open file"
178 inputAccessory={
179 <div className="absolute inset-e-2.5 top-1/2 flex shrink-0 -translate-y-1/2 items-center gap-0.5 rounded-lg border bg-muted/30 p-0.5">
180 <SearchOptionButton
181 active={caseSensitive}
182 disabled={!search.canReadFiles}
183 label="Match case"
184 onClick={() => setCaseSensitive((current) => !current)}
185 >
186 Aa
187 </SearchOptionButton>
188 <SearchOptionButton
189 active={wholeWord}
190 disabled={!search.canReadFiles}
191 label="Match whole word"
192 onClick={() => setWholeWord((current) => !current)}
193 >
194 <span className="underline decoration-2 underline-offset-2">ab</span>
195 </SearchOptionButton>
196 <SearchOptionButton
197 active={useRegex}
198 disabled={!search.canReadFiles}
199 label="Use regular expression"
200 onClick={() => setUseRegex((current) => !current)}
201 >
202 .*
203 </SearchOptionButton>
204 </div>
205 }
206 inputProps={{
207 className: "pe-30",
208 disabled: !search.canReadFiles,
209 placeholder: `Search in ${target.projectName}`,
210 onKeyDown: (event) => {
211 if (event.key === "ArrowDown" && matches.length > 0) {
212 event.preventDefault();
213 setSelectedIndex((current) => (current + 1) % matches.length);
214 } else if (event.key === "ArrowUp" && matches.length > 0) {
215 event.preventDefault();
216 setSelectedIndex((current) => (current - 1 + matches.length) % matches.length);
217 } else if (event.key === "Enter") {
218 if (!canOpenMatches) {
219 event.preventDefault();
220 return;
221 }
222 const match = matches[selectedIndex];
223 if (match) {
224 event.preventDefault();
225 openMatch(match);
226 }
227 }
228 },
229 }}
230 mode="none"
231 onValueChange={setQuery}
232 panelSize="fill"
233 testId="project-content-search"
234 value={query}
235 >
236 {showSearchStatus ? (
237 <div className="flex h-9 shrink-0 items-center border-b px-3 text-xs text-muted-foreground">
238 {search.isPending ? (
239 <span className="flex items-center gap-2">
240 <Spinner size="sm" /> Searching…
241 </span>
242 ) : search.error ? (
243 <span className="text-destructive">{search.error}</span>
244 ) : search.invalidRegex ? (
245 <span className="text-destructive">Invalid regular expression</span>
246 ) : (
247 `${matches.length.toLocaleString()}${search.truncated ? "+" : ""} results in ${fileCount.toLocaleString()} files`
248 )}
249 </div>
250 ) : null}
251
252 {matches.length === 0 ? (
253 <div className="flex flex-1 items-center justify-center px-6 text-center text-sm text-muted-foreground">
254 {search.isCheckingAccess
255 ? "Checking file access…"
256 : !search.canReadFiles
257 ? search.error
258 : search.hasQuery && !search.isPending && !search.error
259 ? "No results found."
260 : "Type to search across your project."}
261 </div>
262 ) : (
263 <ScrollArea className="min-h-0 flex-1" scrollFade>
264 <div className="py-2">
265 {groups.map((group) => {
266 const path = splitPath(group.path);
267 return (
268 <section className="pb-2" key={group.path}>
269 <div className="sticky top-0 z-10 flex h-8 items-center gap-2 bg-popover/95 px-3 text-xs backdrop-blur-sm">
270 <PierreEntryIcon
271 pathValue={group.path}
272 kind="file"
273 theme={resolvedTheme}
274 className="size-3.5"
275 />
276 <span className="font-medium text-foreground">{path.name}</span>
277 {path.directory ? (
278 <span className="min-w-0 truncate text-muted-foreground">
279 {path.directory}
280 </span>
281 ) : null}
282 <span className="ml-auto rounded-full bg-muted px-1.5 py-0.5 tabular-nums text-3xs text-muted-foreground">
283 {group.matches.length}
284 </span>
285 </div>
286 {group.matches.map((match) => (
287 <button
288 type="button"
289 key={`${match.path}:${match.lineNumber}:${match.resultIndex}`}
290 data-content-search-result={match.resultIndex}
291 className={cn(
292 "flex h-7 w-full min-w-0 items-center gap-3 px-3 text-left font-mono text-xs hover:bg-accent/60 disabled:pointer-events-none",
293 match.resultIndex === selectedIndex && "bg-accent text-accent-foreground",
294 )}
295 disabled={!canOpenMatches}
296 onMouseEnter={() => setSelectedIndex(match.resultIndex)}
297 onClick={() => openMatch(match)}
298 >
299 <span className="w-10 shrink-0 text-right tabular-nums text-muted-foreground/70">
300 {match.lineNumber}
301 </span>
302 <span className="min-w-0 flex-1 truncate whitespace-pre">
303 <HighlightedSearchLine
304 match={match}
305 path={group.path}
306 theme={resolvedTheme}
307 />
308 </span>
309 </button>
310 ))}
311 </section>
312 );
313 })}
314 {matches.length > visibleCount ? (
315 <div ref={observeLoadMoreSentinel} className="h-8" aria-hidden="true" />
316 ) : null}
317 </div>
318 </ScrollArea>
319 )}
320 </CommandPaletteContent>
321 );
322}
323
324export function ProjectContentSearchDialog(props: ProjectContentSearchDialogProps) {
325 const target = useActiveProjectTarget();
326
327 return target ? (
328 <OpenContentSearchDialog
329 key={`${target.environmentId}:${target.cwd}`}
330 onOpenChange={props.onOpenChange}
331 target={target}
332 {...(props.initialQuery ? { initialQuery: props.initialQuery } : {})}
333 />
334 ) : (
335 <EmptyContentSearchDialog />
336 );
337}