Elffuss BraidHub

kikocis/continue public

87f75892af56 · 10.9 KiBRawPermalink
1import {
2 ArrowPathIcon,
3 ArrowRightStartOnRectangleIcon,
4 Cog6ToothIcon,
5 PencilIcon,
6 PlusIcon,
7 WrenchScrewdriverIcon,
8} from "@heroicons/react/24/outline";
9import { isOnPremSession } from "core/control-plane/AuthTypes";
10import { useContext, useEffect, useRef } from "react";
11import { useNavigate } from "react-router-dom";
12import { useAuth } from "../../context/Auth";
13import { IdeMessengerContext } from "../../context/IdeMessenger";
14import { useAppDispatch, useAppSelector } from "../../redux/hooks";
15import {
16 selectCurrentOrg,
17 setSelectedProfile,
18} from "../../redux/slices/profilesSlice";
19import { getMetaKeyLabel, isMetaEquivalentKeyPressed } from "../../util";
20import { cn } from "../../util/cn";
21import { CONFIG_ROUTES } from "../../util/navigation";
22import {
23 Button,
24 Listbox,
25 ListboxOptions,
26 Transition,
27 useFontSize,
28} from "../ui";
29import { Divider } from "../ui/Divider";
30import { AssistantOptions } from "./AssistantOptions";
31import { OrganizationOptions } from "./OrganizationOptions";
32import { SelectedAssistantButton } from "./SelectedAssistantButton";
33
34export interface AssistantAndOrgListboxProps {
35 variant: "lump" | "sidebar";
36}
37
38export function AssistantAndOrgListbox({
39 variant = "sidebar",
40}: AssistantAndOrgListboxProps) {
41 const dispatch = useAppDispatch();
42 const navigate = useNavigate();
43 const listboxRef = useRef<HTMLDivElement>(null);
44 const currentOrg = useAppSelector(selectCurrentOrg);
45 const ideMessenger = useContext(IdeMessengerContext);
46 const {
47 profiles,
48 selectedProfile,
49 session,
50 logout,
51 login,
52 organizations,
53 refreshProfiles,
54 } = useAuth();
55 const configLoading = useAppSelector((store) => store.config.loading);
56 const tinyFont = useFontSize(-4);
57 const shouldRenderOrgInfo =
58 session && organizations.length > 1 && !isOnPremSession(session);
59
60 function close() {
61 // Close the listbox by clicking outside or programmatically
62 const event = new KeyboardEvent("keydown", { key: "Escape" });
63 document.dispatchEvent(event);
64 }
65
66 function onNewAssistant() {
67 if (session) {
68 void ideMessenger.request("controlPlane/openUrl", {
69 path: "/new",
70 orgSlug: currentOrg?.slug,
71 });
72 } else {
73 void ideMessenger.request("config/newAssistantFile", undefined);
74 }
75 close();
76 }
77
78 function onNewOrganization() {
79 void ideMessenger.request("controlPlane/openUrl", {
80 path: "/organizations/new",
81 });
82 close();
83 }
84
85 function onAgentsConfig() {
86 navigate(CONFIG_ROUTES.AGENTS);
87 close();
88 }
89
90 function onOrganizationsConfig() {
91 navigate(CONFIG_ROUTES.ORGANIZATIONS);
92 close();
93 }
94
95 function onRulesConfig() {
96 navigate(CONFIG_ROUTES.RULES);
97 close();
98 }
99
100 function onToolsConfig() {
101 navigate(CONFIG_ROUTES.TOOLS);
102 close();
103 }
104
105 useEffect(() => {
106 let lastToggleTime = 0;
107 const DEBOUNCE_MS = 800;
108
109 const handleKeyDown = (event: KeyboardEvent) => {
110 if (
111 event.key === "'" &&
112 isMetaEquivalentKeyPressed(event as any) &&
113 event.shiftKey
114 ) {
115 const now = Date.now();
116
117 if (now - lastToggleTime >= DEBOUNCE_MS) {
118 lastToggleTime = now;
119
120 const profileIds = profiles?.map((profile) => profile.id) ?? [];
121 // In case of 1 or 0 profiles just does nothing
122 if (profileIds.length < 2) {
123 return;
124 }
125 let nextId = profileIds[0];
126 if (selectedProfile) {
127 const curIndex = profileIds.indexOf(selectedProfile.id);
128 const nextIndex = (curIndex + 1) % profileIds.length;
129 nextId = profileIds[nextIndex];
130 }
131 // Optimistic update
132 dispatch(setSelectedProfile(nextId));
133 ideMessenger.post("didChangeSelectedProfile", {
134 id: nextId,
135 });
136 }
137 }
138 };
139
140 window.addEventListener("keydown", handleKeyDown);
141 return () => {
142 window.removeEventListener("keydown", handleKeyDown);
143 };
144 }, [currentOrg, selectedProfile]);
145
146 return (
147 <Listbox>
148 <div className="relative" ref={listboxRef}>
149 <SelectedAssistantButton
150 selectedProfile={selectedProfile}
151 variant={variant}
152 />
153 <Transition>
154 <ListboxOptions
155 className="max-h-32 -translate-x-1.5 overflow-y-auto pb-0"
156 style={{ zIndex: 200 }}
157 >
158 <div className="flex items-center justify-between p-2">
159 <span className="text-description text-xs font-medium">
160 Agents
161 </span>
162 <div className="flex items-center gap-0.5">
163 <Button
164 onClick={(e) => {
165 e.stopPropagation();
166 onNewAssistant();
167 }}
168 variant="ghost"
169 size="sm"
170 className="my-0 h-5 w-5 p-0"
171 >
172 <PlusIcon className="text-description h-3.5 w-3.5" />
173 </Button>
174 <Button
175 onClick={(e) => {
176 e.stopPropagation();
177 onAgentsConfig();
178 }}
179 variant="ghost"
180 size="sm"
181 className="my-0 h-5 w-5 p-0"
182 >
183 <Cog6ToothIcon className="text-description h-3.5 w-3.5" />
184 </Button>
185 </div>
186 </div>
187
188 <AssistantOptions
189 selectedProfileId={selectedProfile?.id}
190 onClose={close}
191 />
192
193 {shouldRenderOrgInfo && (
194 <>
195 <Divider className="!mb-0.5 !mt-0" />
196 <div className="flex items-center justify-between p-2">
197 <span className="text-description text-xs font-medium">
198 Organizations
199 </span>
200 <div className="flex items-center gap-0.5">
201 <Button
202 onClick={(e) => {
203 e.stopPropagation();
204 onNewOrganization();
205 }}
206 variant="ghost"
207 size="sm"
208 className="my-0 h-5 w-5 p-0"
209 >
210 <PlusIcon className="text-description h-3.5 w-3.5" />
211 </Button>
212 <Button
213 onClick={(e) => {
214 e.stopPropagation();
215 onOrganizationsConfig();
216 }}
217 variant="ghost"
218 size="sm"
219 className="my-0 h-5 w-5 p-0"
220 >
221 <Cog6ToothIcon className="text-description h-3.5 w-3.5" />
222 </Button>
223 </div>
224 </div>
225
226 <OrganizationOptions onClose={close} />
227
228 <Divider className="!mb-0 mt-0.5" />
229 </>
230 )}
231
232 {/* Settings Section */}
233 {variant !== "sidebar" && (
234 <div>
235 <Button
236 onClick={(e) => {
237 e.stopPropagation();
238 onRulesConfig();
239 }}
240 variant="ghost"
241 size="sm"
242 className="text-description hover:bg-input my-0 w-full justify-start py-1.5 pl-1 text-left"
243 >
244 <div className="flex w-full items-center">
245 <PencilIcon className="ml-1.5 mr-2 h-3.5 w-3.5 flex-shrink-0" />
246 <span className="text-2xs">Rules</span>
247 </div>
248 </Button>
249 <Button
250 onClick={(e) => {
251 e.stopPropagation();
252 onToolsConfig();
253 }}
254 variant="ghost"
255 size="sm"
256 className="text-description hover:bg-input my-0 w-full justify-start py-1.5 pl-1 text-left"
257 >
258 <div className="flex w-full items-center">
259 <WrenchScrewdriverIcon className="ml-1.5 mr-2 h-3.5 w-3.5 flex-shrink-0" />
260 <span className="text-2xs">Tools</span>
261 </div>
262 </Button>
263 <Button
264 onClick={(e) => {
265 e.stopPropagation();
266 void refreshProfiles("Manual refresh from assistant list");
267 }}
268 variant="ghost"
269 size="sm"
270 className="text-description hover:bg-input my-0 w-full justify-start py-1.5 pl-1 text-left"
271 >
272 <div className="flex w-full items-center">
273 <ArrowPathIcon
274 className={cn(
275 "ml-1.5 mr-2 h-3.5 w-3.5 flex-shrink-0",
276 configLoading && "animate-spin-slow",
277 )}
278 />
279 <span className="text-2xs">Reload</span>
280 </div>
281 </Button>
282 {session ? (
283 <Button
284 onClick={(e) => {
285 e.stopPropagation();
286 logout();
287 close();
288 }}
289 variant="ghost"
290 size="sm"
291 className="text-description hover:bg-input my-0 w-full justify-start py-1.5 pl-1 text-left"
292 >
293 <div className="flex w-full items-center">
294 <ArrowRightStartOnRectangleIcon className="ml-1.5 mr-2 h-3.5 w-3.5 flex-shrink-0" />
295 <span className="text-2xs">Log out</span>
296 </div>
297 </Button>
298 ) : (
299 <Button
300 onClick={(e) => {
301 e.stopPropagation();
302 login(false);
303 close();
304 }}
305 variant="ghost"
306 size="sm"
307 className="text-description hover:bg-input my-0 w-full justify-start py-1.5 pl-1 text-left"
308 >
309 <div className="flex w-full items-center">
310 <ArrowRightStartOnRectangleIcon className="ml-1.5 mr-2 h-3.5 w-3.5 flex-shrink-0 rotate-180" />
311 <span className="text-2xs">Log in</span>
312 </div>
313 </Button>
314 )}
315
316 <Divider className="!mt-0" />
317 </div>
318 )}
319
320 {/* Bottom Actions */}
321 <div>
322 <div className="text-description flex items-center justify-start px-2 py-1">
323 <span className="block" style={{ fontSize: tinyFont }}>
324 <code>{getMetaKeyLabel()} ⇧ '</code> to toggle agent
325 </span>
326 </div>
327 </div>
328 </ListboxOptions>
329 </Transition>
330 </div>
331 </Listbox>
332 );
333}