Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import type { EnvironmentId } from "@t3tools/contracts";
3import { isWindowsAbsolutePath } from "@t3tools/shared/path";
4import { useMemo, useState } from "react";
5
6import { primaryServerKeybindingsAtom } from "~/state/server";
7import { useTheme } from "~/hooks/useTheme";
8import { getLocalFileManagerName, isWindowsPlatform } from "~/lib/utils";
9import { CommandPaletteContent } from "../CommandPaletteContent";
10import type { CommandPaletteActionItem } from "../CommandPalette.logic";
11import { CommandPaletteResults } from "../CommandPaletteResults";
12import { PierreEntryIcon } from "../chat/PierreEntryIcon";
13import {
14 getProjectFilePickerMatches,
15 PROJECT_FILE_PICKER_RESULT_LIMIT,
16} from "../files/ProjectFilePicker.logic";
17import { useProjectFilePickerQuery } from "../files/projectFilesQueryState";
18import { CommandDialog, CommandDialogPopup, CommandFooterAction } from "../ui/command";
19import { toastManager } from "../ui/toast";
20
21function emptyMessage(query: string, error: string | null, isPending: boolean): string {
22 if (error) return error;
23 if (isPending) return query.trim() ? "Searching project files…" : "Indexing project files…";
24 return query.trim() ? "No matching image files." : "No image files found.";
25}
26export function canPickExternalProjectFavicon(cwd: string, platform: string): boolean {
27 return !isWindowsPlatform(platform) || isWindowsAbsolutePath(cwd);
28}
29
30export function ProjectFaviconPickerDialog(props: {
31 readonly cwd: string;
32 readonly environmentId: EnvironmentId;
33 readonly onOpenChange: (open: boolean) => void;
34 readonly onPickExternal?: () => Promise<string | null>;
35 readonly onSelect: (path: string) => void;
36 readonly open: boolean;
37 readonly projectName: string;
38}) {
39 const [query, setQuery] = useState("");
40 const [highlightedItemValue, setHighlightedItemValue] = useState<string | null>(null);
41 const [isPickingExternal, setIsPickingExternal] = useState(false);
42 const result = useProjectFilePickerQuery(
43 props.environmentId,
44 props.cwd,
45 query,
46 PROJECT_FILE_PICKER_RESULT_LIMIT,
47 { imageOnly: true },
48 );
49 const { resolvedTheme } = useTheme();
50 const keybindings = useAtomValue(primaryServerKeybindingsAtom);
51 const pickExternal = props.onPickExternal;
52 const fileManagerName = getLocalFileManagerName(
53 typeof navigator === "undefined" ? "" : navigator.platform,
54 );
55 const items = useMemo<CommandPaletteActionItem[]>(
56 () =>
57 getProjectFilePickerMatches(result.entries, result.matchedQuery).map((match) => ({
58 kind: "action",
59 value: `project-favicon:${match.path}`,
60 searchTerms: [match.name, match.path],
61 title: match.name,
62 description: match.path,
63 icon: <PierreEntryIcon pathValue={match.path} kind="file" theme={resolvedTheme} />,
64 run: async () => props.onSelect(match.path),
65 })),
66 [props.onSelect, resolvedTheme, result.entries, result.matchedQuery],
67 );
68
69 return (
70 <CommandDialog open={props.open} onOpenChange={props.onOpenChange}>
71 {props.open ? (
72 <CommandDialogPopup
73 aria-label="Choose project icon"
74 className="overflow-hidden p-0"
75 onBackdropPointerDown={() => props.onOpenChange(false)}
76 >
77 <CommandPaletteContent
78 aria-label="Choose project icon"
79 autoHighlight="always"
80 escapeLabel="Close"
81 footerActionLabel="Select icon"
82 footerTrailing={
83 pickExternal ? (
84 <CommandFooterAction
85 disabled={isPickingExternal}
86 onClick={() => {
87 setIsPickingExternal(true);
88 void pickExternal()
89 .then((path) => {
90 if (!path) return;
91 props.onOpenChange(false);
92 props.onSelect(path);
93 })
94 .catch((error: unknown) => {
95 toastManager.add({
96 type: "error",
97 title: "Could not open image picker",
98 description:
99 error instanceof Error ? error.message : "An error occurred.",
100 });
101 })
102 .finally(() => setIsPickingExternal(false));
103 }}
104 >
105 {`Open in ${fileManagerName}`}
106 </CommandFooterAction>
107 ) : null
108 }
109 inputProps={{ placeholder: "Search image files…" }}
110 mode="none"
111 onItemHighlighted={(value) => {
112 setHighlightedItemValue(typeof value === "string" ? value : null);
113 }}
114 onValueChange={(value) => {
115 setHighlightedItemValue(null);
116 setQuery(value);
117 }}
118 panelClassName="max-h-[min(34rem,76vh)]"
119 testId="project-favicon-picker"
120 value={query}
121 >
122 <CommandPaletteResults
123 groups={
124 items.length > 0
125 ? [{ value: "project-favicon-files", label: props.projectName, items }]
126 : []
127 }
128 highlightedItemValue={highlightedItemValue}
129 isActionsOnly={false}
130 keybindings={keybindings}
131 onExecuteItem={(item) => {
132 if (item.kind !== "action") return;
133 props.onOpenChange(false);
134 void item.run();
135 }}
136 emptyStateMessage={emptyMessage(query, result.error, result.isPending)}
137 />
138 </CommandPaletteContent>
139 </CommandDialogPopup>
140 ) : null}
141 </CommandDialog>
142 );
143}