Elffuss BraidHub

kikocis/continue public

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