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

ComposerPrimaryActions.tsx

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