| 1 | import { |
| 2 | BuildingOfficeIcon, |
| 3 | ChevronUpDownIcon, |
| 4 | } from "@heroicons/react/24/outline"; |
| 5 | import { UserCircleIcon } from "@heroicons/react/24/solid"; |
| 6 | import { useContext } from "react"; |
| 7 | import { useAuth } from "../../context/Auth"; |
| 8 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 9 | import { useAppDispatch, useAppSelector } from "../../redux/hooks"; |
| 10 | import { |
| 11 | selectCurrentOrg, |
| 12 | setSelectedOrgId, |
| 13 | } from "../../redux/slices/profilesSlice"; |
| 14 | import { |
| 15 | Listbox, |
| 16 | ListboxButton, |
| 17 | ListboxOption, |
| 18 | ListboxOptions, |
| 19 | } from "../ui/Listbox"; |
| 20 | |
| 21 | interface ScopeSelectProps { |
| 22 | onSelect?: () => void; |
| 23 | } |
| 24 | |
| 25 | function getOrgIcon(org: { name: string; iconUrl?: string | null }) { |
| 26 | if (org.iconUrl) { |
| 27 | return ( |
| 28 | <img src={org.iconUrl} alt="" className="h-3.5 w-3.5 rounded-full" /> |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | const IconComponent = |
| 33 | org.name === "Personal" ? UserCircleIcon : BuildingOfficeIcon; |
| 34 | return <IconComponent className="h-3.5 w-3.5" />; |
| 35 | } |
| 36 | |
| 37 | export function ScopeSelect({ onSelect }: ScopeSelectProps) { |
| 38 | const { organizations } = useAuth(); |
| 39 | const ideMessenger = useContext(IdeMessengerContext); |
| 40 | const selectedOrgId = useAppSelector( |
| 41 | (state) => state.profiles.selectedOrganizationId, |
| 42 | ); |
| 43 | const currentOrg = useAppSelector(selectCurrentOrg); |
| 44 | const dispatch = useAppDispatch(); |
| 45 | |
| 46 | const handleChange = (newValue: string) => { |
| 47 | // optimisitic update |
| 48 | dispatch(setSelectedOrgId(newValue)); |
| 49 | ideMessenger.post("didChangeSelectedOrg", { |
| 50 | id: newValue, |
| 51 | }); |
| 52 | onSelect?.(); |
| 53 | }; |
| 54 | |
| 55 | const selectedDisplay = currentOrg ?? { |
| 56 | name: "Personal", |
| 57 | iconUrl: null, |
| 58 | }; |
| 59 | |
| 60 | return ( |
| 61 | <Listbox value={selectedOrgId} onChange={handleChange}> |
| 62 | <div className="relative"> |
| 63 | <ListboxButton className="hover:bg-list-active hover:text-list-active-foreground w-full min-w-[140px] justify-between px-4 py-2 sm:min-w-[200px]"> |
| 64 | <div className="flex items-center gap-2"> |
| 65 | {getOrgIcon(selectedDisplay)} |
| 66 | <span className="truncate"> |
| 67 | {selectedDisplay?.name || "Select Organization"} |
| 68 | </span> |
| 69 | </div> |
| 70 | <ChevronUpDownIcon className="h-3.5 w-3.5" aria-hidden="true" /> |
| 71 | </ListboxButton> |
| 72 | |
| 73 | <ListboxOptions className="z-[1000] min-w-[140px] pt-0.5 sm:min-w-[200px]"> |
| 74 | {organizations.map((org) => ( |
| 75 | <ListboxOption key={org.id} value={org.id} className="py-2"> |
| 76 | <div className="flex items-center gap-2"> |
| 77 | {getOrgIcon(org)} |
| 78 | <span>{org.name}</span> |
| 79 | </div> |
| 80 | </ListboxOption> |
| 81 | ))} |
| 82 | </ListboxOptions> |
| 83 | </div> |
| 84 | </Listbox> |
| 85 | ); |
| 86 | } |