| 1 | import { CheckIcon } from "@heroicons/react/24/outline"; |
| 2 | import { |
| 3 | BuildingOfficeIcon, |
| 4 | UserCircleIcon, |
| 5 | UserIcon, |
| 6 | } from "@heroicons/react/24/solid"; |
| 7 | import { useContext } from "react"; |
| 8 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 9 | import { useAppDispatch, useAppSelector } from "../../redux/hooks"; |
| 10 | import { setSelectedOrgId } from "../../redux/slices/profilesSlice"; |
| 11 | import { ListboxOption } from "../ui"; |
| 12 | |
| 13 | interface OrganizationOptionProps { |
| 14 | organization: { id: string; name: string; iconUrl?: string | null }; |
| 15 | onClose: () => void; |
| 16 | } |
| 17 | |
| 18 | function getOrgIcon( |
| 19 | org: { name: string; iconUrl?: string | null }, |
| 20 | size: "sm" | "md" = "sm", |
| 21 | ) { |
| 22 | const sizeClasses = size === "sm" ? "h-3.5 w-3.5" : "xs:h-4 xs:w-4 h-3 w-3"; |
| 23 | |
| 24 | if (org.iconUrl) { |
| 25 | return ( |
| 26 | <img |
| 27 | src={org.iconUrl} |
| 28 | alt="" |
| 29 | className={`${sizeClasses} flex-shrink-0 rounded-full`} |
| 30 | /> |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | const IconComponent = |
| 35 | org.name === "Personal" |
| 36 | ? size === "md" |
| 37 | ? UserIcon |
| 38 | : UserCircleIcon |
| 39 | : BuildingOfficeIcon; |
| 40 | return <IconComponent className={`${sizeClasses} flex-shrink-0`} />; |
| 41 | } |
| 42 | |
| 43 | export function OrganizationOption({ |
| 44 | organization, |
| 45 | onClose, |
| 46 | }: OrganizationOptionProps) { |
| 47 | const dispatch = useAppDispatch(); |
| 48 | const ideMessenger = useContext(IdeMessengerContext); |
| 49 | const selectedOrgId = useAppSelector( |
| 50 | (state) => state.profiles.selectedOrganizationId, |
| 51 | ); |
| 52 | const isSelected = selectedOrgId === organization.id; |
| 53 | |
| 54 | function handleOptionClick() { |
| 55 | dispatch(setSelectedOrgId(organization.id)); |
| 56 | ideMessenger.post("didChangeSelectedOrg", { |
| 57 | id: organization.id, |
| 58 | }); |
| 59 | onClose(); |
| 60 | } |
| 61 | |
| 62 | return ( |
| 63 | <ListboxOption |
| 64 | value={organization.id} |
| 65 | onClick={handleOptionClick} |
| 66 | fontSizeModifier={-2} |
| 67 | className={isSelected ? "bg-list-active text-list-active-foreground" : ""} |
| 68 | > |
| 69 | <div className="flex w-full items-center justify-between gap-10 py-0.5"> |
| 70 | <div className="flex w-full items-center gap-3"> |
| 71 | <div className="flex h-4 w-4 flex-shrink-0 items-center justify-center"> |
| 72 | {getOrgIcon(organization, "sm")} |
| 73 | </div> |
| 74 | <span |
| 75 | className={`line-clamp-1 flex-1 ${isSelected ? "font-semibold" : ""}`} |
| 76 | > |
| 77 | {organization.name} |
| 78 | </span> |
| 79 | </div> |
| 80 | <div className="flex flex-row items-center gap-1.5"> |
| 81 | {isSelected && <CheckIcon className="h-3.5 w-3.5 flex-shrink-0" />} |
| 82 | </div> |
| 83 | </div> |
| 84 | </ListboxOption> |
| 85 | ); |
| 86 | } |