Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectFilePicker.tsx

164 lines5.2 KB

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