import { ThreadDetailsControl } from "./chat/ThreadDetailsControl"; import { AuthSettingsWriteScope, type EnvironmentId, type ProjectScript, type T3ProjectFileScript, type ResolvedKeybindingsConfig, } from "@t3tools/contracts"; import { useAtomValue } from "@effect/atom-react"; import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings"; import { isAtomCommandInterrupted, squashAtomCommandFailure, } from "@t3tools/client-runtime/state/runtime"; import { projectScriptMenuLabel } from "@t3tools/shared/projectScripts"; 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 { serverEnvironment } from "~/state/server"; import { readEnvironmentScope } from "~/state/session"; 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_LABEL_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 = []; interface ProjectScriptsControlProps { displayMode?: "toolbar" | "panel"; presentation?: "toolbar" | "menu"; onRequestMenuClose?: () => void; environmentId: EnvironmentId; scripts: ReadonlyArray; /** Scripts declared in the project's checked-in t3.json, offered for import. */ fileScripts?: ReadonlyArray; preferredScriptId?: string | null; onRunScript?: ((script: ProjectScript) => void) | undefined; onAddScript: (input: NewProjectScriptInput) => Promise; onUpdateScript: ( scriptId: string, input: NewProjectScriptInput, ) => Promise; onDeleteScript: (scriptId: string) => Promise; } export default function ProjectScriptsControl({ displayMode = "toolbar", presentation = "toolbar", onRequestMenuClose, environmentId, scripts, fileScripts = NO_FILE_SCRIPTS, preferredScriptId = null, onRunScript, onAddScript, onUpdateScript, onDeleteScript, }: ProjectScriptsControlProps) { const isPanel = displayMode === "panel"; const ActionGroup = isPanel ? "div" : Group; const panelAnchorRef = React.useRef(null); const keybindings = useAtomValue(serverEnvironment.configValueAtom(environmentId))?.keybindings ?? DEFAULT_RESOLVED_KEYBINDINGS; const [actionsMenuOpen, setActionsMenuOpen] = useState({ presentation, scripts: false, imports: false, }); if (actionsMenuOpen.presentation !== presentation) { setActionsMenuOpen({ presentation, scripts: false, imports: false }); } const [editorRequest, setEditorRequest] = useState(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, runOnSettle: fileScript.runOnSettle ?? false, ...(readEnvironmentScope(environmentId, AuthSettingsWriteScope) ? { 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 && ( <> {scripts.length > 0 && } From t3.json {importableScripts.map((fileScript) => ( void importFileScript(fileScript)} > {fileScript.name} ))} ); const scriptItems = ( <> {scripts.map((script) => { const shortcutLabel = shortcutLabelForCommand( keybindings, commandForProjectScript(script.id), ); return ( onRunScript?.(script)} > {projectScriptMenuLabel(script)} {shortcutLabel && (presentation === "menu" ? ( {shortcutLabel} ) : ( // The shortcut yields its slot to the edit button on hover. {shortcutLabel} ))} ); })} {importMenuItems} {isPanel ? "Add project script" : "Add action"} ); return ( <> {presentation === "menu" ? ( <> {primaryScript && ( onRunScript?.(primaryScript)} > Run {primaryScript.name} {shortcutLabelForCommand(keybindings, commandForProjectScript(primaryScript.id))} )} {scripts.length > 0 || importableScripts.length > 0 ? ( setActionsMenuOpen({ presentation, scripts: open, imports: false }) } > Project actions {scriptItems} ) : ( Add project action… )} ) : primaryScript ? ( onRunScript?.(primaryScript)} /> } > {primaryScript.name} {onRunScript ? `Run ${primaryScript.name}` : "Pair this client again with permission to run terminal commands."} {isPanel ? ( ) : scripts.length > 0 || importableScripts.length > 0 ? ( // No one-click action (only a settle action, or only t3.json imports), // so the saved actions stay reachable through this menu. isPanel ? (
setActionsMenuOpen({ presentation, scripts: false, imports: true })} > Actions
) : ( setActionsMenuOpen({ presentation, scripts: false, imports: open }) } > } > Actions {scriptItems} ) ) : ( } > {isPanel ? "Add project script" : "Add action"} {isPanel ? "Add project script" : "Add action"} )} void onDeleteScript(scriptId)} onClose={() => setEditorRequest(null)} /> ); }