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

ComposerPendingApprovalActions.tsx

75 lines2.6 KB

1import {
2 type ApprovalRequestId,
3 type ProviderApprovalDecision,
4 type ProviderApprovalOption,
5} from "@t3tools/contracts";
6import { memo } from "react";
7import { TriangleAlertIcon } from "lucide-react";
8import { Button } from "../ui/button";
9import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
10​
11interface ComposerPendingApprovalActionsProps {
12 requestId: ApprovalRequestId;
13 isResponding: boolean;
14 options?: ReadonlyArray<ProviderApprovalOption> | undefined;
15 onRespondToApproval: (
16 requestId: ApprovalRequestId,
17 decision: ProviderApprovalDecision,
18 ) => Promise<unknown>;
20​
21const APPROVAL_ACTION_CLASS_NAME = "font-normal";
22const DEFAULT_APPROVAL_OPTIONS = [
23 { decision: "cancel", label: "Cancel" },
24 { decision: "decline", label: "Decline" },
25 { decision: "acceptForSession", label: "Always allow this session" },
26 { decision: "accept", label: "Approve" },
27] satisfies ReadonlyArray<ProviderApprovalOption>;
28​
29export const ComposerPendingApprovalActions = memo(function ComposerPendingApprovalActions({
30 requestId,
31 isResponding,
32 options = DEFAULT_APPROVAL_OPTIONS,
33 onRespondToApproval,
34}: ComposerPendingApprovalActionsProps) {
35 return (
36 <>
37 {options.map((option) => {
38 const button = (
39 <Button
40 key={option.decision}
41 size="micro"
42 variant="ghost-muted"
43 className={`${APPROVAL_ACTION_CLASS_NAME}${
44 option.decision === "decline"
45 ? " text-destructive-foreground [:hover,[data-pressed]]:text-destructive-foreground"
46 : option.decision === "accept"
47 ? " text-foreground"
48 : option.warning
49 ? " text-warning"
50 : ""
51 }`}
52 disabled={isResponding}
53 aria-description={option.warning}
54 onClick={() => void onRespondToApproval(requestId, option.decision)}
55 >
56 {option.warning ? <TriangleAlertIcon className="size-3 shrink-0" /> : null}
57 <span className="max-w-40 truncate">{option.label}</span>
58 </Button>
59 );
60 // A provider caution, such as a prompt injection warning on "allow
61 // always", rides along as a tooltip so the row stays one line.
62 return option.warning ? (
63 <Tooltip key={option.decision}>
64 <TooltipTrigger render={button} />
65 <TooltipPopup side="top" className="max-w-72 text-xs leading-snug">
66 {option.warning}
67 </TooltipPopup>
68 </Tooltip>
69 ) : (
70 button
71 );
72 })}
73 </>
74 );
75});