Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import type { EnvironmentId } from "@t3tools/contracts";
3import { useMemo, useState } from "react";
4
5import { primaryServerKeybindingsAtom } from "~/state/server";
6import { useTheme } from "~/hooks/useTheme";
7import { CommandPaletteContent } from "../CommandPaletteContent";
8import type { CommandPaletteActionItem } from "../CommandPalette.logic";
9import { CommandPaletteResults } from "../CommandPaletteResults";
10import { PierreEntryIcon } from "../chat/PierreEntryIcon";
11import {
12 getProjectFilePickerMatches,
13 PROJECT_FILE_PICKER_RESULT_LIMIT,
14} from "../files/ProjectFilePicker.logic";
15import { useProjectFilePickerQuery } from "../files/projectFilesQueryState";
16import { CommandDialog, CommandDialogPopup } from "../ui/command";
17
18function emptyMessage(query: string, error: string | null, isPending: boolean): string {
19 if (error) return error;
20 if (isPending) return query.trim() ? "Searching project files…" : "Indexing project files…";
21 return query.trim() ? "No matching image files." : "No image files found.";
22}
23
24export function ProjectFaviconPickerDialog(props: {
25 readonly cwd: string;
26 readonly environmentId: EnvironmentId;
27 readonly onOpenChange: (open: boolean) => void;
28 readonly onSelect: (path: string) => void;
29 readonly open: boolean;
30 readonly projectName: string;
31}) {
32 const [query, setQuery] = useState("");
33 const [highlightedItemValue, setHighlightedItemValue] = useState<string | null>(null);
34 const result = useProjectFilePickerQuery(
35 props.environmentId,
36 props.cwd,
37 query,
38 PROJECT_FILE_PICKER_RESULT_LIMIT,
39 { imageOnly: true },
40 );
41 const { resolvedTheme } = useTheme();
42 const keybindings = useAtomValue(primaryServerKeybindingsAtom);
43 const items = useMemo<CommandPaletteActionItem[]>(
44 () =>
45 getProjectFilePickerMatches(result.entries, result.matchedQuery).map((match) => ({
46 kind: "action",
47 value: `project-favicon:${match.path}`,
48 searchTerms: [match.name, match.path],
49 title: match.name,
50 description: match.path,
51 icon: <PierreEntryIcon pathValue={match.path} kind="file" theme={resolvedTheme} />,
52 run: async () => props.onSelect(match.path),
53 })),
54 [props.onSelect, resolvedTheme, result.entries, result.matchedQuery],
55 );
56
57 return (
58 <CommandDialog open={props.open} onOpenChange={props.onOpenChange}>
59 {props.open ? (
60 <CommandDialogPopup
61 aria-label="Choose project icon"
62 className="overflow-hidden p-0"
63 onBackdropPointerDown={() => props.onOpenChange(false)}
64 >
65 <CommandPaletteContent
66 aria-label="Choose project icon"
67 autoHighlight="always"
68 escapeLabel="Close"
69 footerActionLabel="Select icon"
70 inputProps={{ placeholder: "Search image files…" }}
71 mode="none"
72 onItemHighlighted={(value) => {
73 setHighlightedItemValue(typeof value === "string" ? value : null);
74 }}
75 onValueChange={(value) => {
76 setHighlightedItemValue(null);
77 setQuery(value);
78 }}
79 panelClassName="max-h-[min(34rem,76vh)]"
80 testId="project-favicon-picker"
81 value={query}
82 >
83 <CommandPaletteResults
84 groups={
85 items.length > 0
86 ? [{ value: "project-favicon-files", label: props.projectName, items }]
87 : []
88 }
89 highlightedItemValue={highlightedItemValue}
90 isActionsOnly={false}
91 keybindings={keybindings}
92 onExecuteItem={(item) => {
93 if (item.kind !== "action") return;
94 props.onOpenChange(false);
95 void item.run();
96 }}
97 emptyStateMessage={emptyMessage(query, result.error, result.isPending)}
98 />
99 </CommandPaletteContent>
100 </CommandDialogPopup>
101 ) : null}
102 </CommandDialog>
103 );
104}