Open History to see the last commit
1import {
2 type ProviderApprovalDecision,
3 type ProviderApprovalOption,
4 type RuntimeRequestId,
5} from "@t3tools/contracts";
6import { memo } from "react";
7import { EllipsisIcon, TriangleAlertIcon } from "lucide-react";
8import { Button } from "../ui/button";
9import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu";
10import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
11import { composerFloatingLayerProps } from "./composerEventScope";
12
13interface ComposerPendingApprovalActionsProps {
14 requestId: RuntimeRequestId;
15 isResponding: boolean;
16 canRespond: boolean;
17 options?: ReadonlyArray<ProviderApprovalOption> | undefined;
18 onRespondToApproval: (
19 requestId: RuntimeRequestId,
20 decision: ProviderApprovalDecision,
21 ) => Promise<unknown>;
22}
23
24const DEFAULT_APPROVAL_OPTIONS = [
25 { decision: "cancel", label: "Cancel" },
26 { decision: "decline", label: "Decline" },
27 { decision: "acceptForSession", label: "Always allow this session" },
28 { decision: "accept", label: "Approve" },
29] satisfies ReadonlyArray<ProviderApprovalOption>;
30
31export const ComposerPendingApprovalActions = memo(function ComposerPendingApprovalActions({
32 requestId,
33 isResponding,
34 canRespond,
35 options = DEFAULT_APPROVAL_OPTIONS,
36 onRespondToApproval,
37}: ComposerPendingApprovalActionsProps) {
38 const primaryOptions = options.filter(
39 (option) => option.decision === "decline" || option.decision === "accept",
40 );
41 const moreOptions = options.filter(
42 (option) => option.decision !== "decline" && option.decision !== "accept",
43 );
44
45 return (
46 <>
47 {primaryOptions.map((option) => {
48 const button = (
49 <Button
50 key={option.decision}
51 size="xs"
52 variant={option.decision === "accept" ? "default" : "outline"}
53 disabled={isResponding || !canRespond}
54 aria-description={option.warning}
55 onClick={() => void onRespondToApproval(requestId, option.decision)}
56 >
57 {option.warning ? <TriangleAlertIcon className="size-3 shrink-0" /> : null}
58 <span className="max-w-40 truncate">{option.label}</span>
59 </Button>
60 );
61 return option.warning ? (
62 <Tooltip key={option.decision}>
63 <TooltipTrigger render={button} />
64 <TooltipPopup side="top">{option.warning}</TooltipPopup>
65 </Tooltip>
66 ) : (
67 button
68 );
69 })}
70 {moreOptions.length > 0 ? (
71 <Menu>
72 <MenuTrigger
73 disabled={isResponding}
74 render={<Button size="icon-xs" variant="outline" aria-label="More approval options" />}
75 >
76 <EllipsisIcon />
77 </MenuTrigger>
78 <MenuPopup {...composerFloatingLayerProps} side="top" align="end">
79 {moreOptions.map((option) => {
80 const item = (
81 <MenuItem
82 key={option.decision}
83 disabled={isResponding}
84 aria-description={option.warning}
85 onClick={() => void onRespondToApproval(requestId, option.decision)}
86 variant="ghost"
87 className="mb-1 last:mb-0"
88 >
89 {option.warning ? <TriangleAlertIcon className="size-3 text-warning" /> : null}
90 <span className="min-w-0 whitespace-normal wrap-break-word">{option.label}</span>
91 </MenuItem>
92 );
93 return option.warning ? (
94 <Tooltip key={option.decision}>
95 <TooltipTrigger render={item} />
96 <TooltipPopup side="top">{option.warning}</TooltipPopup>
97 </Tooltip>
98 ) : (
99 item
100 );
101 })}
102 </MenuPopup>
103 </Menu>
104 ) : null}
105 </>
106 );
107});