@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 281 lines 9.7 KB
Raw
1 import { deriveProjectIdentity } from "../../projectIdentity";
2 import { ProjectMonogram } from "../ProjectMonogram";
3 import type { ProjectIconColor, ProjectIconOverride } from "@t3tools/contracts";
4 import { T, useGT, useMessages } from "gt-react";
5 import { createElement, useState } from "react";
6 import {
7 filterProjectIconNames,
8 firstEmoji,
9 isValidProjectMonogram,
10 PROJECT_EMOJIS,
11 PROJECT_ICON_COLORS,
12 projectIconColorClassName,
13 } from "../../projectIconOptions";
14 import { cn } from "~/lib/utils";
15 import { lucideIconByName } from "../../lib/lucideIconByName";
16 import { Button } from "../ui/button";
17 import {
18 Dialog,
19 DialogDescription,
20 DialogFooter,
21 DialogHeader,
22 DialogPanel,
23 DialogPopup,
24 DialogTitle,
25 } from "../ui/dialog";
26 import { Input } from "../ui/input";
27 import { ScrollArea } from "../ui/scroll-area";
28 import { Toggle, ToggleGroup } from "../ui/toggle-group";
29
30 const DEFAULT_ICON = "folder-code";
31
32 function PickerIcon({ name }: { name: string }) {
33 const icon = lucideIconByName(name);
34 return icon ? createElement(icon, { className: "size-5" }) : null;
35 }
36
37 function iconLabel(name: string): string {
38 return name
39 .split("-")
40 .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
41 .join(" ");
42 }
43
44 export function ProjectIconPickerDialog({
45 current,
46 projectName,
47 open,
48 onOpenChange,
49 onSelect,
50 }: {
51 readonly current: ProjectIconOverride | null;
52 readonly projectName: string;
53 readonly open: boolean;
54 readonly onOpenChange: (open: boolean) => void;
55 readonly onSelect: (icon: ProjectIconOverride) => void;
56 }) {
57 return (
58 <Dialog open={open} onOpenChange={onOpenChange}>
59 <DialogPopup className="w-full sm:w-[32rem]">
60 {/* The popup mounts per open, so the draft below starts from `current` each time. */}
61 <ProjectIconPickerBody
62 current={current}
63 projectName={projectName}
64 onClose={() => onOpenChange(false)}
65 onSelect={onSelect}
66 />
67 </DialogPopup>
68 </Dialog>
69 );
70 }
71
72 function ProjectIconPickerBody({
73 current,
74 projectName,
75 onClose,
76 onSelect,
77 }: {
78 readonly current: ProjectIconOverride | null;
79 readonly projectName: string;
80 readonly onClose: () => void;
81 readonly onSelect: (icon: ProjectIconOverride) => void;
82 }) {
83 const gt = useGT();
84 const m = useMessages();
85 const automatic = deriveProjectIdentity(projectName);
86 const [mode, setMode] = useState<ProjectIconOverride["kind"]>(current?.kind ?? "lucide");
87 const [iconName, setIconName] = useState<string>(
88 current?.kind === "lucide" ? current.name : DEFAULT_ICON,
89 );
90 const [color, setColor] = useState<ProjectIconColor>(
91 current && current.kind !== "emoji" ? current.color : automatic.color,
92 );
93 const [letters, setLetters] = useState(
94 current?.kind === "monogram" ? current.text : automatic.monogram,
95 );
96 const [emoji, setEmoji] = useState(current?.kind === "emoji" ? current.emoji : "💻");
97 const [query, setQuery] = useState("");
98 const [customEmoji, setCustomEmoji] = useState("");
99
100 const icons = filterProjectIconNames(query);
101 const selectedColorClassName = projectIconColorClassName(color);
102 const monogram = letters.normalize("NFKC").trim().toUpperCase();
103 const validMonogram = isValidProjectMonogram(monogram);
104 const save = () => {
105 if (mode === "monogram" && !validMonogram) return;
106 onSelect(
107 mode === "monogram"
108 ? { kind: "monogram", text: monogram, color }
109 : mode === "lucide"
110 ? { kind: "lucide", name: iconName, color }
111 : { kind: "emoji", emoji },
112 );
113 onClose();
114 };
115
116 return (
117 <>
118 <DialogHeader>
119 <DialogTitle>
120 <T>Choose project icon</T>
121 </DialogTitle>
122 <DialogDescription>
123 <T>Choose an icon, emoji, or monogram.</T>
124 </DialogDescription>
125 </DialogHeader>
126 <DialogPanel className="flex min-h-0 flex-col">
127 <ToggleGroup
128 aria-label={gt("Icon type")}
129 variant="segmented"
130 value={[mode]}
131 onValueChange={(next) => {
132 const value = next[0];
133 if (value === "lucide" || value === "emoji" || value === "monogram") setMode(value);
134 }}
135 >
136 <Toggle value="lucide">
137 <T>Icons</T>
138 </Toggle>
139 <Toggle value="emoji">
140 <T>Emoji</T>
141 </Toggle>
142 <Toggle value="monogram">
143 <T>Monogram</T>
144 </Toggle>
145 </ToggleGroup>
146
147 {mode !== "emoji" ? (
148 <div>
149 <div className="mb-2 text-xs font-medium text-muted-foreground">
150 <T>Color</T>
151 </div>
152 <div className="flex flex-wrap gap-1.5" role="group" aria-label={gt("Icon color")}>
153 {PROJECT_ICON_COLORS.map((option) => (
154 <button
155 key={option.value}
156 type="button"
157 aria-label={m(option.label)}
158 aria-pressed={color === option.value}
159 className={cn(
160 "flex size-6 items-center justify-center rounded-full border border-transparent outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring",
161 color === option.value && "border-foreground/64",
162 )}
163 onClick={() => setColor(option.value)}
164 >
165 <span className={cn("size-4 rounded-full", option.swatchClassName)} />
166 </button>
167 ))}
168 </div>
169 </div>
170 ) : null}
171
172 {mode === "lucide" ? (
173 <>
174 <Input
175 type="search"
176 value={query}
177 aria-label={gt("Search Lucide icons")}
178 placeholder={gt("Search all Lucide icons")}
179 onChange={(event) => setQuery(event.currentTarget.value)}
180 />
181 <ScrollArea scrollFade className="max-h-64">
182 <div className="grid grid-cols-8 gap-1 p-0.5 sm:grid-cols-10">
183 {icons.map((name) => (
184 <button
185 key={name}
186 type="button"
187 aria-label={iconLabel(name)}
188 aria-pressed={iconName === name}
189 className={cn(
190 "flex aspect-square items-center justify-center rounded-md border border-transparent outline-none hover:bg-accent focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring",
191 iconName === name && "border-border bg-accent",
192 selectedColorClassName,
193 )}
194 onClick={() => setIconName(name)}
195 >
196 <PickerIcon name={name} />
197 </button>
198 ))}
199 </div>
200 </ScrollArea>
201 {icons.length === 0 ? (
202 <p className="py-8 text-center text-sm text-muted-foreground">
203 <T>No icons found.</T>
204 </p>
205 ) : null}
206 </>
207 ) : mode === "monogram" ? (
208 <div className="flex items-center gap-4 py-2">
209 <ProjectMonogram
210 text={validMonogram ? monogram : automatic.monogram}
211 color={color}
212 className="size-12"
213 />
214 <div className="flex-1 space-y-2">
215 <label htmlFor="project-monogram" className="text-sm font-medium">
216 <T>Letters</T>
217 </label>
218 <Input
219 id="project-monogram"
220 value={letters}
221 onChange={(event) => setLetters(event.currentTarget.value)}
222 aria-describedby="project-monogram-hint"
223 aria-invalid={!validMonogram}
224 autoComplete="off"
225 />
226 <p id="project-monogram-hint" className="text-xs text-muted-foreground">
227 <T>One or two letters or numbers.</T>
228 </p>
229 </div>
230 </div>
231 ) : (
232 <>
233 <ScrollArea scrollFade className="max-h-64">
234 <div className="grid grid-cols-8 gap-1 p-0.5 sm:grid-cols-10">
235 {PROJECT_EMOJIS.map((option) => (
236 <button
237 key={option.emoji}
238 type="button"
239 aria-label={m(option.label)}
240 aria-pressed={emoji === option.emoji}
241 className={cn(
242 "flex aspect-square items-center justify-center rounded-md border border-transparent text-xl outline-none hover:bg-accent focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring",
243 emoji === option.emoji && "border-border bg-accent",
244 )}
245 onClick={() => setEmoji(option.emoji)}
246 >
247 {option.emoji}
248 </button>
249 ))}
250 </div>
251 </ScrollArea>
252 <div>
253 <div className="mb-2 text-xs font-medium text-muted-foreground">
254 <T>Or paste any emoji</T>
255 </div>
256 <Input
257 value={customEmoji}
258 aria-label={gt("Custom emoji")}
259 placeholder={gt("Paste an emoji")}
260 onChange={(event) => {
261 const value = event.currentTarget.value;
262 setCustomEmoji(value);
263 const nextEmoji = firstEmoji(value);
264 if (nextEmoji) setEmoji(nextEmoji);
265 }}
266 />
267 </div>
268 </>
269 )}
270 </DialogPanel>
271 <DialogFooter>
272 <Button variant="outline" onClick={onClose}>
273 <T>Cancel</T>
274 </Button>
275 <Button onClick={save} disabled={mode === "monogram" && !validMonogram}>
276 <T>Save icon</T>
277 </Button>
278 </DialogFooter>
279 </>
280 );
281 }