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

ComposerPrimaryActions.tsx

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