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

ComposerPrimaryActions.tsx

280 lines9.7 KB

1import { memo, type PointerEventHandler } from "react";
2import { ChevronDownIcon, ChevronLeftIcon } from "lucide-react";
3import { useEnvironmentIdentificationMode } from "~/hooks/useSettings";
4import { cn } from "~/lib/utils";
5import { StageBackdropButtonArt, useSidebarStageBackdropVariant } from "../SidebarStageBackdrop";
6import { Button } from "../ui/button";
7import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu";
8import { Spinner } from "../ui/spinner";
9import { composerFloatingLayerProps } from "./composerEventScope";
10​
11interface PendingActionState {
12 questionIndex: number;
13 isLastQuestion: boolean;
14 canAdvance: boolean;
15 isResponding: boolean;
16 isComplete: boolean;
18​
19interface ComposerPrimaryActionsProps {
20 compact: boolean;
21 pendingAction: PendingActionState | null;
22 isRunning: boolean;
23 showPlanFollowUpPrompt: boolean;
24 promptHasText: boolean;
25 isSendBusy: boolean;
26 sendDisabledReason: string | null;
27 isConnecting: boolean;
28 isEnvironmentUnavailable: boolean;
29 isPreparingWorktree: boolean;
30 hasSendableContent: boolean;
31 preserveComposerFocusOnPointerDown?: boolean;
32 onPreviousPendingQuestion: () => void;
33 onInterrupt: () => void;
34 onImplementPlanInNewThread: () => void;
36​
37const formatPendingPrimaryActionLabel = (input: {
38 compact: boolean;
39 isLastQuestion: boolean;
40 isResponding: boolean;
41 questionIndex: number;
42}) => {
43 if (input.isResponding) {
44 return "Submitting...";
45 }
46 if (input.compact) {
47 return input.isLastQuestion ? "Submit" : "Next";
48 }
49 if (!input.isLastQuestion) {
50 return "Next question";
51 }
52 return input.questionIndex > 0 ? "Submit answers" : "Submit answer";
53};
54​
55// The composer's labeled primary actions (Submit, Refine, Implement) share the send button's
56// message-action pill, so they are composer-owned buttons rather than restyled Buttons.
57const messageActionPillClassName =
58 "inline-flex shrink-0 cursor-pointer items-center justify-center gap-1.5 whitespace-nowrap rounded-full bg-message-action font-medium text-base text-message-action-foreground shadow-xs shadow-message-action/24 outline-none hover:bg-message-action-hover focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-64 disabled:shadow-none sm:text-sm";
59​
60const preventPointerFocus: PointerEventHandler<HTMLElement> = (event) => {
61 event.preventDefault();
62};
63​
64export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
65 compact,
66 pendingAction,
67 isRunning,
68 showPlanFollowUpPrompt,
69 promptHasText,
70 isSendBusy,
71 sendDisabledReason,
72 isConnecting,
73 isEnvironmentUnavailable,
74 isPreparingWorktree,
75 hasSendableContent,
76 preserveComposerFocusOnPointerDown = false,
77 onPreviousPendingQuestion,
78 onInterrupt,
79 onImplementPlanInNewThread,
80}: ComposerPrimaryActionsProps) {
81 const pointerFocusProps = preserveComposerFocusOnPointerDown
82 ? { onPointerDown: preventPointerFocus }
83 : undefined;
84 const environmentIdentificationMode = useEnvironmentIdentificationMode();
85 const isSendDisabled = sendDisabledReason !== null;
86 const stageBackdropVariant = useSidebarStageBackdropVariant(
87 environmentIdentificationMode === "artwork",
88 );
89​
90 const renderStopGenerationButton = (insidePendingAction: boolean) => (
91 <button
92 type="button"
93 className={cn(
94 "flex cursor-pointer items-center justify-center rounded-full bg-destructive/90 text-white shadow-xs shadow-destructive/24 inset-shadow-2xs inset-shadow-white/16 transition-all duration-150 hover:bg-destructive hover:scale-105 active:inset-shadow-black/8 active:shadow-none",
95 insidePendingAction
96 ? "size-8 sm:size-7"
97 : hasSendableContent
98 ? "size-9 sm:size-8"
99 : "size-8 sm:h-8 sm:w-8",
100 )}
101 {...pointerFocusProps}
102 onClick={onInterrupt}
103 aria-label="Stop generation"
104 >
105 <svg width="12" height="12" viewBox="0 0 12 12" fill="currentColor" aria-hidden="true">
106 <rect x="2" y="2" width="8" height="8" rx="1.5" />
107 </svg>
108 </button>
109 );
110​
111 if (pendingAction) {
112 return (
113 <div className={cn("flex items-center justify-end", compact ? "gap-1.5" : "gap-2")}>
114 {isRunning ? renderStopGenerationButton(true) : null}
115 {pendingAction.questionIndex > 0 ? (
116 compact ? (
117 <Button
118 size="icon-sm"
119 variant="outline"
120 {...pointerFocusProps}
121 onClick={onPreviousPendingQuestion}
122 disabled={pendingAction.isResponding}
123 aria-label="Previous question"
124 >
125 <ChevronLeftIcon className="size-3.5" />
126 </Button>
127 ) : (
128 <Button
129 size="sm"
130 variant="outline"
131 {...pointerFocusProps}
132 onClick={onPreviousPendingQuestion}
133 disabled={pendingAction.isResponding}
134 >
135 Previous
136 </Button>
137 )
138 ) : null}
139 <button
140 type="submit"
141 className={cn(messageActionPillClassName, "h-8 sm:h-7", compact ? "px-3" : "px-4")}
142 {...pointerFocusProps}
143 disabled={
144 isEnvironmentUnavailable ||
145 pendingAction.isResponding ||
146 (pendingAction.isLastQuestion ? !pendingAction.isComplete : !pendingAction.canAdvance)
147 }
148 >
149 {formatPendingPrimaryActionLabel({
150 compact,
151 isLastQuestion: pendingAction.isLastQuestion,
152 isResponding: pendingAction.isResponding,
153 questionIndex: pendingAction.questionIndex,
154 })}
155 </button>
156 </div>
157 );
158 }
159​
160 if (showPlanFollowUpPrompt) {
161 if (promptHasText) {
162 return (
163 <button
164 type="submit"
165 className={cn(messageActionPillClassName, "h-9 sm:h-8", compact ? "px-3" : "px-4")}
166 {...pointerFocusProps}
167 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
168 >
169 {isConnecting || isSendBusy ? "Sending..." : "Refine"}
170 </button>
171 );
172 }
173​
174 return (
175 <div data-chat-composer-implement-actions="true" className="flex items-center justify-end">
176 <button
177 type="submit"
178 className={cn(messageActionPillClassName, "h-9 rounded-r-none px-4 sm:h-8")}
179 {...pointerFocusProps}
180 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
181 >
182 {isConnecting || isSendBusy ? "Sending..." : "Implement"}
183 </button>
184 <Menu>
185 <MenuTrigger
186 render={
187 <button
188 type="button"
189 className={cn(
190 messageActionPillClassName,
191 "h-9 rounded-l-none border-l border-message-action-foreground/20 px-2 sm:h-8",
192 )}
193 aria-label="Implementation actions"
194 {...pointerFocusProps}
195 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
196 />
197 }
198 >
199 <ChevronDownIcon className="size-3.5" />
200 </MenuTrigger>
201 <MenuPopup align="end" side="top" {...composerFloatingLayerProps}>
202 <MenuItem
203 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
204 onClick={() => void onImplementPlanInNewThread()}
205 >
206 Implement in a new thread
207 </MenuItem>
208 </MenuPopup>
209 </Menu>
210 </div>
211 );
212 }
213​
214 const sendButton = (
215 <button
216 type="submit"
217 className={cn(
218 "relative isolate flex h-9 w-9 items-center justify-center overflow-hidden rounded-full shadow-xs transition-all duration-150 enabled:cursor-pointer enabled:inset-shadow-2xs enabled:inset-shadow-white/16 hover:scale-105 active:inset-shadow-black/8 active:shadow-none disabled:pointer-events-none disabled:opacity-64 disabled:shadow-none disabled:hover:scale-100 sm:h-8 sm:w-8",
219 stageBackdropVariant
220 ? "bg-transparent text-white enabled:shadow-black/24 enabled:hover:brightness-110"
221 : "bg-message-action text-message-action-foreground enabled:shadow-message-action/24 hover:bg-message-action-hover",
222 )}
223 {...pointerFocusProps}
224 disabled={
225 isSendBusy ||
226 isSendDisabled ||
227 isConnecting ||
228 isEnvironmentUnavailable ||
229 !hasSendableContent
230 }
231 aria-label={
232 isEnvironmentUnavailable
233 ? "Environment disconnected"
234 : sendDisabledReason
235 ? sendDisabledReason
236 : isConnecting
237 ? "Connecting"
238 : isPreparingWorktree
239 ? "Preparing worktree"
240 : isSendBusy
241 ? "Sending"
242 : isRunning
243 ? "Queue message"
244 : "Send message"
245 }
246 >
247 {stageBackdropVariant ? (
248 <span className="absolute inset-0 -z-10" aria-hidden="true">
249 <StageBackdropButtonArt variant={stageBackdropVariant} />
250 </span>
251 ) : null}
252 {isConnecting || isSendBusy ? (
253 <Spinner size="sm" aria-hidden="true" />
254 ) : (
255 <svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true">
256 <path
257 d="M7 11.5V2.5M7 2.5L3 6.5M7 2.5L11 6.5"
258 stroke="currentColor"
259 strokeWidth="1.8"
260 strokeLinecap="round"
261 strokeLinejoin="round"
262 />
263 </svg>
264 )}
265 </button>
266 );
267​
268 if (!isRunning) {
269 return sendButton;
270 }
271​
272 // While a turn runs, a sendable draft queues for the next tool boundary, so
273 // the send button stays next to Stop on every viewport.
274 return (
275 <>
276 {renderStopGenerationButton(false)}
277 {hasSendableContent ? sendButton : null}
278 </>
279 );
280});