Elffuss BraidHub

kikocis/continue public

68dbf00e2c2f · 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 type { ProfileDescription } from "core/config/ConfigHandler";
11import { useContext, useEffect, useMemo, useRef, useState } from "react";
12import { useNavigate } from "react-router-dom";
13import { useAuth } from "../../../context/Auth";
14import { IdeMessengerContext } from "../../../context/IdeMessenger";
15import { useAppDispatch, useAppSelector } from "../../../redux/hooks";
16import {
17 selectCurrentOrg,
18 setSelectedOrgId,
19 setSelectedProfile,
20} from "../../../redux/slices/profilesSlice";
21import {
22 fontSize,
23 getMetaKeyLabel,
24 isLocalProfile,
25 isMetaEquivalentKeyPressed,
26} from "../../../util";
27import { cn } from "../../../util/cn";
28import { ROUTES } from "../../../util/navigation";
29import { useLump } from "../../mainInput/Lump/LumpContext";
30import {
31 Listbox,
32 ListboxButton,
33 ListboxOption,
34 ListboxOptions,
35 Transition,
36 useFontSize,
37} from "../../ui";
38import AssistantIcon from "./AssistantIcon";
39interface AssistantSelectOptionProps {
40 profile: ProfileDescription;
41 selected: boolean;
42 onClick: () => void;
43}
44
45const AssistantSelectOption = ({
46 profile,
47 selected,
48 onClick,
49}: AssistantSelectOptionProps) => {
50 const tinyFont = useFontSize(-4);
51
52 const navigate = useNavigate();
53 const { setSelectedSection } = useLump();
54
55 const hasFatalErrors = useMemo(() => {
56 return !!profile.errors?.find((error) => error.fatal);
57 }, [profile.errors]);
58
59 const dispatch = useAppDispatch();
60 const ideMessenger = useContext(IdeMessengerContext);
61
62 function handleOptionClick() {
63 // optimistic update
64 dispatch(setSelectedProfile(profile.id));
65 // notify core which will handle actual update
66 ideMessenger.post("didChangeSelectedProfile", {
67 id: profile.id,
68 });
69 onClick();
70 }
71
72 function handleConfigure() {
73 ideMessenger.post("config/openProfile", { profileId: profile.id });
74 onClick();
75 }
76
77 function handleClickError() {
78 if (profile.id === "local") {
79 navigate(ROUTES.HOME);
80 setSelectedSection("error");
81 } else {
82 ideMessenger.post("config/openProfile", { profileId: profile.id });
83 }
84 onClick();
85 }
86
87 return (
88 <ListboxOption
89 value={profile.id}
90 disabled={hasFatalErrors}
91 onClick={!hasFatalErrors ? handleOptionClick : undefined}
92 fontSizeModifier={-2}
93 className={selected ? "bg-list-active text-list-active-foreground" : ""}
94 >
95 <div
96 className="flex w-full flex-col gap-0.5"
97 style={{
98 fontSize: tinyFont,
99 }}
100 >
101 <div className="flex w-full items-center justify-between gap-2 bg-transparent">
102 <div className="flex w-full items-center gap-1">
103 <div className="flex h-4 w-4 flex-shrink-0">
104 <AssistantIcon size={3.5} assistant={profile} />
105 </div>
106 <span
107 className={`line-clamp-1 flex-1 ${selected ? "font-semibold" : ""}`}
108 >
109 {profile.title}
110 </span>
111 </div>
112 <div className="flex flex-row items-center gap-1">
113 {profile.errors && profile.errors?.length > 0 && (
114 <ExclamationTriangleIcon
115 data-tooltip-id={`${profile.id}-errors-tooltip`}
116 className="text-error h-3 w-3 flex-shrink-0 cursor-pointer"
117 onClick={(e) => {
118 e.preventDefault();
119 e.stopPropagation();
120 handleClickError();
121 }}
122 />
123 )}
124 {isLocalProfile(profile) ? (
125 <Cog6ToothIcon
126 className="text-description h-3 w-3 flex-shrink-0 cursor-pointer"
127 onClick={(e) => {
128 e.stopPropagation();
129 e.preventDefault();
130 handleConfigure();
131 }}
132 />
133 ) : (
134 <ArrowTopRightOnSquareIcon
135 className="text-description h-3 w-3 flex-shrink-0 cursor-pointer"
136 onClick={(e) => {
137 e.stopPropagation();
138 e.preventDefault();
139 handleConfigure();
140 }}
141 />
142 )}
143 </div>
144 </div>
145 </div>
146 </ListboxOption>
147 );
148};
149
150export default function AssistantSelect() {
151 const dispatch = useAppDispatch();
152 const buttonRef = useRef<HTMLButtonElement>(null);
153 const currentOrg = useAppSelector(selectCurrentOrg);
154 const orgs = useAppSelector((store) => store.profiles.organizations);
155 const ideMessenger = useContext(IdeMessengerContext);
156 const { isToolbarExpanded } = useLump();
157 const [loading, setLoading] = useState(false);
158 const { profiles, session, login, selectedProfile, refreshProfiles } =
159 useAuth();
160
161 function close() {
162 if (buttonRef.current) {
163 buttonRef.current.click();
164 }
165 }
166 function onNewAssistant() {
167 ideMessenger.post("controlPlane/openUrl", {
168 path: "new",
169 orgSlug: currentOrg?.slug,
170 });
171 close();
172 }
173
174 useEffect(() => {
175 let lastToggleTime = 0;
176 const DEBOUNCE_MS = 800;
177
178 const handleKeyDown = (event: KeyboardEvent) => {
179 if (
180 event.key === "'" &&
181 isMetaEquivalentKeyPressed(event as any) &&
182 event.shiftKey
183 ) {
184 const now = Date.now();
185
186 if (now - lastToggleTime >= DEBOUNCE_MS) {
187 lastToggleTime = now;
188
189 const profileIds = profiles?.map((profile) => profile.id) ?? [];
190 // In case of 1 or 0 profiles just does nothing
191 if (profileIds.length < 2) {
192 return;
193 }
194 let nextId = profileIds[0];
195 if (selectedProfile) {
196 const curIndex = profileIds.indexOf(selectedProfile.id);
197 const nextIndex = (curIndex + 1) % profileIds.length;
198 nextId = profileIds[nextIndex];
199 }
200 // Optimistic update
201 dispatch(setSelectedProfile(nextId));
202 ideMessenger.post("didChangeSelectedProfile", {
203 id: nextId,
204 });
205 }
206 }
207 };
208
209 window.addEventListener("keydown", handleKeyDown);
210 return () => {
211 window.removeEventListener("keydown", handleKeyDown);
212 };
213 }, [currentOrg, selectedProfile]);
214
215 const cycleOrgs = () => {
216 if (!session) {
217 void login(false);
218 return;
219 }
220
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 void ideMessenger.request("controlPlane/openUrl", {
246 path: "/new?type=assistant",
247 orgSlug: currentOrg?.slug,
248 });
249 }}
250 className="text-description flex cursor-pointer select-none items-center gap-1"
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="text-description border-none bg-transparent 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 className="font-semibold">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-description 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 className="flex flex-row items-center">
328 <ListboxOption
329 value={"new-assistant"}
330 fontSizeModifier={-2}
331 className="border-border w-full border-x-0 border-y border-solid"
332 onClick={session ? onNewAssistant : () => login(false)}
333 >
334 <div
335 className="text-description flex flex-row items-center gap-2"
336 style={{
337 fontSize: tinyFont,
338 }}
339 >
340 <PlusIcon className="ml-0.5 h-3 w-3 flex-shrink-0" />
341 New Assistant
342 </div>
343 </ListboxOption>
344 </div>
345
346 <div
347 className="text-description flex items-center justify-between gap-1.5 px-2 py-1"
348 style={{
349 fontSize: tinyFont,
350 }}
351 >
352 <span
353 className="block"
354 style={{
355 fontSize: tinyFont - 1,
356 }}
357 >
358 <code>{getMetaKeyLabel()} ⇧ '</code> to toggle
359 </span>
360 <div
361 className="ml-auto flex items-center gap-1"
362 onClick={cycleOrgs}
363 >
364 {currentOrg?.iconUrl ? (
365 <img
366 src={currentOrg.iconUrl}
367 className="h-2.5 w-2.5 rounded-full"
368 />
369 ) : (
370 <BuildingOfficeIcon className="h-4 w-4" />
371 )}
372 <span
373 className="hover:cursor-pointer hover:underline"
374 style={{
375 fontSize: tinyFont - 1,
376 }}
377 >
378 {currentOrg?.name || "Personal"}
379 </span>
380 </div>
381 </div>
382 </div>
383 </ListboxOptions>
384 </Transition>
385 </div>
386 </Listbox>
387 );
388}