import { managedCapabilityDenial } from "@t3tools/client-runtime/contract-labels"; import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; import { AuthTerminalOperateScope, type WorktreeLayoutCommand, type WorktreeLayoutCommandKind, } from "@t3tools/contracts"; import { msg, T, useGT, useMessages } from "gt-react"; import { useState } from "react"; import type { SidebarProjectGroupMember } from "../../sidebarProjectGrouping"; import { useEnvironments } from "../../state/environments"; import { filesystemEnvironment, useFilesystemReadAccess } from "../../state/filesystem"; import { useManagedPolicy } from "../../state/managedPolicy"; import { useEnvironmentQuery } from "../../state/query"; import { useEnvironmentScope } from "../../state/session"; import { useAtomCommand } from "../../state/use-atom-command"; import { Badge } from "../ui/badge"; import { Button } from "../ui/button"; import { toastManager } from "../ui/toast"; import { WORKTREE_LAYOUT_COMMAND_LABELS } from "../WorktreeLayoutApprovalDialogHost"; import { SettingsRow, SettingsSection } from "./settingsLayout"; const COMMAND_TITLES: Record = { create: msg("Create command"), remove: msg("Remove command"), }; /** The `worktreeLayout` commands each checkout's `t3.json` declares, and whether they may run. */ export function ProjectWorktreeLayoutSettings({ members, }: { members: ReadonlyArray; }) { const checkouts = members.filter((member) => !member.isScratch); return checkouts.map((member) => ( 1} /> )); } function CheckoutWorktreeLayout({ member, showCheckout, }: { member: SidebarProjectGroupMember; showCheckout: boolean; }) { const gt = useGT(); const m = useMessages(); const environmentId = member.environmentId; const { environments } = useEnvironments(); const isConnected = environments.find((entry) => entry.environmentId === environmentId)?.connection.phase === "connected"; const access = useFilesystemReadAccess(environmentId); const canApprove = useEnvironmentScope(environmentId, AuthTerminalOperateScope); const policyDenial = managedCapabilityDenial( useManagedPolicy(environmentId), "worktreeLayoutCommands", m, ); const query = useEnvironmentQuery( access.canReadFiles ? filesystemEnvironment.layoutCommands({ environmentId, input: { projectId: member.id } }) : null, ); const approve = useAtomCommand(filesystemEnvironment.approveLayoutCommands, { reportFailure: false, }); const [savingKind, setSavingKind] = useState(null); const commands = query.data?.commands ?? []; // A failed first read is not a project without commands. if (query.data === null && query.error !== null) { return ( Retry } /> ); } // Most projects declare none, and then there is nothing to show. if (commands.length === 0) return null; // Approving sends the text on screen, so a file that changed since is not approved unseen. const save = async (command: WorktreeLayoutCommand, approved: boolean) => { setSavingKind(command.kind); const result = await approve({ environmentId, input: { projectId: member.id, approvals: [{ kind: command.kind, command: approved ? command.command : null }], }, }); setSavingKind(null); if (result._tag === "Failure") { const error = squashAtomCommandFailure(result); toastManager.add({ type: "error", title: approved ? gt("Could not approve the command") : gt("Could not revoke the approval"), ...(error instanceof Error ? { description: error.message } : {}), }); } query.refresh(); }; return (

{policyDenial ?? ( This project's t3.json sets up worktree threads with its own commands. They run on this environment's machine with your permissions, and only as approved here: a command whose text changes needs approval again. )} {showCheckout ? ( {member.environmentLabel ?? gt("Environment")} ยท {member.workspaceRoot} ) : null}

{commands.map((command) => ( {m(COMMAND_TITLES[command.kind])} {policyDenial !== undefined ? ( Off ) : command.approved ? ( Approved ) : ( Needs approval )} } description={m(WORKTREE_LAYOUT_COMMAND_LABELS[command.kind])} control={ } >
            {command.command}
          
))} {!canApprove ? (

This connection cannot approve commands. Use a client that can use the terminal.

) : null} {query.error ? (

{query.error}

) : null}
); }