Open History to see the last commit
1import { memo, type MouseEventHandler, type PointerEventHandler } from "react";
2import {
3 CheckIcon,
4 ChevronDownIcon,
5 ChevronLeftIcon,
6 CornerUpRightIcon,
7 ListPlusIcon,
8} from "lucide-react";
9import { useEnvironmentIdentificationMode } from "~/hooks/useSettings";
10import { cn } from "~/lib/utils";
11import { useShortcutModifierState } from "../../shortcutModifierState";
12import { StageBackdropButtonArt, useSidebarStageBackdropVariant } from "../SidebarStageBackdrop";
13import { Button } from "../ui/button";
14import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu";
15import { Spinner } from "../ui/spinner";
16import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
17import { composerFloatingLayerProps } from "./composerEventScope";
18import {
19 alternateComposerDispatchAction,
20 resolveComposerDispatchMode,
21} from "@t3tools/client-runtime/state/composer-dispatch";
22
23interface PendingActionState {
24 questionIndex: number;
25 isLastQuestion: boolean;
26 canAdvance: boolean;
27 isResponding: boolean;
28 isComplete: boolean;
29}
30
31interface ComposerPrimaryActionsProps {
32 compact: boolean;
33 pendingAction: PendingActionState | null;
34 isRunning: boolean;
35 followUpBehavior?: "queue" | "steer";
36 alternateShortcutLabel?: string | null;
37 showPlanFollowUpPrompt: boolean;
38 promptHasText: boolean;
39 isSendBusy: boolean;
40 sendDisabledReason: string | null;
41 isConnecting: boolean;
42 isEnvironmentUnavailable: boolean;
43 isPreparingWorktree: boolean;
44 hasSendableContent: boolean;
45 preserveComposerFocusOnPointerDown?: boolean;
46 isEditingQueuedMessage?: boolean;
47 onSubmitMessage?: MouseEventHandler<HTMLButtonElement>;
48 onPreviousPendingQuestion: () => void;
49 onInterrupt: () => void;
50 onImplementPlanInNewThread: () => void;
51}
52
53const formatPendingPrimaryActionLabel = (input: {
54 compact: boolean;
55 isLastQuestion: boolean;
56 isResponding: boolean;
57 questionIndex: number;
58}) => {
59 if (input.isResponding) {
60 return "Submitting...";
61 }
62 if (input.compact) {
63 return input.isLastQuestion ? "Submit" : "Next";
64 }
65 if (!input.isLastQuestion) {
66 return "Next question";
67 }
68 return input.questionIndex > 0 ? "Submit answers" : "Submit answer";
69};
70
71const preventPointerFocus: PointerEventHandler<HTMLElement> = (event) => {
72 event.preventDefault();
73};
74
75export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
76 compact,
77 pendingAction,
78 isRunning,
79 followUpBehavior = "steer",
80 alternateShortcutLabel = null,
81 showPlanFollowUpPrompt,
82 promptHasText,
83 isSendBusy,
84 sendDisabledReason,
85 isConnecting,
86 isEnvironmentUnavailable,
87 isPreparingWorktree,
88 hasSendableContent,
89 preserveComposerFocusOnPointerDown = false,
90 isEditingQueuedMessage = false,
91 onSubmitMessage,
92 onPreviousPendingQuestion,
93 onInterrupt,
94 onImplementPlanInNewThread,
95}: ComposerPrimaryActionsProps) {
96 const pointerFocusProps = preserveComposerFocusOnPointerDown
97 ? { onPointerDown: preventPointerFocus }
98 : undefined;
99 const environmentIdentificationMode = useEnvironmentIdentificationMode();
100 const shortcutModifiers = useShortcutModifierState();
101 const isQueuing =
102 !isEditingQueuedMessage &&
103 resolveComposerDispatchMode({
104 running: isRunning,
105 activeTurnDefault: followUpBehavior,
106 alternateModifier: shortcutModifiers.metaKey || shortcutModifiers.ctrlKey,
107 }) === "queue";
108 const alternateAction = alternateComposerDispatchAction(followUpBehavior);
109 const isSendDisabled = sendDisabledReason !== null;
110 const stageBackdropVariant = useSidebarStageBackdropVariant(
111 environmentIdentificationMode === "artwork",
112 );
113
114 const renderStopGenerationButton = (insidePendingAction: boolean) => (
115 <Tooltip key="interrupt">
116 <TooltipTrigger
117 render={
118 <button
119 type="button"
120 className={cn(
121 "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 [&_svg]:pointer-events-none",
122 insidePendingAction ? "size-8 sm:size-7" : "size-8 sm:h-8 sm:w-8",
123 )}
124 {...pointerFocusProps}
125 onClick={onInterrupt}
126 aria-label="Stop generation"
127 />
128 }
129 >
130 <svg width="12" height="12" viewBox="0 0 12 12" fill="currentColor" aria-hidden="true">
131 <rect x="2" y="2" width="8" height="8" rx="1.5" />
132 </svg>
133 </TooltipTrigger>
134 <TooltipPopup>Interrupt</TooltipPopup>
135 </Tooltip>
136 );
137
138 if (pendingAction) {
139 return (
140 <div className={cn("flex items-center justify-end", compact ? "gap-1.5" : "gap-2")}>
141 {isRunning ? renderStopGenerationButton(true) : null}
142 {pendingAction.questionIndex > 0 ? (
143 compact ? (
144 <Button
145 size="icon-sm"
146 variant="outline"
147 className="rounded-full"
148 {...pointerFocusProps}
149 onClick={onPreviousPendingQuestion}
150 disabled={pendingAction.isResponding}
151 aria-label="Previous question"
152 >
153 <ChevronLeftIcon className="size-3.5" />
154 </Button>
155 ) : (
156 <Button
157 size="sm"
158 variant="outline"
159 className="rounded-full"
160 {...pointerFocusProps}
161 onClick={onPreviousPendingQuestion}
162 disabled={pendingAction.isResponding}
163 >
164 Previous
165 </Button>
166 )
167 ) : null}
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 ? "px-3" : "px-4",
174 )}
175 {...pointerFocusProps}
176 disabled={
177 isEnvironmentUnavailable ||
178 pendingAction.isResponding ||
179 (pendingAction.isLastQuestion ? !pendingAction.isComplete : !pendingAction.canAdvance)
180 }
181 >
182 {formatPendingPrimaryActionLabel({
183 compact,
184 isLastQuestion: pendingAction.isLastQuestion,
185 isResponding: pendingAction.isResponding,
186 questionIndex: pendingAction.questionIndex,
187 })}
188 </Button>
189 </div>
190 );
191 }
192
193 if (showPlanFollowUpPrompt) {
194 if (promptHasText) {
195 return (
196 <Button
197 type="submit"
198 size="sm"
199 className={cn(
200 "rounded-full bg-message-action text-message-action-foreground hover:bg-message-action-hover",
201 compact ? "h-9 px-3 sm:h-8" : "h-9 px-4 sm:h-8",
202 )}
203 {...pointerFocusProps}
204 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
205 >
206 {isConnecting || isSendBusy ? "Sending..." : "Refine"}
207 </Button>
208 );
209 }
210
211 return (
212 <div data-chat-composer-implement-actions="true" className="flex items-center justify-end">
213 <Button
214 type="submit"
215 size="sm"
216 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"
217 {...pointerFocusProps}
218 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
219 >
220 {isConnecting || isSendBusy ? "Sending..." : "Implement"}
221 </Button>
222 <Menu>
223 <MenuTrigger
224 render={
225 <Button
226 size="sm"
227 variant="default"
228 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"
229 aria-label="Implementation actions"
230 {...pointerFocusProps}
231 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
232 />
233 }
234 >
235 <ChevronDownIcon className="size-3.5" />
236 </MenuTrigger>
237 <MenuPopup align="end" side="top" {...composerFloatingLayerProps}>
238 <MenuItem
239 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
240 onClick={() => void onImplementPlanInNewThread()}
241 >
242 Implement in a new thread
243 </MenuItem>
244 </MenuPopup>
245 </Menu>
246 </div>
247 );
248 }
249
250 if (isRunning && !hasSendableContent && !isEditingQueuedMessage) {
251 return renderStopGenerationButton(false);
252 }
253
254 const submitLabel = isEditingQueuedMessage
255 ? "Update queued message"
256 : isQueuing
257 ? "Queue message"
258 : isRunning
259 ? "Steer message"
260 : "Submit message";
261 const submitStatus = isEnvironmentUnavailable
262 ? "Environment disconnected"
263 : (sendDisabledReason ??
264 (isConnecting
265 ? "Connecting"
266 : isPreparingWorktree
267 ? "Preparing worktree"
268 : isSendBusy
269 ? isEditingQueuedMessage
270 ? "Updating queued message"
271 : "Submitting message"
272 : null));
273 const submitTooltip =
274 submitStatus ??
275 (isRunning && !isEditingQueuedMessage
276 ? `Click to ${followUpBehavior}, Ctrl/⌘-click${alternateShortcutLabel ? ` or ${alternateShortcutLabel}` : ""} to ${alternateAction}`
277 : submitLabel);
278
279 const sendButton = (
280 <button
281 type="submit"
282 className={cn(
283 "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 [&_svg]:pointer-events-none",
284 stageBackdropVariant
285 ? "bg-transparent text-white enabled:shadow-black/24 enabled:hover:brightness-110"
286 : "bg-message-action text-message-action-foreground enabled:shadow-message-action/24 hover:bg-message-action-hover",
287 )}
288 {...pointerFocusProps}
289 onClick={onSubmitMessage}
290 disabled={
291 isSendBusy ||
292 isSendDisabled ||
293 isConnecting ||
294 isEnvironmentUnavailable ||
295 !hasSendableContent
296 }
297 aria-label={submitStatus ?? submitLabel}
298 >
299 {stageBackdropVariant ? (
300 <span className="pointer-events-none absolute inset-0 -z-10" aria-hidden="true">
301 <StageBackdropButtonArt variant={stageBackdropVariant} />
302 </span>
303 ) : null}
304 {isConnecting || isSendBusy ? (
305 <Spinner className="size-3.5" aria-hidden="true" />
306 ) : isEditingQueuedMessage ? (
307 <CheckIcon className="size-4" aria-hidden="true" />
308 ) : isQueuing ? (
309 <ListPlusIcon className="size-4" aria-hidden="true" />
310 ) : isRunning ? (
311 <CornerUpRightIcon className="size-4" aria-hidden="true" />
312 ) : (
313 <svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true">
314 <path
315 d="M7 11.5V2.5M7 2.5L3 6.5M7 2.5L11 6.5"
316 stroke="currentColor"
317 strokeWidth="1.8"
318 strokeLinecap="round"
319 strokeLinejoin="round"
320 />
321 </svg>
322 )}
323 </button>
324 );
325
326 return (
327 <Tooltip key="submit">
328 <TooltipTrigger render={<span className="inline-flex" />}>{sendButton}</TooltipTrigger>
329 <TooltipPopup>{submitTooltip}</TooltipPopup>
330 </Tooltip>
331 );
332});