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