| 1 | import { |
| 2 | ArrowPathIcon, |
| 3 | ArrowTopRightOnSquareIcon, |
| 4 | BuildingOfficeIcon, |
| 5 | ChevronDownIcon, |
| 6 | Cog6ToothIcon, |
| 7 | ExclamationTriangleIcon, |
| 8 | PlusIcon, |
| 9 | } from "@heroicons/react/24/outline"; |
| 10 | import { useContext, useEffect, useMemo, useRef, useState } from "react"; |
| 11 | import { useAuth } from "../../../context/Auth"; |
| 12 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 13 | import { |
| 14 | selectCurrentOrg, |
| 15 | setSelectedOrgId, |
| 16 | setSelectedProfile, |
| 17 | } from "../../../redux"; |
| 18 | import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; |
| 19 | import { |
| 20 | fontSize, |
| 21 | getMetaKeyLabel, |
| 22 | isLocalProfile, |
| 23 | isMetaEquivalentKeyPressed, |
| 24 | } from "../../../util"; |
| 25 | import { |
| 26 | Listbox, |
| 27 | ListboxButton, |
| 28 | ListboxOption, |
| 29 | ListboxOptions, |
| 30 | Transition, |
| 31 | } from "../../ui"; |
| 32 | |
| 33 | import type { ProfileDescription } from "core/config/ConfigHandler"; |
| 34 | import { useNavigate } from "react-router-dom"; |
| 35 | import { vscCommandCenterInactiveBorder } from "../.."; |
| 36 | import { cn } from "../../../util/cn"; |
| 37 | import { ROUTES } from "../../../util/navigation"; |
| 38 | import { useLump } from "../../mainInput/Lump/LumpContext"; |
| 39 | import { useFontSize } from "../../ui/font"; |
| 40 | import AssistantIcon from "./AssistantIcon"; |
| 41 | interface AssistantSelectOptionProps { |
| 42 | profile: ProfileDescription; |
| 43 | selected: boolean; |
| 44 | onClick: () => void; |
| 45 | } |
| 46 | |
| 47 | const AssistantSelectOption = ({ |
| 48 | profile, |
| 49 | selected, |
| 50 | onClick, |
| 51 | }: AssistantSelectOptionProps) => { |
| 52 | const tinyFont = useFontSize(-4); |
| 53 | |
| 54 | const navigate = useNavigate(); |
| 55 | const { setSelectedSection } = useLump(); |
| 56 | |
| 57 | const hasFatalErrors = useMemo(() => { |
| 58 | return !!profile.errors?.find((error) => error.fatal); |
| 59 | }, [profile.errors]); |
| 60 | |
| 61 | const dispatch = useAppDispatch(); |
| 62 | const ideMessenger = useContext(IdeMessengerContext); |
| 63 | |
| 64 | function handleOptionClick() { |
| 65 | // optimistic update |
| 66 | dispatch(setSelectedProfile(profile.id)); |
| 67 | // notify core which will handle actual update |
| 68 | ideMessenger.post("didChangeSelectedProfile", { |
| 69 | id: profile.id, |
| 70 | }); |
| 71 | onClick(); |
| 72 | } |
| 73 | |
| 74 | function handleConfigure() { |
| 75 | ideMessenger.post("config/openProfile", { profileId: profile.id }); |
| 76 | onClick(); |
| 77 | } |
| 78 | |
| 79 | function handleClickError() { |
| 80 | if (profile.id === "local") { |
| 81 | navigate(ROUTES.HOME); |
| 82 | setSelectedSection("error"); |
| 83 | } else { |
| 84 | ideMessenger.post("config/openProfile", { profileId: profile.id }); |
| 85 | } |
| 86 | onClick(); |
| 87 | } |
| 88 | |
| 89 | return ( |
| 90 | <ListboxOption |
| 91 | value={profile.id} |
| 92 | disabled={hasFatalErrors} |
| 93 | onClick={!hasFatalErrors ? handleOptionClick : undefined} |
| 94 | fontSizeModifier={-2} |
| 95 | className={selected ? "bg-list-active text-list-active-foreground" : ""} |
| 96 | > |
| 97 | <div |
| 98 | className="flex w-full flex-col gap-0.5" |
| 99 | style={{ |
| 100 | fontSize: tinyFont, |
| 101 | }} |
| 102 | > |
| 103 | <div className="flex w-full items-center justify-between gap-2 bg-transparent"> |
| 104 | <div className="flex w-full items-center gap-1"> |
| 105 | <div className="flex h-4 w-4 flex-shrink-0"> |
| 106 | <AssistantIcon size={3.5} assistant={profile} /> |
| 107 | </div> |
| 108 | <span |
| 109 | className={`line-clamp-1 flex-1 ${selected ? "font-semibold" : ""}`} |
| 110 | > |
| 111 | {profile.title} |
| 112 | </span> |
| 113 | </div> |
| 114 | <div className="flex flex-row items-center gap-1"> |
| 115 | {profile.errors && profile.errors?.length > 0 && ( |
| 116 | <ExclamationTriangleIcon |
| 117 | data-tooltip-id={`${profile.id}-errors-tooltip`} |
| 118 | className="h-3 w-3 flex-shrink-0 cursor-pointer text-red-500" |
| 119 | onClick={(e) => { |
| 120 | e.preventDefault(); |
| 121 | e.stopPropagation(); |
| 122 | handleClickError(); |
| 123 | }} |
| 124 | /> |
| 125 | )} |
| 126 | {isLocalProfile(profile) ? ( |
| 127 | <Cog6ToothIcon |
| 128 | className="text-description h-3 w-3 flex-shrink-0 cursor-pointer" |
| 129 | onClick={(e) => { |
| 130 | e.stopPropagation(); |
| 131 | e.preventDefault(); |
| 132 | handleConfigure(); |
| 133 | }} |
| 134 | /> |
| 135 | ) : ( |
| 136 | <ArrowTopRightOnSquareIcon |
| 137 | className="text-description h-3 w-3 flex-shrink-0 cursor-pointer" |
| 138 | onClick={(e) => { |
| 139 | e.stopPropagation(); |
| 140 | e.preventDefault(); |
| 141 | handleConfigure(); |
| 142 | }} |
| 143 | /> |
| 144 | )} |
| 145 | </div> |
| 146 | </div> |
| 147 | </div> |
| 148 | </ListboxOption> |
| 149 | ); |
| 150 | }; |
| 151 | |
| 152 | export default function AssistantSelect() { |
| 153 | const dispatch = useAppDispatch(); |
| 154 | const buttonRef = useRef<HTMLButtonElement>(null); |
| 155 | const currentOrg = useAppSelector(selectCurrentOrg); |
| 156 | const orgs = useAppSelector((store) => store.profiles.organizations); |
| 157 | const ideMessenger = useContext(IdeMessengerContext); |
| 158 | const { isToolbarExpanded } = useLump(); |
| 159 | const [loading, setLoading] = useState(false); |
| 160 | const { profiles, session, login, selectedProfile, refreshProfiles } = |
| 161 | useAuth(); |
| 162 | |
| 163 | function close() { |
| 164 | if (buttonRef.current) { |
| 165 | buttonRef.current.click(); |
| 166 | } |
| 167 | } |
| 168 | function onNewAssistant() { |
| 169 | ideMessenger.post("controlPlane/openUrl", { |
| 170 | path: "new", |
| 171 | orgSlug: currentOrg?.slug, |
| 172 | }); |
| 173 | close(); |
| 174 | } |
| 175 | |
| 176 | useEffect(() => { |
| 177 | let lastToggleTime = 0; |
| 178 | const DEBOUNCE_MS = 800; |
| 179 | |
| 180 | const handleKeyDown = (event: KeyboardEvent) => { |
| 181 | if ( |
| 182 | event.key === "'" && |
| 183 | isMetaEquivalentKeyPressed(event as any) && |
| 184 | event.shiftKey |
| 185 | ) { |
| 186 | const now = Date.now(); |
| 187 | |
| 188 | if (now - lastToggleTime >= DEBOUNCE_MS) { |
| 189 | lastToggleTime = now; |
| 190 | |
| 191 | const profileIds = profiles?.map((profile) => profile.id) ?? []; |
| 192 | // In case of 1 or 0 profiles just does nothing |
| 193 | if (profileIds.length < 2) { |
| 194 | return; |
| 195 | } |
| 196 | let nextId = profileIds[0]; |
| 197 | if (selectedProfile) { |
| 198 | const curIndex = profileIds.indexOf(selectedProfile.id); |
| 199 | const nextIndex = (curIndex + 1) % profileIds.length; |
| 200 | nextId = profileIds[nextIndex]; |
| 201 | } |
| 202 | // Optimistic update |
| 203 | dispatch(setSelectedProfile(nextId)); |
| 204 | ideMessenger.post("didChangeSelectedProfile", { |
| 205 | id: nextId, |
| 206 | }); |
| 207 | } |
| 208 | } |
| 209 | }; |
| 210 | |
| 211 | window.addEventListener("keydown", handleKeyDown); |
| 212 | return () => { |
| 213 | window.removeEventListener("keydown", handleKeyDown); |
| 214 | }; |
| 215 | }, [currentOrg, selectedProfile]); |
| 216 | |
| 217 | const cycleOrgs = () => { |
| 218 | if (!session) { |
| 219 | void login(false); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | const orgIds = orgs.map((org) => org.id); |
| 224 | if (orgIds.length < 2) { |
| 225 | return; |
| 226 | } |
| 227 | let nextId = orgIds[0]; |
| 228 | if (currentOrg) { |
| 229 | const curIndex = orgIds.indexOf(currentOrg.id); |
| 230 | const nextIndex = (curIndex + 1) % orgIds.length; |
| 231 | nextId = orgIds[nextIndex]; |
| 232 | } |
| 233 | // Optimistic update |
| 234 | dispatch(setSelectedOrgId(nextId)); |
| 235 | ideMessenger.post("didChangeSelectedOrg", { |
| 236 | id: nextId, |
| 237 | }); |
| 238 | }; |
| 239 | |
| 240 | const tinyFont = useFontSize(-4); |
| 241 | const smallFont = useFontSize(-3); |
| 242 | |
| 243 | if (!selectedProfile) { |
| 244 | return ( |
| 245 | <div |
| 246 | onClick={() => { |
| 247 | void ideMessenger.request("controlPlane/openUrl", { |
| 248 | path: "/new?type=assistant", |
| 249 | orgSlug: currentOrg?.slug, |
| 250 | }); |
| 251 | }} |
| 252 | className="text-description flex cursor-pointer select-none items-center gap-1" |
| 253 | style={{ fontSize: smallFont }} |
| 254 | > |
| 255 | <PlusIcon className="h-3 w-3 flex-shrink-0 select-none" /> |
| 256 | <span |
| 257 | className={`line-clamp-1 select-none break-all ${isToolbarExpanded ? "xs:hidden sm:line-clamp-1" : ""}`} |
| 258 | > |
| 259 | Create your first assistant |
| 260 | </span> |
| 261 | </div> |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | return ( |
| 266 | <Listbox> |
| 267 | <div className="relative"> |
| 268 | <ListboxButton |
| 269 | data-testid="assistant-select-button" |
| 270 | ref={buttonRef} |
| 271 | className="text-description border-none bg-transparent hover:brightness-125" |
| 272 | style={{ fontSize: fontSize(-3) }} |
| 273 | > |
| 274 | <div className="flex flex-row items-center gap-1.5"> |
| 275 | <div className="h-3 w-3 flex-shrink-0 select-none"> |
| 276 | <AssistantIcon size={3} assistant={selectedProfile} /> |
| 277 | </div> |
| 278 | <span |
| 279 | className={`line-clamp-1 select-none break-all ${isToolbarExpanded ? "xs:hidden sm:line-clamp-1" : ""}`} |
| 280 | > |
| 281 | {selectedProfile.title} |
| 282 | </span> |
| 283 | </div> |
| 284 | <ChevronDownIcon |
| 285 | className="h-2 w-2 flex-shrink-0 select-none" |
| 286 | aria-hidden="true" |
| 287 | /> |
| 288 | </ListboxButton> |
| 289 | |
| 290 | <Transition> |
| 291 | <ListboxOptions className="pb-0"> |
| 292 | <div className="flex gap-1.5 px-2.5 py-1"> |
| 293 | <span>Assistants</span> |
| 294 | <div |
| 295 | className="flex cursor-pointer flex-row items-center gap-1 hover:brightness-125" |
| 296 | onClick={async (e) => { |
| 297 | e.stopPropagation(); |
| 298 | setLoading(true); |
| 299 | await refreshProfiles(); |
| 300 | setLoading(false); |
| 301 | buttonRef.current?.click(); |
| 302 | }} |
| 303 | > |
| 304 | <ArrowPathIcon |
| 305 | className={cn( |
| 306 | "text-description h-2.5 w-2.5", |
| 307 | loading && "animate-spin-slow", |
| 308 | )} |
| 309 | /> |
| 310 | </div> |
| 311 | </div> |
| 312 | |
| 313 | <div |
| 314 | className={`thin-scrollbar flex max-h-[300px] flex-col overflow-y-auto`} |
| 315 | > |
| 316 | {profiles?.map((profile, idx) => { |
| 317 | return ( |
| 318 | <AssistantSelectOption |
| 319 | key={idx} |
| 320 | profile={profile} |
| 321 | onClick={close} |
| 322 | selected={profile.id === selectedProfile.id} |
| 323 | /> |
| 324 | ); |
| 325 | })} |
| 326 | </div> |
| 327 | |
| 328 | <div className="flex flex-col"> |
| 329 | <div |
| 330 | className="my-0 h-[0.5px]" |
| 331 | style={{ |
| 332 | backgroundColor: vscCommandCenterInactiveBorder, |
| 333 | }} |
| 334 | /> |
| 335 | |
| 336 | <div className="flex flex-row items-center"> |
| 337 | <ListboxOption |
| 338 | className="w-full" |
| 339 | value={"new-assistant"} |
| 340 | fontSizeModifier={-2} |
| 341 | onClick={session ? onNewAssistant : () => login(false)} |
| 342 | > |
| 343 | <div |
| 344 | className="text-description flex flex-row items-center gap-2" |
| 345 | style={{ |
| 346 | fontSize: tinyFont, |
| 347 | }} |
| 348 | > |
| 349 | <PlusIcon className="ml-0.5 h-3 w-3 flex-shrink-0" /> |
| 350 | New Assistant |
| 351 | </div> |
| 352 | </ListboxOption> |
| 353 | </div> |
| 354 | |
| 355 | <div |
| 356 | className="my-0 h-[0.5px]" |
| 357 | style={{ |
| 358 | backgroundColor: vscCommandCenterInactiveBorder, |
| 359 | }} |
| 360 | /> |
| 361 | |
| 362 | <div |
| 363 | className="text-description flex items-center justify-between gap-1.5 px-2 py-1" |
| 364 | style={{ |
| 365 | fontSize: tinyFont, |
| 366 | }} |
| 367 | > |
| 368 | <span |
| 369 | className="block" |
| 370 | style={{ |
| 371 | fontSize: tinyFont - 1, |
| 372 | }} |
| 373 | > |
| 374 | <code>{getMetaKeyLabel()} ⇧ '</code> to toggle |
| 375 | </span> |
| 376 | <div |
| 377 | className="ml-auto flex items-center gap-1" |
| 378 | onClick={cycleOrgs} |
| 379 | > |
| 380 | {currentOrg?.iconUrl ? ( |
| 381 | <img |
| 382 | src={currentOrg.iconUrl} |
| 383 | className="h-2.5 w-2.5 rounded-full" |
| 384 | /> |
| 385 | ) : ( |
| 386 | <BuildingOfficeIcon className="h-4 w-4" /> |
| 387 | )} |
| 388 | <span |
| 389 | className="hover:cursor-pointer hover:underline" |
| 390 | style={{ |
| 391 | fontSize: tinyFont - 1, |
| 392 | }} |
| 393 | > |
| 394 | {currentOrg?.name || "Personal"} |
| 395 | </span> |
| 396 | </div> |
| 397 | </div> |
| 398 | </div> |
| 399 | </ListboxOptions> |
| 400 | </Transition> |
| 401 | </div> |
| 402 | </Listbox> |
| 403 | ); |
| 404 | } |