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