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

ComposerPendingApprovalActions.tsx

141 lines4.8 KB

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, InlineButton } 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 presentation?: "composer" | "inline";
15 requestId: RuntimeRequestId;
16 isResponding: boolean;
17 canRespond: boolean;
18 disabled?: boolean;
19 options?: ReadonlyArray<ProviderApprovalOption> | undefined;
20 onRespondToApproval: (
21 requestId: RuntimeRequestId,
22 decision: ProviderApprovalDecision,
23 ) => Promise<unknown>;
25​
26const DEFAULT_APPROVAL_OPTIONS = [
27 { decision: "cancel", label: "Cancel" },
28 { decision: "decline", label: "Decline" },
29 { decision: "acceptForSession", label: "Always allow this session" },
30 { decision: "accept", label: "Approve" },
31] satisfies ReadonlyArray<ProviderApprovalOption>;
32​
33export const ComposerPendingApprovalActions = memo(function ComposerPendingApprovalActions({
34 presentation = "composer",
35 requestId,
36 isResponding,
37 canRespond,
38 disabled = false,
39 options = DEFAULT_APPROVAL_OPTIONS,
40 onRespondToApproval,
41}: ComposerPendingApprovalActionsProps) {
42 const primaryOptions = options.filter(
43 (option) => option.decision === "decline" || option.decision === "accept",
44 );
45 const moreOptions = options.filter(
46 (option) => option.decision !== "decline" && option.decision !== "accept",
47 );
48​
49 return (
50 <>
51 {primaryOptions.map((option) => {
52 const buttonProps = {
53 disabled: disabled || isResponding || !canRespond,
54 "aria-description": option.warning,
55 onClick: () => {
56 if (!disabled && !isResponding && canRespond) {
57 void onRespondToApproval(requestId, option.decision);
58 }
59 },
60 };
61 const content = (
62 <>
63 {option.warning ? <TriangleAlertIcon className="size-3 shrink-0" /> : null}
64 <span className={presentation === "inline" ? "whitespace-normal" : "max-w-40 truncate"}>
65 {option.label}
66 </span>
67 </>
68 );
69 const button =
70 presentation === "inline" ? (
71 <InlineButton
72 key={option.decision}
73 {...buttonProps}
74 tone={option.decision === "accept" ? "default" : "muted"}
75 >
76 {content}
77 </InlineButton>
78 ) : (
79 <Button
80 key={option.decision}
81 {...buttonProps}
82 size="xs"
83 variant={option.decision === "accept" ? "default" : "outline"}
84 >
85 {content}
86 </Button>
87 );
88 return option.warning ? (
89 <Tooltip key={option.decision}>
90 <TooltipTrigger render={button} />
91 <TooltipPopup side="top">{option.warning}</TooltipPopup>
92 </Tooltip>
93 ) : (
94 button
95 );
96 })}
97 {moreOptions.length > 0 ? (
98 <Menu>
99 <MenuTrigger
100 disabled={disabled || isResponding || !canRespond}
101 render={
102 presentation === "inline" ? (
103 <InlineButton tone="muted" aria-label="More approval options" />
104 ) : (
105 <Button size="icon-xs" variant="outline" aria-label="More approval options" />
106 )
107 }
108 >
109 {presentation === "inline" ? "More" : <EllipsisIcon />}
110 </MenuTrigger>
111 <MenuPopup {...composerFloatingLayerProps} side="top" align="end" width="content">
112 {moreOptions.map((option) => {
113 const item = (
114 <MenuItem
115 key={option.decision}
116 disabled={disabled || isResponding || !canRespond}
117 aria-description={option.warning}
118 onClick={() => {
119 if (!disabled && !isResponding && canRespond)
120 void onRespondToApproval(requestId, option.decision);
121 }}
122 >
123 {option.warning ? <TriangleAlertIcon className="size-3 text-warning" /> : null}
124 <span className="min-w-0 whitespace-normal wrap-break-word">{option.label}</span>
125 </MenuItem>
126 );
127 return option.warning ? (
128 <Tooltip key={option.decision}>
129 <TooltipTrigger render={item} />
130 <TooltipPopup side="top">{option.warning}</TooltipPopup>
131 </Tooltip>
132 ) : (
133 item
134 );
135 })}
136 </MenuPopup>
137 </Menu>
138 ) : null}
139 </>
140 );
141});