Elffuss BraidHub

kikocis/continue public

9d8db5fc402e · 12.0 KiBRawPermalink
1import {
2 ArrowPathIcon,
3 ArrowTopRightOnSquareIcon,
4 BuildingOfficeIcon,
5 ChevronDownIcon,
6 Cog6ToothIcon,
7 ExclamationTriangleIcon,
8 PlusIcon,
9} from "@heroicons/react/24/outline";
10import { useContext, useEffect, useMemo, useRef, useState } from "react";
11import { useAuth } from "../../../context/Auth";
12import { IdeMessengerContext } from "../../../context/IdeMessenger";
13import {
14 selectCurrentOrg,
15 setSelectedOrgId,
16 setSelectedProfile,
17} from "../../../redux";
18import { useAppDispatch, useAppSelector } from "../../../redux/hooks";
19import {
20 fontSize,
21 getMetaKeyLabel,
22 isLocalProfile,
23 isMetaEquivalentKeyPressed,
24} from "../../../util";
25import {
26 Listbox,
27 ListboxButton,
28 ListboxOption,
29 ListboxOptions,
30 Transition,
31} from "../../ui";
32
33import type { ProfileDescription } from "core/config/ConfigHandler";
34import { useNavigate } from "react-router-dom";
35import { vscCommandCenterInactiveBorder } from "../..";
36import { cn } from "../../../util/cn";
37import { ROUTES } from "../../../util/navigation";
38import { useLump } from "../../mainInput/Lump/LumpContext";
39import { useFontSize } from "../../ui/font";
40import AssistantIcon from "./AssistantIcon";
41
42interface AssistantSelectOptionProps {
43 profile: ProfileDescription;
44 selected: boolean;
45 onClick: () => void;
46}
47
48const AssistantSelectOption = ({
49 profile,
50 selected,
51 onClick,
52}: AssistantSelectOptionProps) => {
53 const tinyFont = useFontSize(-4);
54
55 const navigate = useNavigate();
56 const { setSelectedSection } = useLump();
57
58 const hasFatalErrors = useMemo(() => {
59 return !!profile.errors?.find((error) => error.fatal);
60 }, [profile.errors]);
61
62 const dispatch = useAppDispatch();
63 const ideMessenger = useContext(IdeMessengerContext);
64
65 function handleOptionClick() {
66 // optimistic update
67 dispatch(setSelectedProfile(profile.id));
68 // notify core which will handle actual update
69 ideMessenger.post("didChangeSelectedProfile", {
70 id: profile.id,
71 });
72 onClick();
73 }
74
75 function handleConfigure() {
76 ideMessenger.post("config/openProfile", { profileId: profile.id });
77 onClick();
78 }
79
80 function handleClickError() {
81 if (profile.id === "local") {
82 navigate(ROUTES.HOME);
83 setSelectedSection("error");
84 } else {
85 ideMessenger.post("config/openProfile", { profileId: profile.id });
86 }
87 onClick();
88 }
89
90 return (
91 <ListboxOption
92 value={profile.id}
93 disabled={hasFatalErrors}
94 onClick={!hasFatalErrors ? handleOptionClick : undefined}
95 fontSizeModifier={-2}
96 className={selected ? "bg-list-active text-list-active-foreground" : ""}
97 >
98 <div
99 className="flex w-full flex-col gap-0.5"
100 style={{
101 fontSize: tinyFont,
102 }}
103 >
104 <div className="flex w-full items-center justify-between gap-2 bg-transparent">
105 <div className="flex w-full items-center gap-1">
106 <div className="flex h-4 w-4 flex-shrink-0">
107 <AssistantIcon size={3.5} assistant={profile} />
108 </div>
109 <span
110 className={`line-clamp-1 flex-1 ${selected ? "font-semibold" : ""}`}
111 >
112 {profile.title}
113 </span>
114 </div>
115 <div className="flex flex-row items-center gap-1">
116 {profile.errors && profile.errors?.length > 0 && (
117 <ExclamationTriangleIcon
118 data-tooltip-id={`${profile.id}-errors-tooltip`}
119 className="h-3 w-3 flex-shrink-0 cursor-pointer text-red-500"
120 onClick={(e) => {
121 e.preventDefault();
122 e.stopPropagation();
123 handleClickError();
124 }}
125 />
126 )}
127 {isLocalProfile(profile) ? (
128 <Cog6ToothIcon
129 className="text-lightgray h-3 w-3 flex-shrink-0 cursor-pointer"
130 onClick={(e) => {
131 e.stopPropagation();
132 e.preventDefault();
133 handleConfigure();
134 }}
135 />
136 ) : (
137 <ArrowTopRightOnSquareIcon
138 className="text-lightgray h-3 w-3 flex-shrink-0 cursor-pointer"
139 onClick={(e) => {
140 e.stopPropagation();
141 e.preventDefault();
142 handleConfigure();
143 }}
144 />
145 )}
146 </div>
147 </div>
148 </div>
149 </ListboxOption>
150 );
151};
152
153export default function AssistantSelect() {
154 const dispatch = useAppDispatch();
155 const buttonRef = useRef<HTMLButtonElement>(null);
156 const { selectedProfile, refreshProfiles } = useAuth();
157 const currentOrg = useAppSelector(selectCurrentOrg);
158 const orgs = useAppSelector((store) => store.profiles.organizations);
159 const ideMessenger = useContext(IdeMessengerContext);
160 const { isToolbarExpanded } = useLump();
161 const [loading, setLoading] = useState(false);
162
163 const { profiles, session, login } = useAuth();
164 const navigate = useNavigate();
165
166 function close() {
167 if (buttonRef.current) {
168 buttonRef.current.click();
169 }
170 }
171 function onNewAssistant() {
172 ideMessenger.post("controlPlane/openUrl", {
173 path: "new",
174 orgSlug: currentOrg?.slug,
175 });
176 close();
177 }
178
179 useEffect(() => {
180 let lastToggleTime = 0;
181 const DEBOUNCE_MS = 800;
182
183 const handleKeyDown = (event: KeyboardEvent) => {
184 if (
185 event.key === "'" &&
186 isMetaEquivalentKeyPressed(event as any) &&
187 event.shiftKey
188 ) {
189 const now = Date.now();
190
191 if (now - lastToggleTime >= DEBOUNCE_MS) {
192 lastToggleTime = now;
193
194 const profileIds = profiles?.map((profile) => profile.id) ?? [];
195 // In case of 1 or 0 profiles just does nothing
196 if (profileIds.length < 2) {
197 return;
198 }
199 let nextId = profileIds[0];
200 if (selectedProfile) {
201 const curIndex = profileIds.indexOf(selectedProfile.id);
202 const nextIndex = (curIndex + 1) % profileIds.length;
203 nextId = profileIds[nextIndex];
204 }
205 // Optimistic update
206 dispatch(setSelectedProfile(nextId));
207 ideMessenger.post("didChangeSelectedProfile", {
208 id: nextId,
209 });
210 }
211 }
212 };
213
214 window.addEventListener("keydown", handleKeyDown);
215 return () => {
216 window.removeEventListener("keydown", handleKeyDown);
217 };
218 }, [currentOrg, selectedProfile]);
219
220 const cycleOrgs = () => {
221 const orgIds = orgs.map((org) => org.id);
222 if (orgIds.length < 2) {
223 return;
224 }
225 let nextId = orgIds[0];
226 if (currentOrg) {
227 const curIndex = orgIds.indexOf(currentOrg.id);
228 const nextIndex = (curIndex + 1) % orgIds.length;
229 nextId = orgIds[nextIndex];
230 }
231 // Optimistic update
232 dispatch(setSelectedOrgId(nextId));
233 ideMessenger.post("didChangeSelectedOrg", {
234 id: nextId,
235 });
236 };
237
238 const tinyFont = useFontSize(-4);
239 const smallFont = useFontSize(-3);
240
241 if (!selectedProfile) {
242 return (
243 <div
244 onClick={() => {
245 ideMessenger.request("controlPlane/openUrl", {
246 path: "/new?type=assistant",
247 orgSlug: currentOrg?.slug,
248 });
249 }}
250 className="flex cursor-pointer select-none items-center gap-1 text-gray-400"
251 style={{ fontSize: smallFont }}
252 >
253 <PlusIcon className="h-3 w-3 flex-shrink-0 select-none" />
254 <span
255 className={`line-clamp-1 select-none break-all ${isToolbarExpanded ? "xs:hidden sm:line-clamp-1" : ""}`}
256 >
257 Create your first assistant
258 </span>
259 </div>
260 );
261 }
262
263 return (
264 <Listbox>
265 <div className="relative">
266 <ListboxButton
267 data-testid="assistant-select-button"
268 ref={buttonRef}
269 className="border-none bg-transparent text-gray-400 hover:brightness-125"
270 style={{ fontSize: fontSize(-3) }}
271 >
272 <div className="flex flex-row items-center gap-1.5">
273 <div className="h-3 w-3 flex-shrink-0 select-none">
274 <AssistantIcon size={3} assistant={selectedProfile} />
275 </div>
276 <span
277 className={`line-clamp-1 select-none break-all ${isToolbarExpanded ? "xs:hidden sm:line-clamp-1" : ""}`}
278 >
279 {selectedProfile.title}
280 </span>
281 </div>
282 <ChevronDownIcon
283 className="h-2 w-2 flex-shrink-0 select-none"
284 aria-hidden="true"
285 />
286 </ListboxButton>
287
288 <Transition>
289 <ListboxOptions className="pb-0">
290 <div className="flex gap-1.5 px-2.5 py-1">
291 <span>Assistants</span>
292 <div
293 className="flex cursor-pointer flex-row items-center gap-1 hover:brightness-125"
294 onClick={async (e) => {
295 e.stopPropagation();
296 setLoading(true);
297 await refreshProfiles();
298 setLoading(false);
299 buttonRef.current?.click();
300 }}
301 >
302 <ArrowPathIcon
303 className={cn(
304 "text-lightgray h-2.5 w-2.5",
305 loading && "animate-spin-slow",
306 )}
307 />
308 </div>
309 </div>
310
311 <div
312 className={`thin-scrollbar flex max-h-[300px] flex-col overflow-y-auto`}
313 >
314 {profiles?.map((profile, idx) => {
315 return (
316 <AssistantSelectOption
317 key={idx}
318 profile={profile}
319 onClick={close}
320 selected={profile.id === selectedProfile.id}
321 />
322 );
323 })}
324 </div>
325
326 <div className="flex flex-col">
327 <div
328 className="my-0 h-[0.5px]"
329 style={{
330 backgroundColor: vscCommandCenterInactiveBorder,
331 }}
332 />
333
334 <div className="flex flex-row items-center">
335 <ListboxOption
336 className="w-full"
337 value={"new-assistant"}
338 fontSizeModifier={-2}
339 onClick={session ? onNewAssistant : () => login(false)}
340 >
341 <div
342 className="text-lightgray flex flex-row items-center gap-2"
343 style={{
344 fontSize: tinyFont,
345 }}
346 >
347 <PlusIcon className="ml-0.5 h-3 w-3 flex-shrink-0" />
348 New Assistant
349 </div>
350 </ListboxOption>
351 </div>
352
353 <div
354 className="my-0 h-[0.5px]"
355 style={{
356 backgroundColor: vscCommandCenterInactiveBorder,
357 }}
358 />
359
360 <div
361 className="text-lightgray flex items-center justify-between px-2 py-1"
362 style={{
363 fontSize: tinyFont,
364 }}
365 >
366 <span
367 className="block"
368 style={{
369 fontSize: tinyFont - 1,
370 }}
371 >
372 <code>{getMetaKeyLabel()} ⇧ '</code> to toggle
373 </span>
374 <div
375 className="ml-auto flex items-center gap-1"
376 onClick={cycleOrgs}
377 >
378 {currentOrg?.iconUrl ? (
379 <img
380 src={currentOrg.iconUrl}
381 className="h-2.5 w-2.5 rounded-full"
382 />
383 ) : (
384 <BuildingOfficeIcon className="h-4 w-4" />
385 )}
386 <span
387 className="hover:cursor-pointer hover:underline"
388 style={{
389 fontSize: tinyFont - 1,
390 }}
391 >
392 {currentOrg?.name || "Personal"}
393 </span>
394 </div>
395 </div>
396 </div>
397 </ListboxOptions>
398 </Transition>
399 </div>
400 </Listbox>
401 );
402}