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