Elffuss BraidHub

kikocis/continue public

b334cb9dcd5e · 6.6 KiBRawPermalink
1import { ArrowPathIcon, PlusIcon } from "@heroicons/react/24/outline";
2import { AuthType, isOnPremSession } from "core/control-plane/AuthTypes";
3import { useContext, useEffect, useRef } from "react";
4import { useAuth } from "../../context/Auth";
5import { IdeMessengerContext } from "../../context/IdeMessenger";
6import { useAppDispatch, useAppSelector } from "../../redux/hooks";
7import {
8 selectCurrentOrg,
9 setSelectedProfile,
10} from "../../redux/slices/profilesSlice";
11import { getMetaKeyLabel, isMetaEquivalentKeyPressed } from "../../util";
12import { cn } from "../../util/cn";
13import { useLump } from "../mainInput/Lump/LumpContext";
14import {
15 Listbox,
16 ListboxOption,
17 ListboxOptions,
18 Transition,
19 useFontSize,
20} from "../ui";
21import { AssistantOptions } from "./AssistantOptions";
22import { ScopeSelect } from "./ScopeSelect";
23import { SelectedAssistantButton } from "./SelectedAssistantButton";
24
25export function AssistantAndOrgListbox() {
26 const dispatch = useAppDispatch();
27 const listboxRef = useRef<HTMLDivElement>(null);
28 const currentOrg = useAppSelector(selectCurrentOrg);
29 const ideMessenger = useContext(IdeMessengerContext);
30 const { isToolbarExpanded } = useLump();
31 const {
32 profiles,
33 selectedProfile,
34 session,
35 login,
36 logout,
37 organizations,
38 refreshProfiles,
39 } = useAuth();
40 const configLoading = useAppSelector((store) => store.config.loading);
41 const smallFont = useFontSize(-3);
42 const tinyFont = useFontSize(-4);
43 const shouldRenderOrgInfo =
44 session && organizations.length > 1 && !isOnPremSession(session);
45
46 function close() {
47 // Close the listbox by clicking outside or programmatically
48 const event = new KeyboardEvent("keydown", { key: "Escape" });
49 document.dispatchEvent(event);
50 }
51
52 function onNewAssistant() {
53 void ideMessenger.request("controlPlane/openUrl", {
54 path: "/new",
55 orgSlug: currentOrg?.slug,
56 });
57 close();
58 }
59
60 function onLogout() {
61 logout();
62 close();
63 }
64
65 useEffect(() => {
66 let lastToggleTime = 0;
67 const DEBOUNCE_MS = 800;
68
69 const handleKeyDown = (event: KeyboardEvent) => {
70 if (
71 event.key === "'" &&
72 isMetaEquivalentKeyPressed(event as any) &&
73 event.shiftKey
74 ) {
75 const now = Date.now();
76
77 if (now - lastToggleTime >= DEBOUNCE_MS) {
78 lastToggleTime = now;
79
80 const profileIds = profiles?.map((profile) => profile.id) ?? [];
81 // In case of 1 or 0 profiles just does nothing
82 if (profileIds.length < 2) {
83 return;
84 }
85 let nextId = profileIds[0];
86 if (selectedProfile) {
87 const curIndex = profileIds.indexOf(selectedProfile.id);
88 const nextIndex = (curIndex + 1) % profileIds.length;
89 nextId = profileIds[nextIndex];
90 }
91 // Optimistic update
92 dispatch(setSelectedProfile(nextId));
93 ideMessenger.post("didChangeSelectedProfile", {
94 id: nextId,
95 });
96 }
97 }
98 };
99
100 window.addEventListener("keydown", handleKeyDown);
101 return () => {
102 window.removeEventListener("keydown", handleKeyDown);
103 };
104 }, [currentOrg, selectedProfile]);
105
106 return (
107 <Listbox>
108 <div className="relative" ref={listboxRef}>
109 <SelectedAssistantButton selectedProfile={selectedProfile} />
110 <Transition>
111 <ListboxOptions className="-translate-x-1.5 pb-0">
112 <div className="border-border border-x-0 border-t-0 border-solid px-2 py-3">
113 <div className="flex flex-col gap-2 pb-1 pl-1">
114 {session && session?.AUTH_TYPE !== AuthType.OnPrem && (
115 <span className="text-description-muted flex items-center pb-1">
116 {session?.account.id}
117 </span>
118 )}
119 {shouldRenderOrgInfo && (
120 <>
121 <label className="text-vsc-foreground font-semibold">
122 Organization
123 </label>
124 <ScopeSelect />
125 </>
126 )}
127 </div>
128 </div>
129
130 <AssistantOptions
131 selectedProfileId={selectedProfile?.id}
132 onClose={close}
133 />
134
135 {/* Bottom Actions */}
136 <div className="border-border border-x-0 border-b-0 border-t border-solid">
137 <ListboxOption
138 value="new-assistant"
139 fontSizeModifier={-2}
140 className="border-border border-b px-2 py-1.5"
141 onClick={session ? onNewAssistant : () => login(false)}
142 >
143 <span
144 className="text-description flex flex-row items-center"
145 style={{ fontSize: tinyFont }}
146 >
147 <PlusIcon className="mr-1 h-3 w-3" /> New Assistant
148 </span>
149 </ListboxOption>
150
151 <ListboxOption
152 value="reload-assistant"
153 fontSizeModifier={-2}
154 className="border-border border-b px-2 py-1.5"
155 onClick={() =>
156 refreshProfiles("Manual refresh from assistant list")
157 }
158 >
159 <span
160 className="text-description flex flex-row items-center"
161 style={{ fontSize: tinyFont }}
162 >
163 <ArrowPathIcon
164 className={cn(
165 "mr-1 h-3 w-3",
166 configLoading && "animate-spin-slow",
167 )}
168 />
169 Reload assistants
170 </span>
171 </ListboxOption>
172
173 {session && (
174 <ListboxOption
175 value="log-out"
176 fontSizeModifier={-2}
177 className="border-border border-b px-3 py-1.5"
178 onClick={onLogout}
179 >
180 <div
181 className="text-description flex flex-row items-center"
182 style={{ fontSize: tinyFont }}
183 >
184 Log out
185 </div>
186 </ListboxOption>
187 )}
188
189 <div
190 className="text-description border-border flex items-center justify-between gap-1.5 border-x-0 border-b-0 border-t border-solid px-2 py-2"
191 style={{ fontSize: tinyFont }}
192 >
193 <span className="block" style={{ fontSize: tinyFont - 1 }}>
194 <code>{getMetaKeyLabel()} ⇧ '</code> to toggle assistant
195 </span>
196 </div>
197 </div>
198 </ListboxOptions>
199 </Transition>
200 </div>
201 </Listbox>
202 );
203}