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