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

ComposerPrimaryActions.tsx

256 lines8.4 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 if (pendingAction) {
85 return (
86 <div className={cn("flex items-center justify-end", compact ? "gap-1.5" : "gap-2")}>
87 {pendingAction.questionIndex > 0 ? (
88 compact ? (
89 <Button
90 size="icon-sm"
91 variant="outline"
92 className="rounded-full"
93 {...pointerFocusProps}
94 onClick={onPreviousPendingQuestion}
95 disabled={pendingAction.isResponding}
96 aria-label="Previous question"
97 >
98 <ChevronLeftIcon className="size-3.5" />
99 </Button>
100 ) : (
101 <Button
102 size="sm"
103 variant="outline"
104 className="rounded-full"
105 {...pointerFocusProps}
106 onClick={onPreviousPendingQuestion}
107 disabled={pendingAction.isResponding}
108 >
109 Previous
110 </Button>
111 )
112 ) : null}
113 <Button
114 type="submit"
115 size="sm"
116 className={cn("rounded-full", compact ? "px-3" : "px-4")}
117 {...pointerFocusProps}
118 disabled={
119 isEnvironmentUnavailable ||
120 pendingAction.isResponding ||
121 (pendingAction.isLastQuestion ? !pendingAction.isComplete : !pendingAction.canAdvance)
122 }
123 >
124 {formatPendingPrimaryActionLabel({
125 compact,
126 isLastQuestion: pendingAction.isLastQuestion,
127 isResponding: pendingAction.isResponding,
128 questionIndex: pendingAction.questionIndex,
129 })}
130 </Button>
131 </div>
132 );
133 }
134​
135 if (isRunning) {
136 return (
137 <button
138 type="button"
139 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"
140 {...pointerFocusProps}
141 onClick={onInterrupt}
142 aria-label="Stop generation"
143 >
144 <svg width="12" height="12" viewBox="0 0 12 12" fill="currentColor" aria-hidden="true">
145 <rect x="2" y="2" width="8" height="8" rx="1.5" />
146 </svg>
147 </button>
148 );
149 }
150​
151 if (showPlanFollowUpPrompt) {
152 if (promptHasText) {
153 return (
154 <Button
155 type="submit"
156 size="sm"
157 className={cn("rounded-full", compact ? "h-9 px-3 sm:h-8" : "h-9 px-4 sm:h-8")}
158 {...pointerFocusProps}
159 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
160 >
161 {isConnecting || isSendBusy ? "Sending..." : "Refine"}
162 </Button>
163 );
164 }
165​
166 return (
167 <div data-chat-composer-implement-actions="true" className="flex items-center justify-end">
168 <Button
169 type="submit"
170 size="sm"
171 className="h-9 rounded-l-full rounded-r-none px-4 sm:h-8"
172 {...pointerFocusProps}
173 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
174 >
175 {isConnecting || isSendBusy ? "Sending..." : "Implement"}
176 </Button>
177 <Menu>
178 <MenuTrigger
179 render={
180 <Button
181 size="sm"
182 variant="default"
183 className="h-9 rounded-l-none rounded-r-full border-l-white/12 px-2 sm:h-8"
184 aria-label="Implementation actions"
185 {...pointerFocusProps}
186 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
187 />
188 }
189 >
190 <ChevronDownIcon className="size-3.5" />
191 </MenuTrigger>
192 <MenuPopup align="end" side="top">
193 <MenuItem
194 disabled={isSendBusy || isSendDisabled || isConnecting || isEnvironmentUnavailable}
195 onClick={() => void onImplementPlanInNewThread()}
196 >
197 Implement in a new thread
198 </MenuItem>
199 </MenuPopup>
200 </Menu>
201 </div>
202 );
203 }
204​
205 return (
206 <button
207 type="submit"
208 className={cn(
209 "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",
210 stageBackdropVariant
211 ? "bg-transparent enabled:shadow-black/24 enabled:hover:brightness-110"
212 : "bg-primary/90 enabled:shadow-primary/24 hover:bg-primary",
213 )}
214 {...pointerFocusProps}
215 disabled={
216 isSendBusy ||
217 isSendDisabled ||
218 isConnecting ||
219 isEnvironmentUnavailable ||
220 !hasSendableContent
221 }
222 aria-label={
223 isEnvironmentUnavailable
224 ? "Environment disconnected"
225 : sendDisabledReason
226 ? sendDisabledReason
227 : isConnecting
228 ? "Connecting"
229 : isPreparingWorktree
230 ? "Preparing worktree"
231 : isSendBusy
232 ? "Sending"
233 : "Send message"
234 }
235 >
236 {stageBackdropVariant ? (
237 <span className="absolute inset-0 -z-10" aria-hidden="true">
238 <StageBackdropButtonArt variant={stageBackdropVariant} />
239 </span>
240 ) : null}
241 {isConnecting || isSendBusy ? (
242 <Spinner className="size-3.5" aria-hidden="true" />
243 ) : (
244 <svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true">
245 <path
246 d="M7 11.5V2.5M7 2.5L3 6.5M7 2.5L11 6.5"
247 stroke="currentColor"
248 strokeWidth="1.8"
249 strokeLinecap="round"
250 strokeLinejoin="round"
251 />
252 </svg>
253 )}
254 </button>
255 );
256});