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

ComposerPrimaryActions.tsx

328 lines11.7 KB

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