Open History to see the last commit
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;
17}
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 /** Enter-to-send is disabled on mobile viewports, where stop would otherwise
33 * be the only primary action and a running turn could not be steered. */
34 showSendWhileRunning?: boolean;
35 onPreviousPendingQuestion: () => void;
36 onInterrupt: () => void;
37 onImplementPlanInNewThread: () => void;
38}
39
40const formatPendingPrimaryActionLabel = (input: {
41 compact: boolean;
42 isLastQuestion: boolean;
43 isResponding: boolean;
44 questionIndex: number;
45}) => {
46 if (input.isResponding) {
47 return "Submitting...";
48 }
49 if (input.compact) {
50 return input.isLastQuestion ? "Submit" : "Next";
51 }
52 if (!input.isLastQuestion) {
53 return "Next question";
54 }
55 return input.questionIndex > 0 ? "Submit answers" : "Submit answer";
56};
57
58const preventPointerFocus: PointerEventHandler<HTMLElement> = (event) => {
59 event.preventDefault();
60};
61
62export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
63 compact,
64 pendingAction,
65 isRunning,
66 showPlanFollowUpPrompt,
67 promptHasText,
68 isSendBusy,
69 sendDisabledReason,
70 isConnecting,
71 isEnvironmentUnavailable,
72 isPreparingWorktree,
73 hasSendableContent,
74 preserveComposerFocusOnPointerDown = false,
75 showSendWhileRunning = false,
76 onPreviousPendingQuestion,
77 onInterrupt,
78 onImplementPlanInNewThread,
79}: ComposerPrimaryActionsProps) {
80 const pointerFocusProps = preserveComposerFocusOnPointerDown
81 ? { onPointerDown: preventPointerFocus }
82 : undefined;
83 const environmentIdentificationMode = useEnvironmentIdentificationMode();
84 const isSendDisabled = sendDisabledReason !== null;
85 const stageBackdropVariant = useSidebarStageBackdropVariant(
86 environmentIdentificationMode === "artwork",
87 );
88
89 const renderStopGenerationButton = (insidePendingAction: boolean) => (
90 <button
91 type="button"
92 className={cn(
93 "flex cursor-pointer items-center justify-center rounded-full bg-destructive/90 text-white shadow-xs shadow-destructive/24 inset-shadow-[0_1px_--theme(--color-white/16%)] transition-all duration-150 hover:bg-destructive hover:scale-105 active:inset-shadow-[0_1px_--theme(--color-black/8%)] active:shadow-none",
94 insidePendingAction
95 ? "size-8 sm:size-7"
96 : showSendWhileRunning && hasSendableContent
97 ? "size-9 sm:size-8"
98 : "size-8 sm:h-8 sm:w-8",
99 )}
100 {...pointerFocusProps}
101 onClick={onInterrupt}
102 aria-label="Stop generation"
103 >
104 <svg width="12" height="12" viewBox="0 0 12 12" fill="currentColor" aria-hidden="true">
105 <rect x="2" y="2" width="8" height="8" rx="1.5" />
106 </svg>
107 </button>
108 );
109
110 if (pendingAction) {
111 return (
112 <div className={cn("flex items-center justify-end", compact ? "gap-1.5" : "gap-2")}>
113 {isRunning ? renderStopGenerationButton(true) : null}
114 {pendingAction.questionIndex > 0 ? (
115 compact ? (
116 <Button
117 size="icon-sm"
118 variant="outline"
119 className="rounded-full"
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 className="rounded-full"
132 {...pointerFocusProps}
133 onClick={onPreviousPendingQuestion}
134 disabled={pendingAction.isResponding}
135 >
136 Previous
137 </Button>
138 )
139 ) : null}
140 <Button
141 type="submit"
142 size="sm"
143 className={cn(
144 "rounded-full bg-message-action text-message-action-foreground hover:bg-message-action-hover",
145 compact ? "px-3" : "px-4",
146 )}
147 {...pointerFocusProps}
148 disabled={
149 isEnvironmentUnavailable ||
150 pendingAction.isResponding ||
151 (pendingAction.isLastQuestion ? !pendingAction.isComplete : !pendingAction.canAdvance)
152 }
153 >
154 {formatPendingPrimaryActionLabel({
155 compact,
156 isLastQuestion: pendingAction.isLastQuestion,
157 isResponding: pendingAction.isResponding,
158 questionIndex: pendingAction.questionIndex,
159 })}
160 </Button>
161 </div>
162 );
163 }
164
165 if (showPlanFollowUpPrompt) {
166 if (promptHasText) {
167 return (
168 <Button
169 type="submit"
170 size="sm"
171 className={cn(
172 "rounded-full bg-message-action text-message-action-foreground hover:bg-message-action-hover",
173 compact ? "h-9 px-3 sm:h-8" : "h-9 px-4 sm:h-8",
174 )}
175 {...pointerFocusProps}
176 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
177 >
178 {isConnecting || isSendBusy ? "Sending..." : "Refine"}
179 </Button>
180 );
181 }
182
183 return (
184 <div data-chat-composer-implement-actions="true" className="flex items-center justify-end">
185 <Button
186 type="submit"
187 size="sm"
188 className="h-9 rounded-l-full rounded-r-none bg-message-action px-4 text-message-action-foreground hover:bg-message-action-hover sm:h-8"
189 {...pointerFocusProps}
190 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
191 >
192 {isConnecting || isSendBusy ? "Sending..." : "Implement"}
193 </Button>
194 <Menu>
195 <MenuTrigger
196 render={
197 <Button
198 size="sm"
199 variant="default"
200 className="h-9 rounded-l-none rounded-r-full border-l-message-action-foreground/20 bg-message-action px-2 text-message-action-foreground hover:bg-message-action-hover sm:h-8"
201 aria-label="Implementation actions"
202 {...pointerFocusProps}
203 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
204 />
205 }
206 >
207 <ChevronDownIcon className="size-3.5" />
208 </MenuTrigger>
209 <MenuPopup align="end" side="top" {...composerFloatingLayerProps}>
210 <MenuItem
211 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
212 onClick={() => void onImplementPlanInNewThread()}
213 >
214 Implement in a new thread
215 </MenuItem>
216 </MenuPopup>
217 </Menu>
218 </div>
219 );
220 }
221
222 const sendButton = (
223 <button
224 type="submit"
225 className={cn(
226 "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-[0_1px_--theme(--color-white/16%)] hover:scale-105 active:inset-shadow-[0_1px_--theme(--color-black/8%)] active:shadow-none disabled:pointer-events-none disabled:opacity-30 disabled:shadow-none disabled:hover:scale-100 sm:h-8 sm:w-8",
227 stageBackdropVariant
228 ? "bg-transparent text-white enabled:shadow-black/24 enabled:hover:brightness-110"
229 : "bg-message-action text-message-action-foreground enabled:shadow-message-action/24 hover:bg-message-action-hover",
230 )}
231 {...pointerFocusProps}
232 disabled={
233 isSendBusy ||
234 isSendDisabled ||
235 isConnecting ||
236 isEnvironmentUnavailable ||
237 !hasSendableContent
238 }
239 aria-label={
240 isEnvironmentUnavailable
241 ? "Environment disconnected"
242 : sendDisabledReason
243 ? sendDisabledReason
244 : isConnecting
245 ? "Connecting"
246 : isPreparingWorktree
247 ? "Preparing worktree"
248 : isSendBusy
249 ? "Sending"
250 : "Send message"
251 }
252 >
253 {stageBackdropVariant ? (
254 <span className="absolute inset-0 -z-10" aria-hidden="true">
255 <StageBackdropButtonArt variant={stageBackdropVariant} />
256 </span>
257 ) : null}
258 {isConnecting || isSendBusy ? (
259 <Spinner className="size-3.5" aria-hidden="true" />
260 ) : (
261 <svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true">
262 <path
263 d="M7 11.5V2.5M7 2.5L3 6.5M7 2.5L11 6.5"
264 stroke="currentColor"
265 strokeWidth="1.8"
266 strokeLinecap="round"
267 strokeLinejoin="round"
268 />
269 </svg>
270 )}
271 </button>
272 );
273
274 if (!isRunning) {
275 return sendButton;
276 }
277
278 return (
279 <>
280 {renderStopGenerationButton(false)}
281 {showSendWhileRunning && hasSendableContent ? sendButton : null}
282 </>
283 );
284});