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

ProjectScriptsControl.tsx

755 lines27.9 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 onClick={() => onRunScript(primaryScript)}
373 />
374 }
375 >
376 <ScriptIcon
377 icon={primaryScript.icon}
378 {...(isPanel ? { className: THREAD_DETAILS_PANEL_ICON_CLASS } : {})}
379 />
380 <span
381 className={cn(
382 "sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5",
383 isPanel && "not-sr-only ml-0.5 truncate",
384 )}
385 >
386 {primaryScript.name}
387 </span>
388 </TooltipTrigger>
389 <TooltipPopup side="top">Run {primaryScript.name}</TooltipPopup>
390 </Tooltip>
391 {isPanel ? (
392 <span aria-hidden="true" className={THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS} />
393 ) : (
394 <GroupSeparator className="hidden @3xl/header-actions:block" />
395 )}
396 <Menu
397 highlightItemOnHover={false}
398 open={actionsMenuOpen.scripts}
399 onOpenChange={(open) => setActionsMenuOpen({ scripts: open, imports: false })}
400 >
401 <MenuTrigger
402 render={
403 <Button
404 size={isPanel ? "sm" : "icon-xs"}
405 variant={isPanel ? "ghost" : "outline"}
406 className={isPanel ? THREAD_DETAILS_PANEL_SPLIT_SECONDARY_CLASS : undefined}
407 aria-label="Script actions"
408 />
409 }
410 >
411 <ChevronDownIcon
412 className={isPanel ? THREAD_DETAILS_PANEL_CHEVRON_CLASS : "size-4"}
413 />
414 </MenuTrigger>
415 <MenuPopup
416 align="end"
417 {...(isPanel ? { anchor: panelAnchorRef } : {})}
418 className={isPanel ? THREAD_DETAILS_PANEL_ROW_POPUP_CLASS : undefined}
419 >
420 {scripts.map((script) => {
421 const shortcutLabel = shortcutLabelForCommand(
422 keybindings,
423 commandForProjectScript(script.id),
424 );
425 return (
426 <MenuItem
427 key={script.id}
428 className={`group ${dropdownItemClassName}`}
429 onClick={() => onRunScript(script)}
430 >
431 <ScriptIcon icon={script.icon} className="size-4" />
432 <span className="truncate">
433 {script.runOnWorktreeCreate ? `${script.name} (setup)` : script.name}
434 </span>
435 <span className="relative ms-auto flex h-6 min-w-6 items-center justify-end">
436 {shortcutLabel && (
437 <MenuShortcut className="ms-0 transition-opacity group-hover:opacity-0 group-focus-visible:opacity-0">
438 {shortcutLabel}
439 </MenuShortcut>
440 )}
441 <Button
442 type="button"
443 variant="ghost"
444 size="icon-xs"
445 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"
446 aria-label={`Edit ${script.name}`}
447 onPointerDown={(event) => {
448 event.preventDefault();
449 event.stopPropagation();
450 }}
451 onClick={(event) => {
452 event.preventDefault();
453 event.stopPropagation();
454 openEditDialog(script);
455 }}
456 >
457 <SettingsIcon className="size-3.5" />
458 </Button>
459 </span>
460 </MenuItem>
461 );
462 })}
463 {importMenuItems}
464 <MenuItem className={dropdownItemClassName} onClick={openAddDialog}>
465 <PlusIcon className="size-4" />
466 {isPanel ? "Add project script" : "Add action"}
467 </MenuItem>
468 </MenuPopup>
469 </Menu>
470 </ActionGroup>
471 ) : importableScripts.length > 0 ? (
472 isPanel ? (
473 <div
474 role="group"
475 aria-label="Project actions"
476 className={THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS}
477 ref={panelAnchorRef}
478 >
479 <Button
480 size="sm"
481 variant="ghost"
482 className={THREAD_DETAILS_PANEL_SPLIT_PRIMARY_CLASS}
483 aria-label="Project actions"
484 onClick={() => setActionsMenuOpen({ scripts: false, imports: true })}
485 >
486 <WrenchIcon className={THREAD_DETAILS_PANEL_ICON_CLASS} />
487 <span className="ml-0.5 min-w-0 truncate">Actions</span>
488 </Button>
489 <span aria-hidden="true" className={THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS} />
490 <Menu
491 highlightItemOnHover={false}
492 open={actionsMenuOpen.imports}
493 onOpenChange={(open) => setActionsMenuOpen({ scripts: false, imports: open })}
494 >
495 <MenuTrigger
496 render={
497 <Button
498 size="sm"
499 variant="ghost"
500 className={THREAD_DETAILS_PANEL_SPLIT_SECONDARY_CLASS}
501 aria-label="Choose project action"
502 />
503 }
504 >
505 <ChevronDownIcon className={THREAD_DETAILS_PANEL_CHEVRON_CLASS} />
506 </MenuTrigger>
507 <MenuPopup
508 align="end"
509 anchor={panelAnchorRef}
510 className={THREAD_DETAILS_PANEL_ROW_POPUP_CLASS}
511 >
512 {importMenuItems}
513 <MenuItem className={dropdownItemClassName} onClick={openAddDialog}>
514 <PlusIcon className="size-4" />
515 Add action
516 </MenuItem>
517 </MenuPopup>
518 </Menu>
519 </div>
520 ) : (
521 <Menu
522 highlightItemOnHover={false}
523 open={actionsMenuOpen.imports}
524 onOpenChange={(open) => setActionsMenuOpen({ scripts: false, imports: open })}
525 >
526 <MenuTrigger
527 render={<Button size="xs" variant="outline" aria-label="Project actions" />}
528 >
529 <WrenchIcon className="size-3.5" />
530 <span className="sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5">
531 Actions
532 </span>
533 <ChevronDownIcon className="size-3.5" />
534 </MenuTrigger>
535 <MenuPopup align="end">
536 {importMenuItems}
537 <MenuItem className={dropdownItemClassName} onClick={openAddDialog}>
538 <PlusIcon className="size-4" />
539 Add action
540 </MenuItem>
541 </MenuPopup>
542 </Menu>
543 )
544 ) : (
545 <Tooltip>
546 <TooltipTrigger
547 render={
548 <Button
549 size="xs"
550 variant={isPanel ? "ghost" : "outline"}
551 className={
552 isPanel
553 ? THREAD_DETAILS_PANEL_ROW_CLASS
554 : "w-7 px-0 sm:w-6 @3xl/header-actions:w-auto! @3xl/header-actions:px-[calc(--spacing(2)-1px)]"
555 }
556 aria-label={isPanel ? "Add project script" : "Add action"}
557 onClick={openAddDialog}
558 />
559 }
560 >
561 <PlusIcon className="size-3.5" />
562 <span
563 className={cn(
564 "sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5",
565 isPanel && "not-sr-only ml-0.5",
566 )}
567 >
568 {isPanel ? "Add project script" : "Add action"}
569 </span>
570 </TooltipTrigger>
571 <TooltipPopup side="top">{isPanel ? "Add project script" : "Add action"}</TooltipPopup>
572 </Tooltip>
573 )}
574​
575 <Dialog
576 onOpenChange={(open) => {
577 setDialogOpen(open);
578 if (!open) {
579 setIconPickerOpen(false);
580 }
581 }}
582 onOpenChangeComplete={(open) => {
583 if (open) return;
584 setEditingScriptId(null);
585 setName("");
586 setCommand("");
587 setIcon("play");
588 setRunOnWorktreeCreate(false);
589 setKeybinding("");
590 setPreviewUrl("");
591 setAutoOpenPreview(false);
592 setValidationError(null);
593 }}
594 open={dialogOpen}
595 >
596 <DialogPopup>
597 <DialogHeader>
598 <DialogTitle>{isEditing ? "Edit Action" : "Add Action"}</DialogTitle>
599 <DialogDescription>
600 Actions are project-scoped commands you can run from the top bar or keybindings.
601 </DialogDescription>
602 </DialogHeader>
603 <DialogPanel>
604 <form id={addScriptFormId} className="space-y-4" onSubmit={submitAddScript}>
605 <div className="space-y-1.5">
606 <Label htmlFor="script-name">Name</Label>
607 <div className="flex items-center gap-2">
608 <Popover onOpenChange={setIconPickerOpen} open={iconPickerOpen}>
609 <PopoverTrigger
610 render={
611 <Button
612 type="button"
613 variant="outline"
614 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"
615 aria-label="Choose icon"
616 />
617 }
618 >
619 <ScriptIcon icon={icon} className="size-4.5" />
620 </PopoverTrigger>
621 <PopoverPopup align="start">
622 <div className="grid grid-cols-3 gap-2">
623 {SCRIPT_ICONS.map((entry) => {
624 const isSelected = entry.id === icon;
625 return (
626 <button
627 key={entry.id}
628 type="button"
629 className={`relative flex flex-col items-center gap-2 rounded-md border px-2 py-2 text-xs dark:border-transparent ${
630 isSelected
631 ? "border-primary/70 bg-primary/10 dark:ring-1 dark:ring-primary/30"
632 : "border-border/70 hover:bg-accent/60 dark:bg-white/[0.035]"
633 }`}
634 onClick={() => {
635 setIcon(entry.id);
636 setIconPickerOpen(false);
637 }}
638 >
639 <ScriptIcon icon={entry.id} className="size-4" />
640 <span>{entry.label}</span>
641 </button>
642 );
643 })}
644 </div>
645 </PopoverPopup>
646 </Popover>
647 <Input
648 id="script-name"
649 autoFocus
650 placeholder="Test"
651 value={name}
652 onChange={(event) => setName(event.target.value)}
653 />
654 </div>
655 </div>
656 <div className="space-y-1.5">
657 <Label htmlFor="script-keybinding">Keybinding</Label>
658 <Input
659 id="script-keybinding"
660 placeholder="Press shortcut"
661 value={keybinding}
662 readOnly
663 onKeyDown={captureKeybinding}
664 />
665 <p className="text-xs text-muted-foreground">
666 Press a shortcut. Use <code>Backspace</code> to clear.
667 </p>
668 </div>
669 <div className="space-y-1.5">
670 <Label htmlFor="script-command">Command</Label>
671 <Textarea
672 id="script-command"
673 placeholder="bun test"
674 value={command}
675 onChange={(event) => setCommand(event.target.value)}
676 />
677 </div>
678 <div className="space-y-1.5">
679 <Label htmlFor="script-preview-url">Preview URL (optional)</Label>
680 <Input
681 id="script-preview-url"
682 placeholder="http://localhost:5173"
683 value={previewUrl}
684 onChange={(event) => setPreviewUrl(event.target.value)}
685 />
686 <p className="text-xs text-muted-foreground">
687 Open this URL in the in-app preview when this action runs.
688 </p>
689 </div>
690 <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]">
691 <span>Run automatically on worktree creation</span>
692 <Switch
693 checked={runOnWorktreeCreate}
694 onCheckedChange={(checked) => setRunOnWorktreeCreate(Boolean(checked))}
695 />
696 </label>
697 <label
698 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] ${
699 previewUrl.trim().length === 0 ? "opacity-60" : ""
700 }`}
701 >
702 <span>Open preview automatically when this action runs</span>
703 <Switch
704 checked={autoOpenPreview}
705 disabled={previewUrl.trim().length === 0}
706 onCheckedChange={(checked) => setAutoOpenPreview(Boolean(checked))}
707 />
708 </label>
709 {validationError && <p className="text-sm text-destructive">{validationError}</p>}
710 </form>
711 </DialogPanel>
712 <DialogFooter className="dark:border-transparent dark:bg-transparent">
713 {isEditing && (
714 <Button
715 type="button"
716 variant="destructive-outline"
717 className="mr-auto"
718 onClick={() => setDeleteConfirmOpen(true)}
719 >
720 Delete
721 </Button>
722 )}
723 <Button
724 type="button"
725 variant="outline"
726 onClick={() => {
727 setDialogOpen(false);
728 }}
729 >
730 Cancel
731 </Button>
732 <Button form={addScriptFormId} type="submit">
733 {isEditing ? "Save changes" : "Save action"}
734 </Button>
735 </DialogFooter>
736 </DialogPopup>
737 </Dialog>
738​
739 <AlertDialog open={deleteConfirmOpen} onOpenChange={setDeleteConfirmOpen}>
740 <AlertDialogPopup>
741 <AlertDialogHeader>
742 <AlertDialogTitle>Delete action "{name}"?</AlertDialogTitle>
743 <AlertDialogDescription>This action cannot be undone.</AlertDialogDescription>
744 </AlertDialogHeader>
745 <AlertDialogFooter>
746 <AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
747 <Button variant="destructive" onClick={confirmDeleteScript}>
748 Delete action
749 </Button>
750 </AlertDialogFooter>
751 </AlertDialogPopup>
752 </AlertDialog>
753 </>
754 );