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

ProjectScriptsControl.tsx

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