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

ComposerPrimaryActions.tsx

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