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

ComposerPrimaryActions.tsx

255 lines8.4 KB

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