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

ComposerPrimaryActions.tsx

291 lines9.9 KB

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