Files
t3code/apps/web/src/components/ProjectScriptsControl.tsx
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

471 lines
17 KiB
TypeScript

import { ThreadDetailsControl } from "./chat/ThreadDetailsControl";
import type {
ProjectScript,
ResolvedKeybindingsConfig,
T3ProjectFileScript,
} from "@t3tools/contracts";
import {
isAtomCommandInterrupted,
squashAtomCommandFailure,
} from "@t3tools/client-runtime/state/runtime";
import { ChevronDownIcon, DownloadIcon, PlusIcon, SettingsIcon, WrenchIcon } from "lucide-react";
import React, { useCallback, useMemo, useState } from "react";
import { commandForProjectScript, primaryProjectScript } from "~/projectScripts";
import { shortcutLabelForCommand } from "~/keybindings";
import {
EMPTY_PROJECT_SCRIPT_INPUT,
editorRequestForScript,
ProjectScriptEditorDialog,
ScriptIcon,
type NewProjectScriptInput,
type ProjectScriptActionResult,
type ProjectScriptEditorRequest,
} from "./projectScriptEditor";
import { Button } from "./ui/button";
import { Group, GroupSeparator } from "./ui/group";
import {
Menu,
MenuGroup,
MenuGroupLabel,
MenuItem,
MenuItemLabel,
MenuPopup,
MenuSeparator,
MenuShortcut,
MenuTrigger,
MenuSub,
MenuSubTrigger,
MenuSubPopup,
} from "./ui/menu";
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
import { cn } from "~/lib/utils";
import {
THREAD_DETAILS_PANEL_CHEVRON_CLASS,
THREAD_DETAILS_PANEL_ICON_CLASS,
THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS,
THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS,
} from "./chat/threadDetailsPanelStyles";
export type { NewProjectScriptInput, ProjectScriptActionResult };
const NO_FILE_SCRIPTS: ReadonlyArray<T3ProjectFileScript> = [];
interface ProjectScriptsControlProps {
displayMode?: "toolbar" | "panel";
presentation?: "toolbar" | "menu";
onRequestMenuClose?: () => void;
scripts: ReadonlyArray<ProjectScript>;
/** Scripts declared in the project's checked-in t3.json, offered for import. */
fileScripts?: ReadonlyArray<T3ProjectFileScript>;
keybindings: ResolvedKeybindingsConfig;
preferredScriptId?: string | null;
onRunScript: (script: ProjectScript) => void;
onAddScript: (input: NewProjectScriptInput) => Promise<ProjectScriptActionResult>;
onUpdateScript: (
scriptId: string,
input: NewProjectScriptInput,
) => Promise<ProjectScriptActionResult>;
onDeleteScript: (scriptId: string) => Promise<ProjectScriptActionResult>;
}
export default function ProjectScriptsControl({
displayMode = "toolbar",
presentation = "toolbar",
onRequestMenuClose,
scripts,
fileScripts = NO_FILE_SCRIPTS,
keybindings,
preferredScriptId = null,
onRunScript,
onAddScript,
onUpdateScript,
onDeleteScript,
}: ProjectScriptsControlProps) {
const isPanel = displayMode === "panel";
const ActionGroup = isPanel ? "div" : Group;
const panelAnchorRef = React.useRef<HTMLDivElement | null>(null);
const [actionsMenuOpen, setActionsMenuOpen] = useState({
presentation,
scripts: false,
imports: false,
});
if (actionsMenuOpen.presentation !== presentation) {
setActionsMenuOpen({ presentation, scripts: false, imports: false });
}
const [editorRequest, setEditorRequest] = useState<ProjectScriptEditorRequest | null>(null);
const primaryScript = useMemo(() => {
if (preferredScriptId) {
const preferred = scripts.find((script) => script.id === preferredScriptId);
if (preferred) return preferred;
}
return primaryProjectScript(scripts);
}, [preferredScriptId, scripts]);
const importableScripts = useMemo(
() =>
fileScripts.filter(
(fileScript) =>
!scripts.some(
(script) =>
script.command === fileScript.command ||
script.name.toLowerCase() === fileScript.name.toLowerCase(),
),
),
[fileScripts, scripts],
);
const openAddDialog = () => {
setEditorRequest({ scriptId: null, initial: EMPTY_PROJECT_SCRIPT_INPUT });
};
const openEditDialog = (script: ProjectScript) => {
onRequestMenuClose?.();
setActionsMenuOpen({ presentation, scripts: false, imports: false });
setEditorRequest(editorRequestForScript(script, keybindings));
};
const submitScript = useCallback(
(scriptId: string | null, input: NewProjectScriptInput) =>
scriptId === null ? onAddScript(input) : onUpdateScript(scriptId, input),
[onAddScript, onUpdateScript],
);
const importFileScript = async (fileScript: T3ProjectFileScript) => {
const payload: NewProjectScriptInput = {
name: fileScript.name,
command: fileScript.command,
icon: fileScript.icon ?? "play",
runOnWorktreeCreate: fileScript.runOnWorktreeCreate ?? false,
waitForSetup: fileScript.runOnWorktreeCreate === true && fileScript.async === false,
keybinding: null,
previewUrl: fileScript.previewUrl ?? null,
autoOpenPreview: fileScript.previewUrl ? (fileScript.autoOpenPreview ?? false) : false,
};
const result = await onAddScript(payload);
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
// Surface the failure through the regular add dialog, prefilled so the
// user can adjust and retry.
const error = squashAtomCommandFailure(result);
setEditorRequest({
scriptId: null,
initial: payload,
error: error instanceof Error ? error.message : "Failed to import action.",
});
}
};
const importMenuItems = importableScripts.length > 0 && (
<>
{primaryScript && <MenuSeparator />}
<MenuGroup>
<MenuGroupLabel>From t3.json</MenuGroupLabel>
{importableScripts.map((fileScript) => (
<MenuItem
density={presentation === "menu" ? "touch" : "default"}
key={`${fileScript.name} ${fileScript.command}`}
onClick={() => void importFileScript(fileScript)}
>
<ScriptIcon icon={fileScript.icon ?? "play"} className="size-4" />
<MenuItemLabel>{fileScript.name}</MenuItemLabel>
<MenuShortcut>
<DownloadIcon className="size-3.5" aria-label="Import" />
</MenuShortcut>
</MenuItem>
))}
</MenuGroup>
</>
);
const scriptItems = (
<>
{scripts.map((script) => {
const shortcutLabel = shortcutLabelForCommand(
keybindings,
commandForProjectScript(script.id),
);
return (
<MenuItem
density={presentation === "menu" ? "touch" : "default"}
key={script.id}
className="group"
onClick={() => onRunScript(script)}
>
<ScriptIcon icon={script.icon} className="size-4" />
<MenuItemLabel>
{script.runOnWorktreeCreate ? `${script.name} (setup)` : script.name}
</MenuItemLabel>
<span className="relative ms-auto flex h-6 min-w-6 items-center justify-end">
{shortcutLabel &&
(presentation === "menu" ? (
<MenuShortcut className="ms-0 mr-7">{shortcutLabel}</MenuShortcut>
) : (
// The shortcut yields its slot to the edit button on hover.
<span className="transition-opacity group-hover:opacity-0 group-focus-visible:opacity-0">
<MenuShortcut className="ms-0">{shortcutLabel}</MenuShortcut>
</span>
))}
<span
className={`absolute right-0 top-1/2 flex -translate-y-1/2 ${presentation === "menu" ? "" : "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"}`}
>
<Button
type="button"
variant="ghost"
size="icon-xs"
className="size-6"
aria-label={`Edit ${script.name}`}
onPointerDown={(event) => {
event.preventDefault();
event.stopPropagation();
}}
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
openEditDialog(script);
}}
>
<SettingsIcon className="size-3.5" />
</Button>
</span>
</span>
</MenuItem>
);
})}
{importMenuItems}
<MenuItem density={presentation === "menu" ? "touch" : "default"} onClick={openAddDialog}>
<PlusIcon className="size-4" />
<MenuItemLabel>{isPanel ? "Add project script" : "Add action"}</MenuItemLabel>
</MenuItem>
</>
);
return (
<>
{presentation === "menu" ? (
<>
{primaryScript && (
<MenuItem
density={presentation === "menu" ? "touch" : "default"}
onClick={() => onRunScript(primaryScript)}
>
<ScriptIcon icon={primaryScript.icon} className="size-4" />
<MenuItemLabel>Run {primaryScript.name}</MenuItemLabel>
<MenuShortcut>
{shortcutLabelForCommand(keybindings, commandForProjectScript(primaryScript.id))}
</MenuShortcut>
</MenuItem>
)}
{primaryScript || importableScripts.length > 0 ? (
<MenuSub
open={actionsMenuOpen.scripts}
onOpenChange={(open) =>
setActionsMenuOpen({ presentation, scripts: open, imports: false })
}
>
<MenuSubTrigger density="touch">
<ScriptIcon icon="play" className="size-4" />
<MenuItemLabel>Project actions</MenuItemLabel>
</MenuSubTrigger>
<MenuSubPopup>{scriptItems}</MenuSubPopup>
</MenuSub>
) : (
<MenuItem
density={presentation === "menu" ? "touch" : "default"}
onClick={openAddDialog}
>
<PlusIcon className="size-4" />
<MenuItemLabel>Add project action…</MenuItemLabel>
</MenuItem>
)}
</>
) : primaryScript ? (
<ActionGroup
role="group"
aria-label="Project scripts"
{...(isPanel
? { className: THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS, ref: panelAnchorRef }
: {})}
>
<Tooltip>
<TooltipTrigger
render={
<ThreadDetailsControl
size="xs"
variant={isPanel ? "ghost" : "outline"}
part="primary"
panel={isPanel}
className={isPanel ? undefined : "w-7 sm:w-6 @3xl/header-actions:w-auto!"}
aria-label={`Run ${primaryScript.name}`}
// The tooltip wrapper replaces data-slot="button", so themed
// toolbar styling needs its own hook.
data-toolbar-control=""
onClick={() => onRunScript(primaryScript)}
/>
}
>
<ScriptIcon
icon={primaryScript.icon}
{...(isPanel ? { className: THREAD_DETAILS_PANEL_ICON_CLASS } : {})}
/>
<span
className={cn(
"sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5",
isPanel && "not-sr-only ml-0 truncate",
)}
>
{primaryScript.name}
</span>
</TooltipTrigger>
<TooltipPopup side="top">Run {primaryScript.name}</TooltipPopup>
</Tooltip>
{isPanel ? (
<span aria-hidden="true" className={THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS} />
) : (
<GroupSeparator className="hidden @3xl/header-actions:block" />
)}
<Menu
open={actionsMenuOpen.scripts}
onOpenChange={(open) =>
setActionsMenuOpen({ presentation, scripts: open, imports: false })
}
>
<MenuTrigger
render={
<ThreadDetailsControl
size={isPanel ? "sm" : "icon-xs"}
variant={isPanel ? "ghost" : "outline"}
part="secondary"
panel={isPanel}
aria-label="Script actions"
/>
}
>
<ChevronDownIcon
className={isPanel ? THREAD_DETAILS_PANEL_CHEVRON_CLASS : "size-4"}
/>
</MenuTrigger>
<MenuPopup
align="end"
{...(isPanel ? { anchor: panelAnchorRef } : {})}
className={isPanel ? "w-(--anchor-width)" : undefined}
>
{scriptItems}
</MenuPopup>
</Menu>
</ActionGroup>
) : importableScripts.length > 0 ? (
isPanel ? (
<div
role="group"
aria-label="Project actions"
className={THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS}
ref={panelAnchorRef}
>
<ThreadDetailsControl
size="sm"
variant="ghost"
part="primary"
aria-label="Project actions"
onClick={() => setActionsMenuOpen({ presentation, scripts: false, imports: true })}
>
<WrenchIcon className={THREAD_DETAILS_PANEL_ICON_CLASS} />
<span className="min-w-0 truncate">Actions</span>
</ThreadDetailsControl>
<span aria-hidden="true" className={THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS} />
<Menu
highlightItemOnHover={false}
open={actionsMenuOpen.imports}
onOpenChange={(open) =>
setActionsMenuOpen({ presentation, scripts: false, imports: open })
}
>
<MenuTrigger
render={
<ThreadDetailsControl
size="sm"
variant="ghost"
part="secondary"
aria-label="Choose project action"
/>
}
>
<ChevronDownIcon className={THREAD_DETAILS_PANEL_CHEVRON_CLASS} />
</MenuTrigger>
<MenuPopup align="end" anchor={panelAnchorRef} className="w-(--anchor-width)">
{importMenuItems}
<MenuItem onClick={openAddDialog}>
<PlusIcon className="size-4" />
Add action
</MenuItem>
</MenuPopup>
</Menu>
</div>
) : (
<Menu
highlightItemOnHover={false}
open={actionsMenuOpen.imports}
onOpenChange={(open) =>
setActionsMenuOpen({ presentation, scripts: false, imports: open })
}
>
<MenuTrigger
render={<Button size="xs" variant="outline" aria-label="Project actions" />}
>
<WrenchIcon className="size-3.5" />
<span className="sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5">
Actions
</span>
<ChevronDownIcon className="size-3.5" />
</MenuTrigger>
<MenuPopup align="end">
{importMenuItems}
<MenuItem onClick={openAddDialog}>
<PlusIcon className="size-4" />
Add action
</MenuItem>
</MenuPopup>
</Menu>
)
) : (
<Tooltip>
<TooltipTrigger
render={
<ThreadDetailsControl
size="xs"
variant={isPanel ? "ghost" : "outline"}
part="row"
panel={isPanel}
className={isPanel ? undefined : "w-7 sm:w-6 @3xl/header-actions:w-auto!"}
aria-label={isPanel ? "Add project script" : "Add action"}
// The tooltip wrapper replaces data-slot="button", so themed
// toolbar styling needs its own hook.
data-toolbar-control=""
onClick={openAddDialog}
/>
}
>
<PlusIcon className="size-3.5" />
<span
className={cn(
"sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5",
isPanel && "not-sr-only ml-0.5",
)}
>
{isPanel ? "Add project script" : "Add action"}
</span>
</TooltipTrigger>
<TooltipPopup side="top">{isPanel ? "Add project script" : "Add action"}</TooltipPopup>
</Tooltip>
)}
<ProjectScriptEditorDialog
request={editorRequest}
scripts={scripts}
onSubmit={submitScript}
onDelete={(scriptId) => void onDeleteScript(scriptId)}
onClose={() => setEditorRequest(null)}
/>
</>
);
}