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

ComposerPrimaryActions.tsx

335 lines12.3 KB

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