Elffuss BraidHub

kikocis/continue public

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