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