| 1 | import { Cog6ToothIcon } 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 { useNavigate } from "react-router-dom"; |
| 9 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 10 | import { useAppDispatch, useAppSelector } from "../../redux/hooks"; |
| 11 | import { setSelectedOrgId } from "../../redux/slices/profilesSlice"; |
| 12 | import { CONFIG_ROUTES } from "../../util/navigation"; |
| 13 | import { Button, ListboxOption } from "../ui"; |
| 14 | |
| 15 | interface OrganizationOptionProps { |
| 16 | organization: { id: string; name: string; iconUrl?: string | null }; |
| 17 | onClose: () => void; |
| 18 | } |
| 19 | |
| 20 | function getOrgIcon( |
| 21 | org: { name: string; iconUrl?: string | null }, |
| 22 | size: "sm" | "md" = "sm", |
| 23 | ) { |
| 24 | const sizeClasses = size === "sm" ? "h-3.5 w-3.5" : "xs:h-4 xs:w-4 h-3 w-3"; |
| 25 | |
| 26 | if (org.iconUrl) { |
| 27 | return ( |
| 28 | <img |
| 29 | src={org.iconUrl} |
| 30 | alt="" |
| 31 | className={`${sizeClasses} flex-shrink-0 rounded-full`} |
| 32 | /> |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | const IconComponent = |
| 37 | org.name === "Personal" |
| 38 | ? size === "md" |
| 39 | ? UserIcon |
| 40 | : UserCircleIcon |
| 41 | : BuildingOfficeIcon; |
| 42 | return <IconComponent className={`${sizeClasses} flex-shrink-0`} />; |
| 43 | } |
| 44 | |
| 45 | export function OrganizationOption({ |
| 46 | organization, |
| 47 | onClose, |
| 48 | }: OrganizationOptionProps) { |
| 49 | const dispatch = useAppDispatch(); |
| 50 | const navigate = useNavigate(); |
| 51 | const ideMessenger = useContext(IdeMessengerContext); |
| 52 | const selectedOrgId = useAppSelector( |
| 53 | (state) => state.profiles.selectedOrganizationId, |
| 54 | ); |
| 55 | const isSelected = selectedOrgId === organization.id; |
| 56 | |
| 57 | function handleOptionClick() { |
| 58 | dispatch(setSelectedOrgId(organization.id)); |
| 59 | ideMessenger.post("didChangeSelectedOrg", { |
| 60 | id: organization.id, |
| 61 | }); |
| 62 | onClose(); |
| 63 | } |
| 64 | |
| 65 | return ( |
| 66 | <ListboxOption |
| 67 | value={organization.id} |
| 68 | onClick={handleOptionClick} |
| 69 | fontSizeModifier={-2} |
| 70 | className={`group ${isSelected ? "bg-list-active text-list-active-foreground" : ""}`} |
| 71 | > |
| 72 | <div className="flex w-full items-center justify-between gap-10 py-0.5"> |
| 73 | <div className="flex w-full items-center gap-3"> |
| 74 | <div className="flex h-4 w-4 flex-shrink-0 items-center justify-center"> |
| 75 | {getOrgIcon(organization, "sm")} |
| 76 | </div> |
| 77 | <span |
| 78 | className={`line-clamp-1 flex-1 ${isSelected ? "font-semibold" : ""}`} |
| 79 | > |
| 80 | {organization.name} |
| 81 | </span> |
| 82 | </div> |
| 83 | <div className="flex flex-row items-center gap-1.5"> |
| 84 | <Button |
| 85 | variant="ghost" |
| 86 | size="sm" |
| 87 | className="text-description-muted hover:enabled:text-foreground my-0 h-4 w-4 p-0 opacity-0 transition-opacity group-hover:opacity-100" |
| 88 | onClick={(e) => { |
| 89 | e.stopPropagation(); |
| 90 | navigate(CONFIG_ROUTES.ORGANIZATIONS); |
| 91 | onClose(); // Close the listbox |
| 92 | }} |
| 93 | > |
| 94 | <Cog6ToothIcon className="h-3.5 w-3.5" /> |
| 95 | </Button> |
| 96 | </div> |
| 97 | </div> |
| 98 | </ListboxOption> |
| 99 | ); |
| 100 | } |