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

ComposerPrimaryActions.tsx

342 lines12.3 KB

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