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

ProjectFaviconPickerDialog.tsx

142 lines5.5 KB

1import { useAtomValue } from "@effect/atom-react";
2import type { EnvironmentId } from "@t3tools/contracts";
3import { isWindowsAbsolutePath } from "@t3tools/shared/path";
4import { 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.";
26export function canPickExternalProjectFavicon(cwd: string, platform: string): boolean {
27 return !isWindowsPlatform(platform) || isWindowsAbsolutePath(cwd);
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 { onPickExternal: pickExternal, onSelect } = props;
52 const fileManagerName = getLocalFileManagerName(
53 typeof navigator === "undefined" ? "" : navigator.platform,
54 );
55 const items: CommandPaletteActionItem[] = getProjectFilePickerMatches(
56 result.entries,
57 result.matchedQuery,
58 ).map((match) => ({
59 kind: "action",
60 value: `project-favicon:${match.path}`,
61 searchTerms: [match.name, match.path],
62 title: match.name,
63 description: match.path,
64 icon: <PierreEntryIcon pathValue={match.path} kind="file" theme={resolvedTheme} />,
65 run: async () => onSelect(match.path),
66 }));
67​
68 return (
69 <CommandDialog open={props.open} onOpenChange={props.onOpenChange}>
70 {props.open ? (
71 <CommandDialogPopup
72 aria-label="Choose project icon"
73 className="overflow-hidden"
74 onBackdropPointerDown={() => props.onOpenChange(false)}
75 >
76 <CommandPaletteContent
77 aria-label="Choose project icon"
78 autoHighlight="always"
79 escapeLabel="Close"
80 footerActionLabel="Select icon"
81 footerTrailing={
82 pickExternal ? (
83 <CommandFooterAction
84 disabled={isPickingExternal}
85 onClick={() => {
86 setIsPickingExternal(true);
87 void pickExternal()
88 .then((path) => {
89 if (!path) return;
90 props.onOpenChange(false);
91 onSelect(path);
92 })
93 .catch((error: unknown) => {
94 toastManager.add({
95 type: "error",
96 title: "Could not open image picker",
97 description:
98 error instanceof Error ? error.message : "An error occurred.",
99 });
100 })
101 .finally(() => setIsPickingExternal(false));
102 }}
103 >
104 {`Open in ${fileManagerName}`}
105 </CommandFooterAction>
106 ) : null
107 }
108 inputProps={{ placeholder: "Search image files…" }}
109 mode="none"
110 onItemHighlighted={(value) => {
111 setHighlightedItemValue(typeof value === "string" ? value : null);
112 }}
113 onValueChange={(value) => {
114 setHighlightedItemValue(null);
115 setQuery(value);
116 }}
117 panelSize="tall-list"
118 testId="project-favicon-picker"
119 value={query}
120 >
121 <CommandPaletteResults
122 groups={
123 items.length > 0
124 ? [{ value: "project-favicon-files", label: props.projectName, items }]
125 : []
126 }
127 highlightedItemValue={highlightedItemValue}
128 isActionsOnly={false}
129 keybindings={keybindings}
130 onExecuteItem={(item) => {
131 if (item.kind !== "action") return;
132 props.onOpenChange(false);
133 void item.run();
134 }}
135 emptyStateMessage={emptyMessage(query, result.error, result.isPending)}
136 />
137 </CommandPaletteContent>
138 </CommandDialogPopup>
139 ) : null}
140 </CommandDialog>
141 );