The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | type ProviderApprovalDecision, |
| 3 | type ProviderApprovalOption, |
| 4 | type RuntimeRequestId, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { msg, useGT, useMessages } from "gt-react"; |
| 7 | import { memo } from "react"; |
| 8 | import { EllipsisIcon, TriangleAlertIcon } from "lucide-react"; |
| 9 | import { Button, InlineButton } from "../ui/button"; |
| 10 | import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; |
| 11 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 12 | import { composerFloatingLayerProps } from "./composerEventScope"; |
| 13 | |
| 14 | interface ComposerPendingApprovalActionsProps { |
| 15 | presentation?: "composer" | "inline"; |
| 16 | requestId: RuntimeRequestId; |
| 17 | isResponding: boolean; |
| 18 | canRespond: boolean; |
| 19 | disabled?: boolean; |
| 20 | options?: ReadonlyArray<ProviderApprovalOption> | undefined; |
| 21 | onRespondToApproval: ( |
| 22 | requestId: RuntimeRequestId, |
| 23 | decision: ProviderApprovalDecision, |
| 24 | ) => Promise<unknown>; |
| 25 | } |
| 26 | |
| 27 | const DEFAULT_APPROVAL_OPTIONS = [ |
| 28 | { |
| 29 | decision: "cancel", |
| 30 | label: msg("Cancel", { $context: "verb, response to an approval request" }), |
| 31 | }, |
| 32 | { |
| 33 | decision: "decline", |
| 34 | label: msg("Decline", { $context: "verb, response to an approval request" }), |
| 35 | }, |
| 36 | { decision: "acceptForSession", label: msg("Always allow this session") }, |
| 37 | { |
| 38 | decision: "accept", |
| 39 | label: msg("Approve", { $context: "verb, response to an approval request" }), |
| 40 | }, |
| 41 | ] satisfies ReadonlyArray<ProviderApprovalOption>; |
| 42 | |
| 43 | export const ComposerPendingApprovalActions = memo(function ComposerPendingApprovalActions({ |
| 44 | presentation = "composer", |
| 45 | requestId, |
| 46 | isResponding, |
| 47 | canRespond, |
| 48 | disabled = false, |
| 49 | options = DEFAULT_APPROVAL_OPTIONS, |
| 50 | onRespondToApproval, |
| 51 | }: ComposerPendingApprovalActionsProps) { |
| 52 | const gt = useGT(); |
| 53 | const m = useMessages(); |
| 54 | // Provider options carry server-authored labels; only the built-in defaults are translated. |
| 55 | const optionLabel = (label: string) => (options === DEFAULT_APPROVAL_OPTIONS ? m(label) : label); |
| 56 | const primaryOptions = options.filter( |
| 57 | (option) => option.decision === "decline" || option.decision === "accept", |
| 58 | ); |
| 59 | const moreOptions = options.filter( |
| 60 | (option) => option.decision !== "decline" && option.decision !== "accept", |
| 61 | ); |
| 62 | |
| 63 | return ( |
| 64 | <> |
| 65 | {primaryOptions.map((option) => { |
| 66 | const buttonProps = { |
| 67 | disabled: disabled || isResponding || !canRespond, |
| 68 | "aria-description": option.warning, |
| 69 | onClick: () => { |
| 70 | if (!disabled && !isResponding && canRespond) { |
| 71 | void onRespondToApproval(requestId, option.decision); |
| 72 | } |
| 73 | }, |
| 74 | }; |
| 75 | const content = ( |
| 76 | <> |
| 77 | {option.warning ? <TriangleAlertIcon className="size-3 shrink-0" /> : null} |
| 78 | <span className={presentation === "inline" ? "whitespace-normal" : "max-w-40 truncate"}> |
| 79 | {optionLabel(option.label)} |
| 80 | </span> |
| 81 | </> |
| 82 | ); |
| 83 | const button = |
| 84 | presentation === "inline" ? ( |
| 85 | <InlineButton |
| 86 | key={option.decision} |
| 87 | {...buttonProps} |
| 88 | tone={option.decision === "accept" ? "default" : "muted"} |
| 89 | > |
| 90 | {content} |
| 91 | </InlineButton> |
| 92 | ) : ( |
| 93 | <Button |
| 94 | key={option.decision} |
| 95 | {...buttonProps} |
| 96 | size="xs" |
| 97 | variant={option.decision === "accept" ? "default" : "outline"} |
| 98 | > |
| 99 | {content} |
| 100 | </Button> |
| 101 | ); |
| 102 | return option.warning ? ( |
| 103 | <Tooltip key={option.decision}> |
| 104 | <TooltipTrigger render={button} /> |
| 105 | <TooltipPopup side="top">{option.warning}</TooltipPopup> |
| 106 | </Tooltip> |
| 107 | ) : ( |
| 108 | button |
| 109 | ); |
| 110 | })} |
| 111 | {moreOptions.length > 0 ? ( |
| 112 | <Menu> |
| 113 | <MenuTrigger |
| 114 | disabled={disabled || isResponding || !canRespond} |
| 115 | render={ |
| 116 | presentation === "inline" ? ( |
| 117 | <InlineButton tone="muted" aria-label={gt("More approval options")} /> |
| 118 | ) : ( |
| 119 | <Button size="icon-xs" variant="outline" aria-label={gt("More approval options")} /> |
| 120 | ) |
| 121 | } |
| 122 | > |
| 123 | {presentation === "inline" ? ( |
| 124 | gt("More", { $context: "button that opens more approval options" }) |
| 125 | ) : ( |
| 126 | <EllipsisIcon /> |
| 127 | )} |
| 128 | </MenuTrigger> |
| 129 | <MenuPopup {...composerFloatingLayerProps} side="top" align="end" width="content"> |
| 130 | {moreOptions.map((option) => { |
| 131 | const item = ( |
| 132 | <MenuItem |
| 133 | key={option.decision} |
| 134 | disabled={disabled || isResponding || !canRespond} |
| 135 | aria-description={option.warning} |
| 136 | onClick={() => { |
| 137 | if (!disabled && !isResponding && canRespond) |
| 138 | void onRespondToApproval(requestId, option.decision); |
| 139 | }} |
| 140 | > |
| 141 | {option.warning ? <TriangleAlertIcon className="size-3 text-warning" /> : null} |
| 142 | <span className="min-w-0 whitespace-normal wrap-break-word"> |
| 143 | {optionLabel(option.label)} |
| 144 | </span> |
| 145 | </MenuItem> |
| 146 | ); |
| 147 | return option.warning ? ( |
| 148 | <Tooltip key={option.decision}> |
| 149 | <TooltipTrigger render={item} /> |
| 150 | <TooltipPopup side="top">{option.warning}</TooltipPopup> |
| 151 | </Tooltip> |
| 152 | ) : ( |
| 153 | item |
| 154 | ); |
| 155 | })} |
| 156 | </MenuPopup> |
| 157 | </Menu> |
| 158 | ) : null} |
| 159 | </> |
| 160 | ); |
| 161 | }); |