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

ProjectScriptsControl.tsx

649 lines24.1 KB

1import type {
2 ProjectScript,
3 ProjectScriptIcon,
4 ResolvedKeybindingsConfig,
5 T3ProjectFileScript,
6} from "@t3tools/contracts";
7import {
8 isAtomCommandInterrupted,
9 squashAtomCommandFailure,
10 type AtomCommandResult,
11} from "@t3tools/client-runtime/state/runtime";
12import {
13 BugIcon,
14 ChevronDownIcon,
15 DownloadIcon,
16 FlaskConicalIcon,
17 HammerIcon,
18 ListChecksIcon,
19 PlayIcon,
20 PlusIcon,
21 SettingsIcon,
22 WrenchIcon,
23} from "lucide-react";
24import React, { type FormEvent, type KeyboardEvent, useCallback, useMemo, useState } from "react";
25​
26import {
27 keybindingValueForCommand,
28 decodeProjectScriptKeybindingRule,
29} from "~/lib/projectScriptKeybindings";
30import { keybindingFromKeyboardEvent } from "~/components/settings/KeybindingsSettings.logic";
31import {
32 commandForProjectScript,
33 nextProjectScriptId,
34 primaryProjectScript,
35} from "~/projectScripts";
36import { shortcutLabelForCommand } from "~/keybindings";
37import {
38 AlertDialog,
39 AlertDialogClose,
40 AlertDialogDescription,
41 AlertDialogFooter,
42 AlertDialogHeader,
43 AlertDialogPopup,
44 AlertDialogTitle,
45} from "./ui/alert-dialog";
46import { Button } from "./ui/button";
47import {
48 Dialog,
49 DialogDescription,
50 DialogFooter,
51 DialogHeader,
52 DialogPanel,
53 DialogPopup,
54 DialogTitle,
55} from "./ui/dialog";
56import { Group, GroupSeparator } from "./ui/group";
57import { Input } from "./ui/input";
58import { Label } from "./ui/label";
59import {
60 Menu,
61 MenuGroup,
62 MenuGroupLabel,
63 MenuItem,
64 MenuPopup,
65 MenuSeparator,
66 MenuShortcut,
67 MenuTrigger,
68} from "./ui/menu";
69import { Popover, PopoverPopup, PopoverTrigger } from "./ui/popover";
70import { Switch } from "./ui/switch";
71import { Textarea } from "./ui/textarea";
72import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
73​
74const SCRIPT_ICONS: Array<{ id: ProjectScriptIcon; label: string }> = [
75 { id: "play", label: "Play" },
76 { id: "test", label: "Test" },
77 { id: "lint", label: "Lint" },
78 { id: "configure", label: "Configure" },
79 { id: "build", label: "Build" },
80 { id: "debug", label: "Debug" },
81];
82​
83function ScriptIcon({
84 icon,
85 className = "size-3.5",
86}: {
87 icon: ProjectScriptIcon;
88 className?: string;
89}) {
90 if (icon === "test") return <FlaskConicalIcon className={className} />;
91 if (icon === "lint") return <ListChecksIcon className={className} />;
92 if (icon === "configure") return <WrenchIcon className={className} />;
93 if (icon === "build") return <HammerIcon className={className} />;
94 if (icon === "debug") return <BugIcon className={className} />;
95 return <PlayIcon className={className} />;
97​
98export interface NewProjectScriptInput {
99 name: string;
100 command: string;
101 icon: ProjectScriptIcon;
102 runOnWorktreeCreate: boolean;
103 keybinding: string | null;
104 /** Optional URL to open in the in-app preview when this script runs. */
105 previewUrl: string | null;
106 /** When true, automatically open the preview panel pointed at `previewUrl`. */
107 autoOpenPreview: boolean;
109​
110export type ProjectScriptActionResult = AtomCommandResult<void, unknown>;
111​
112const NO_FILE_SCRIPTS: ReadonlyArray<T3ProjectFileScript> = [];
113​
114interface ProjectScriptsControlProps {
115 scripts: ReadonlyArray<ProjectScript>;
116 /** Scripts declared in the project's checked-in t3.json, offered for import. */
117 fileScripts?: ReadonlyArray<T3ProjectFileScript>;
118 keybindings: ResolvedKeybindingsConfig;
119 preferredScriptId?: string | null;
120 onRunScript: (script: ProjectScript) => void;
121 onAddScript: (input: NewProjectScriptInput) => Promise<ProjectScriptActionResult>;
122 onUpdateScript: (
123 scriptId: string,
124 input: NewProjectScriptInput,
125 ) => Promise<ProjectScriptActionResult>;
126 onDeleteScript: (scriptId: string) => Promise<ProjectScriptActionResult>;
128​
129export default function ProjectScriptsControl({
130 scripts,
131 fileScripts = NO_FILE_SCRIPTS,
132 keybindings,
133 preferredScriptId = null,
134 onRunScript,
135 onAddScript,
136 onUpdateScript,
137 onDeleteScript,
138}: ProjectScriptsControlProps) {
139 const addScriptFormId = React.useId();
140 const [editingScriptId, setEditingScriptId] = useState<string | null>(null);
141 const [actionsMenuOpen, setActionsMenuOpen] = useState({
142 scripts: false,
143 imports: false,
144 });
145 const [dialogOpen, setDialogOpen] = useState(false);
146 const [name, setName] = useState("");
147 const [command, setCommand] = useState("");
148 const [icon, setIcon] = useState<ProjectScriptIcon>("play");
149 const [iconPickerOpen, setIconPickerOpen] = useState(false);
150 const [runOnWorktreeCreate, setRunOnWorktreeCreate] = useState(false);
151 const [keybinding, setKeybinding] = useState("");
152 const [previewUrl, setPreviewUrl] = useState("");
153 const [autoOpenPreview, setAutoOpenPreview] = useState(false);
154 const [validationError, setValidationError] = useState<string | null>(null);
155 const [deleteConfirmOpen, setDeleteConfirmOpen] = useState(false);
156​
157 const primaryScript = useMemo(() => {
158 if (preferredScriptId) {
159 const preferred = scripts.find((script) => script.id === preferredScriptId);
160 if (preferred) return preferred;
161 }
162 return primaryProjectScript(scripts);
163 }, [preferredScriptId, scripts]);
164 const importableScripts = useMemo(
165 () =>
166 fileScripts.filter(
167 (fileScript) =>
168 !scripts.some(
169 (script) =>
170 script.command === fileScript.command ||
171 script.name.toLowerCase() === fileScript.name.toLowerCase(),
172 ),
173 ),
174 [fileScripts, scripts],
175 );
176 const isEditing = editingScriptId !== null;
177 const dropdownItemClassName =
178 "data-highlighted:bg-transparent data-highlighted:text-foreground hover:bg-accent hover:text-accent-foreground focus-visible:bg-accent focus-visible:text-accent-foreground data-highlighted:hover:bg-accent data-highlighted:hover:text-accent-foreground data-highlighted:focus-visible:bg-accent data-highlighted:focus-visible:text-accent-foreground";
179​
180 const captureKeybinding = (event: KeyboardEvent<HTMLInputElement>) => {
181 if (event.key === "Tab") return;
182 event.preventDefault();
183 if (event.key === "Backspace" || event.key === "Delete") {
184 setKeybinding("");
185 return;
186 }
187 const next = keybindingFromKeyboardEvent(event, navigator.platform);
188 if (!next) return;
189 setKeybinding(next);
190 };
191​
192 const submitAddScript = async (event: FormEvent) => {
193 event.preventDefault();
194 const trimmedName = name.trim();
195 const trimmedCommand = command.trim();
196 if (trimmedName.length === 0) {
197 setValidationError("Name is required.");
198 return;
199 }
200 if (trimmedCommand.length === 0) {
201 setValidationError("Command is required.");
202 return;
203 }
204​
205 setValidationError(null);
206 let payload: NewProjectScriptInput;
207 try {
208 const scriptIdForValidation =
209 editingScriptId ??
210 nextProjectScriptId(
211 trimmedName,
212 scripts.map((script) => script.id),
213 );
214 const keybindingRule = decodeProjectScriptKeybindingRule({
215 keybinding,
216 command: commandForProjectScript(scriptIdForValidation),
217 });
218 const trimmedPreviewUrl = previewUrl.trim();
219 payload = {
220 name: trimmedName,
221 command: trimmedCommand,
222 icon,
223 runOnWorktreeCreate,
224 keybinding: keybindingRule?.key ?? null,
225 previewUrl: trimmedPreviewUrl.length > 0 ? trimmedPreviewUrl : null,
226 autoOpenPreview: trimmedPreviewUrl.length > 0 ? autoOpenPreview : false,
227 } satisfies NewProjectScriptInput;
228 } catch (error) {
229 setValidationError(error instanceof Error ? error.message : "Failed to save action.");
230 return;
231 }
232​
233 const result = editingScriptId
234 ? await onUpdateScript(editingScriptId, payload)
235 : await onAddScript(payload);
236 if (result._tag === "Failure") {
237 if (!isAtomCommandInterrupted(result)) {
238 const error = squashAtomCommandFailure(result);
239 setValidationError(error instanceof Error ? error.message : "Failed to save action.");
240 }
241 return;
242 }
243 setDialogOpen(false);
244 setIconPickerOpen(false);
245 };
246​
247 const openAddDialog = () => {
248 setEditingScriptId(null);
249 setName("");
250 setCommand("");
251 setIcon("play");
252 setIconPickerOpen(false);
253 setRunOnWorktreeCreate(false);
254 setKeybinding("");
255 setPreviewUrl("");
256 setAutoOpenPreview(false);
257 setValidationError(null);
258 setDialogOpen(true);
259 };
260​
261 const openEditDialog = (script: ProjectScript) => {
262 setActionsMenuOpen({ scripts: false, imports: false });
263 setEditingScriptId(script.id);
264 setName(script.name);
265 setCommand(script.command);
266 setIcon(script.icon);
267 setIconPickerOpen(false);
268 setRunOnWorktreeCreate(script.runOnWorktreeCreate);
269 setKeybinding(keybindingValueForCommand(keybindings, commandForProjectScript(script.id)) ?? "");
270 setPreviewUrl(script.previewUrl ?? "");
271 setAutoOpenPreview(script.autoOpenPreview ?? false);
272 setValidationError(null);
273 setDialogOpen(true);
274 };
275​
276 const confirmDeleteScript = useCallback(() => {
277 if (!editingScriptId) return;
278 setDeleteConfirmOpen(false);
279 setDialogOpen(false);
280 void onDeleteScript(editingScriptId);
281 }, [editingScriptId, onDeleteScript]);
282​
283 const importFileScript = async (fileScript: T3ProjectFileScript) => {
284 const payload: NewProjectScriptInput = {
285 name: fileScript.name,
286 command: fileScript.command,
287 icon: fileScript.icon ?? "play",
288 runOnWorktreeCreate: fileScript.runOnWorktreeCreate ?? false,
289 keybinding: null,
290 previewUrl: fileScript.previewUrl ?? null,
291 autoOpenPreview: fileScript.previewUrl ? (fileScript.autoOpenPreview ?? false) : false,
292 };
293 const result = await onAddScript(payload);
294 if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
295 // Surface the failure through the regular add dialog, prefilled so the
296 // user can adjust and retry.
297 const error = squashAtomCommandFailure(result);
298 setEditingScriptId(null);
299 setName(payload.name);
300 setCommand(payload.command);
301 setIcon(payload.icon);
302 setIconPickerOpen(false);
303 setRunOnWorktreeCreate(payload.runOnWorktreeCreate);
304 setKeybinding("");
305 setPreviewUrl(payload.previewUrl ?? "");
306 setAutoOpenPreview(payload.autoOpenPreview);
307 setValidationError(error instanceof Error ? error.message : "Failed to import action.");
308 setDialogOpen(true);
309 }
310 };
311​
312 const importMenuItems = importableScripts.length > 0 && (
313 <>
314 {primaryScript && <MenuSeparator />}
315 <MenuGroup>
316 <MenuGroupLabel>From t3.json</MenuGroupLabel>
317 {importableScripts.map((fileScript) => (
318 <MenuItem
319 key={`${fileScript.name} ${fileScript.command}`}
320 className={dropdownItemClassName}
321 onClick={() => void importFileScript(fileScript)}
322 >
323 <ScriptIcon icon={fileScript.icon ?? "play"} className="size-4" />
324 <span className="truncate">{fileScript.name}</span>
325 <MenuShortcut className="ms-auto">
326 <DownloadIcon className="size-3.5" aria-label="Import" />
327 </MenuShortcut>
328 </MenuItem>
329 ))}
330 </MenuGroup>
331 </>
332 );
333​
334 return (
335 <>
336 {primaryScript ? (
337 <Group aria-label="Project scripts">
338 <Tooltip>
339 <TooltipTrigger
340 render={
341 <Button
342 size="xs"
343 variant="outline"
344 className="w-7 px-0 sm:w-6 @3xl/header-actions:w-auto! @3xl/header-actions:px-[calc(--spacing(2)-1px)]"
345 aria-label={`Run ${primaryScript.name}`}
346 // The tooltip wrapper replaces data-slot="button", so themed
347 // toolbar styling needs its own hook.
348 data-toolbar-control=""
349 onClick={() => onRunScript(primaryScript)}
350 />
351 }
352 >
353 <ScriptIcon icon={primaryScript.icon} />
354 <span className="sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5">
355 {primaryScript.name}
356 </span>
357 </TooltipTrigger>
358 <TooltipPopup side="top">Run {primaryScript.name}</TooltipPopup>
359 </Tooltip>
360 <GroupSeparator className="hidden @3xl/header-actions:block" />
361 <Menu
362 highlightItemOnHover={false}
363 open={actionsMenuOpen.scripts}
364 onOpenChange={(open) => setActionsMenuOpen({ scripts: open, imports: false })}
365 >
366 <MenuTrigger
367 render={<Button size="icon-xs" variant="outline" aria-label="Script actions" />}
368 >
369 <ChevronDownIcon className="size-4" />
370 </MenuTrigger>
371 <MenuPopup align="end">
372 {scripts.map((script) => {
373 const shortcutLabel = shortcutLabelForCommand(
374 keybindings,
375 commandForProjectScript(script.id),
376 );
377 return (
378 <MenuItem
379 key={script.id}
380 className={`group ${dropdownItemClassName}`}
381 onClick={() => onRunScript(script)}
382 >
383 <ScriptIcon icon={script.icon} className="size-4" />
384 <span className="truncate">
385 {script.runOnWorktreeCreate ? `${script.name} (setup)` : script.name}
386 </span>
387 <span className="relative ms-auto flex h-6 min-w-6 items-center justify-end">
388 {shortcutLabel && (
389 <MenuShortcut className="ms-0 transition-opacity group-hover:opacity-0 group-focus-visible:opacity-0">
390 {shortcutLabel}
391 </MenuShortcut>
392 )}
393 <Button
394 type="button"
395 variant="ghost"
396 size="icon-xs"
397 className="absolute right-0 top-1/2 size-6 -translate-y-1/2 opacity-0 pointer-events-none transition-opacity group-hover:opacity-100 group-hover:pointer-events-auto group-focus-visible:opacity-100 group-focus-visible:pointer-events-auto"
398 aria-label={`Edit ${script.name}`}
399 onPointerDown={(event) => {
400 event.preventDefault();
401 event.stopPropagation();
402 }}
403 onClick={(event) => {
404 event.preventDefault();
405 event.stopPropagation();
406 openEditDialog(script);
407 }}
408 >
409 <SettingsIcon className="size-3.5" />
410 </Button>
411 </span>
412 </MenuItem>
413 );
414 })}
415 {importMenuItems}
416 <MenuItem className={dropdownItemClassName} onClick={openAddDialog}>
417 <PlusIcon className="size-4" />
418 Add action
419 </MenuItem>
420 </MenuPopup>
421 </Menu>
422 </Group>
423 ) : importableScripts.length > 0 ? (
424 <Menu
425 highlightItemOnHover={false}
426 open={actionsMenuOpen.imports}
427 onOpenChange={(open) => setActionsMenuOpen({ scripts: false, imports: open })}
428 >
429 <MenuTrigger render={<Button size="xs" variant="outline" aria-label="Project actions" />}>
430 <PlusIcon className="size-3.5" />
431 <span className="sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5">
432 Add action
433 </span>
434 <ChevronDownIcon className="size-3.5" />
435 </MenuTrigger>
436 <MenuPopup align="end">
437 {importMenuItems}
438 <MenuItem className={dropdownItemClassName} onClick={openAddDialog}>
439 <PlusIcon className="size-4" />
440 Add action
441 </MenuItem>
442 </MenuPopup>
443 </Menu>
444 ) : (
445 <Tooltip>
446 <TooltipTrigger
447 render={
448 <Button
449 size="xs"
450 variant="outline"
451 className="w-7 px-0 sm:w-6 @3xl/header-actions:w-auto! @3xl/header-actions:px-[calc(--spacing(2)-1px)]"
452 aria-label="Add action"
453 // The tooltip wrapper replaces data-slot="button", so themed
454 // toolbar styling needs its own hook.
455 data-toolbar-control=""
456 onClick={openAddDialog}
457 />
458 }
459 >
460 <PlusIcon className="size-3.5" />
461 <span className="sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5">
462 Add action
463 </span>
464 </TooltipTrigger>
465 <TooltipPopup side="top">Add action</TooltipPopup>
466 </Tooltip>
467 )}
468​
469 <Dialog
470 onOpenChange={(open) => {
471 setDialogOpen(open);
472 if (!open) {
473 setIconPickerOpen(false);
474 }
475 }}
476 onOpenChangeComplete={(open) => {
477 if (open) return;
478 setEditingScriptId(null);
479 setName("");
480 setCommand("");
481 setIcon("play");
482 setRunOnWorktreeCreate(false);
483 setKeybinding("");
484 setPreviewUrl("");
485 setAutoOpenPreview(false);
486 setValidationError(null);
487 }}
488 open={dialogOpen}
489 >
490 <DialogPopup>
491 <DialogHeader>
492 <DialogTitle>{isEditing ? "Edit Action" : "Add Action"}</DialogTitle>
493 <DialogDescription>
494 Actions are project-scoped commands you can run from the top bar or keybindings.
495 </DialogDescription>
496 </DialogHeader>
497 <DialogPanel>
498 <form id={addScriptFormId} className="space-y-4" onSubmit={submitAddScript}>
499 <div className="space-y-1.5">
500 <Label htmlFor="script-name">Name</Label>
501 <div className="flex items-center gap-2">
502 <Popover onOpenChange={setIconPickerOpen} open={iconPickerOpen}>
503 <PopoverTrigger
504 render={
505 <Button
506 type="button"
507 variant="outline"
508 className="size-9 shrink-0 hover:bg-popover active:bg-popover data-pressed:bg-popover data-pressed:shadow-xs/5 data-pressed:before:shadow-[0_1px_--theme(--color-black/4%)] dark:border-transparent dark:bg-white/[0.035] dark:data-pressed:before:shadow-none"
509 aria-label="Choose icon"
510 />
511 }
512 >
513 <ScriptIcon icon={icon} className="size-4.5" />
514 </PopoverTrigger>
515 <PopoverPopup align="start">
516 <div className="grid grid-cols-3 gap-2">
517 {SCRIPT_ICONS.map((entry) => {
518 const isSelected = entry.id === icon;
519 return (
520 <button
521 key={entry.id}
522 type="button"
523 className={`relative flex flex-col items-center gap-2 rounded-md border px-2 py-2 text-xs dark:border-transparent ${
524 isSelected
525 ? "border-primary/70 bg-primary/10 dark:ring-1 dark:ring-primary/30"
526 : "border-border/70 hover:bg-accent/60 dark:bg-white/[0.035]"
527 }`}
528 onClick={() => {
529 setIcon(entry.id);
530 setIconPickerOpen(false);
531 }}
532 >
533 <ScriptIcon icon={entry.id} className="size-4" />
534 <span>{entry.label}</span>
535 </button>
536 );
537 })}
538 </div>
539 </PopoverPopup>
540 </Popover>
541 <Input
542 id="script-name"
543 autoFocus
544 placeholder="Test"
545 value={name}
546 onChange={(event) => setName(event.target.value)}
547 />
548 </div>
549 </div>
550 <div className="space-y-1.5">
551 <Label htmlFor="script-keybinding">Keybinding</Label>
552 <Input
553 id="script-keybinding"
554 placeholder="Press shortcut"
555 value={keybinding}
556 readOnly
557 onKeyDown={captureKeybinding}
558 />
559 <p className="text-xs text-muted-foreground">
560 Press a shortcut. Use <code>Backspace</code> to clear.
561 </p>
562 </div>
563 <div className="space-y-1.5">
564 <Label htmlFor="script-command">Command</Label>
565 <Textarea
566 id="script-command"
567 placeholder="bun test"
568 value={command}
569 onChange={(event) => setCommand(event.target.value)}
570 />
571 </div>
572 <div className="space-y-1.5">
573 <Label htmlFor="script-preview-url">Preview URL (optional)</Label>
574 <Input
575 id="script-preview-url"
576 placeholder="http://localhost:5173"
577 value={previewUrl}
578 onChange={(event) => setPreviewUrl(event.target.value)}
579 />
580 <p className="text-xs text-muted-foreground">
581 Open this URL in the in-app preview when this action runs.
582 </p>
583 </div>
584 <label className="flex items-center justify-between gap-3 rounded-md border border-border/70 px-3 py-2 text-sm dark:border-transparent dark:bg-white/[0.035]">
585 <span>Run automatically on worktree creation</span>
586 <Switch
587 checked={runOnWorktreeCreate}
588 onCheckedChange={(checked) => setRunOnWorktreeCreate(Boolean(checked))}
589 />
590 </label>
591 <label
592 className={`flex items-center justify-between gap-3 rounded-md border border-border/70 px-3 py-2 text-sm dark:border-transparent dark:bg-white/[0.035] ${
593 previewUrl.trim().length === 0 ? "opacity-60" : ""
594 }`}
595 >
596 <span>Open preview automatically when this action runs</span>
597 <Switch
598 checked={autoOpenPreview}
599 disabled={previewUrl.trim().length === 0}
600 onCheckedChange={(checked) => setAutoOpenPreview(Boolean(checked))}
601 />
602 </label>
603 {validationError && <p className="text-sm text-destructive">{validationError}</p>}
604 </form>
605 </DialogPanel>
606 <DialogFooter className="dark:border-transparent dark:bg-transparent">
607 {isEditing && (
608 <Button
609 type="button"
610 variant="destructive-outline"
611 className="mr-auto"
612 onClick={() => setDeleteConfirmOpen(true)}
613 >
614 Delete
615 </Button>
616 )}
617 <Button
618 type="button"
619 variant="outline"
620 onClick={() => {
621 setDialogOpen(false);
622 }}
623 >
624 Cancel
625 </Button>
626 <Button form={addScriptFormId} type="submit">
627 {isEditing ? "Save changes" : "Save action"}
628 </Button>
629 </DialogFooter>
630 </DialogPopup>
631 </Dialog>
632​
633 <AlertDialog open={deleteConfirmOpen} onOpenChange={setDeleteConfirmOpen}>
634 <AlertDialogPopup>
635 <AlertDialogHeader>
636 <AlertDialogTitle>Delete action "{name}"?</AlertDialogTitle>
637 <AlertDialogDescription>This action cannot be undone.</AlertDialogDescription>
638 </AlertDialogHeader>
639 <AlertDialogFooter>
640 <AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
641 <Button variant="destructive" onClick={confirmDeleteScript}>
642 Delete action
643 </Button>
644 </AlertDialogFooter>
645 </AlertDialogPopup>
646 </AlertDialog>
647 </>
648 );