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

ProjectFilePicker.tsx

173 lines5.5 KB

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;
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>;
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."));
64​
65function EmptyProjectFilePicker() {
66 const gt = useGT();
67 return (
68 <CommandPaletteContent
69 aria-label={gt("File picker")}
70 escapeLabel={gt("Back", { $context: "button, leaves the file picker" })}
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 );
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 const emptyStateMessage = getEmptyStateMessage(query, result.error, result.isPending, messages);
127​
128 return (
129 <CommandPaletteContent
130 aria-label={gt("File picker")}
131 autoHighlight="always"
132 escapeLabel={gt("Back", { $context: "button, leaves the file picker" })}
133 footerActionLabel={gt("Open file")}
134 inputProps={{ placeholder: gt("Search files…") }}
135 mode="none"
136 onItemHighlighted={(value) => {
137 setHighlightedItemValue(typeof value === "string" ? value : null);
138 }}
139 onValueChange={(value) => {
140 setHighlightedItemValue(null);
141 setQuery(value);
142 }}
143 panelSize="tall-list"
144 testId="project-file-picker"
145 value={query}
146 >
147 <CommandPaletteResults
148 groups={
149 items.length > 0 ? [{ value: "project-files", label: target.projectName, items }] : []
150 }
151 highlightedItemValue={highlightedItemValue}
152 isActionsOnly={false}
153 keybindings={keybindings}
154 onExecuteItem={(item) => {
155 if (item.kind !== "action") return;
156 props.setOpen(false);
157 void item.run();
158 }}
159 emptyStateMessage={emptyStateMessage}
160 />
161 </CommandPaletteContent>
162 );
164​
165export function ProjectFilePicker(props: ProjectFilePickerProps) {
166 const target = useActiveProjectTarget();
167​
168 if (!target) {
169 return <EmptyProjectFilePicker />;
170 }
171​
172 return <OpenProjectFilePicker setOpen={props.setOpen} target={target} />;