Elffuss BraidHub

kikocis/continue public

99fe51849b68 · 6.6 KiBRawPermalink
1import { ArrowPathIcon, PlusIcon } from "@heroicons/react/24/outline";
2import { 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 { AccountOption } from "./AccountOption";
22import { AssistantOptions } from "./AssistantOptions";
23import { ScopeSelect } from "./ScopeSelect";
24import { SelectedAssistantButton } from "./SelectedAssistantButton";
25
26export function AssistantAndOrgListbox() {
27 const dispatch = useAppDispatch();
28 const listboxRef = useRef<HTMLDivElement>(null);
29 const currentOrg = useAppSelector(selectCurrentOrg);
30 const ideMessenger = useContext(IdeMessengerContext);
31 const { isToolbarExpanded } = useLump();
32 const {
33 profiles,
34 selectedProfile,
35 session,
36 login,
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 useEffect(() => {
61 let lastToggleTime = 0;
62 const DEBOUNCE_MS = 800;
63
64 const handleKeyDown = (event: KeyboardEvent) => {
65 if (
66 event.key === "'" &&
67 isMetaEquivalentKeyPressed(event as any) &&
68 event.shiftKey
69 ) {
70 const now = Date.now();
71
72 if (now - lastToggleTime >= DEBOUNCE_MS) {
73 lastToggleTime = now;
74
75 const profileIds = profiles?.map((profile) => profile.id) ?? [];
76 // In case of 1 or 0 profiles just does nothing
77 if (profileIds.length < 2) {
78 return;
79 }
80 let nextId = profileIds[0];
81 if (selectedProfile) {
82 const curIndex = profileIds.indexOf(selectedProfile.id);
83 const nextIndex = (curIndex + 1) % profileIds.length;
84 nextId = profileIds[nextIndex];
85 }
86 // Optimistic update
87 dispatch(setSelectedProfile(nextId));
88 ideMessenger.post("didChangeSelectedProfile", {
89 id: nextId,
90 });
91 }
92 }
93 };
94
95 window.addEventListener("keydown", handleKeyDown);
96 return () => {
97 window.removeEventListener("keydown", handleKeyDown);
98 };
99 }, [currentOrg, selectedProfile]);
100
101 if (!selectedProfile) {
102 return (
103 <div
104 onClick={() => {
105 void ideMessenger.request("controlPlane/openUrl", {
106 path: "/new?type=assistant",
107 orgSlug: currentOrg?.slug,
108 });
109 }}
110 className="text-description flex cursor-pointer select-none items-center gap-1"
111 style={{ fontSize: smallFont }}
112 >
113 <PlusIcon className="h-3 w-3 flex-shrink-0 select-none" />
114 <span
115 className={`line-clamp-1 select-none break-all ${isToolbarExpanded ? "xs:hidden sm:line-clamp-1" : ""}`}
116 >
117 Create your first assistant
118 </span>
119 </div>
120 );
121 }
122
123 return (
124 <Listbox>
125 <div className="relative" ref={listboxRef}>
126 <SelectedAssistantButton selectedProfile={selectedProfile} />
127 <Transition>
128 <ListboxOptions className="-translate-x-1.5 pb-0">
129 {shouldRenderOrgInfo && (
130 <div className="border-border border-x-0 border-t-0 border-solid px-2 py-3">
131 <div className="flex flex-col gap-2 pb-1 pl-1">
132 <span className="text-description-muted flex items-center pb-1">
133 {session.account.id}
134 </span>
135 <label className="text-vsc-foreground font-semibold">
136 Organization
137 </label>
138 <ScopeSelect />
139 </div>
140 </div>
141 )}
142 <AssistantOptions
143 selectedProfileId={selectedProfile.id}
144 onClose={close}
145 />
146
147 {/* Bottom Actions */}
148 <div className="border-border border-x-0 border-b-0 border-t border-solid">
149 <ListboxOption
150 value="new-assistant"
151 fontSizeModifier={-2}
152 className="border-border border-b px-2 py-1.5"
153 onClick={session ? onNewAssistant : () => login(false)}
154 >
155 <span
156 className="text-description flex flex-row items-center"
157 style={{ fontSize: tinyFont }}
158 >
159 <PlusIcon className="mr-1 h-3 w-3" /> New Assistant
160 </span>
161 </ListboxOption>
162
163 <ListboxOption
164 value="reload-assistant"
165 fontSizeModifier={-2}
166 className="border-border border-b px-2 py-1.5"
167 onClick={() => refreshProfiles()}
168 >
169 <span
170 className="text-description flex flex-row items-center"
171 style={{ fontSize: tinyFont }}
172 >
173 <ArrowPathIcon
174 className={cn(
175 "mr-1 h-3 w-3",
176 configLoading && "animate-spin-slow",
177 )}
178 />
179 Reload config
180 </span>
181 </ListboxOption>
182
183 <AccountOption onClose={close} />
184
185 <div
186 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"
187 style={{ fontSize: tinyFont }}
188 >
189 <span className="block" style={{ fontSize: tinyFont - 1 }}>
190 <code>{getMetaKeyLabel()} ⇧ '</code> to toggle assistant
191 </span>
192 </div>
193 </div>
194 </ListboxOptions>
195 </Transition>
196 </div>
197 </Listbox>
198 );
199}