/** * Sector Selector Component * Displays a dropdown to select sectors for the active site * Used in the header area of pages that need sector filtering */ import { useState, useEffect, useRef } from 'react'; import { Dropdown } from '../ui/dropdown/Dropdown'; import { DropdownItem } from '../ui/dropdown/DropdownItem'; import { useSectorStore } from '../../store/sectorStore'; import { useSiteStore } from '../../store/siteStore'; export default function SectorSelector() { const { activeSite } = useSiteStore(); const { activeSector, sectors, setActiveSector, loading } = useSectorStore(); const [isOpen, setIsOpen] = useState(false); const buttonRef = useRef(null); // Don't render if no active site if (!activeSite) { return null; } // Don't render if no sectors available if (!loading && sectors.length === 0) { return (
No sectors available
); } const handleSectorSelect = (sectorId: number | null) => { if (sectorId === null) { // "All Sectors" option setActiveSector(null); setIsOpen(false); } else { const sector = sectors.find(s => s.id === sectorId); if (sector) { setActiveSector(sector); setIsOpen(false); } } }; return (
setIsOpen(false)} anchorRef={buttonRef} placement="bottom-right" className="w-64 p-2 overflow-y-auto max-h-[300px]" > {/* "All Sectors" option */} handleSectorSelect(null)} className={`flex items-center gap-3 px-3 py-2 font-medium rounded-lg text-sm text-left ${ !activeSector ? "bg-brand-50 text-brand-700 dark:bg-brand-500/20 dark:text-brand-300" : "text-gray-700 hover:bg-gray-100 hover:text-gray-700 dark:text-gray-400 dark:hover:bg-white/5 dark:hover:text-gray-300" }`} > All Sectors {!activeSector && ( )} {sectors.map((sector) => ( handleSectorSelect(sector.id)} className={`flex items-center gap-3 px-3 py-2 font-medium rounded-lg text-sm text-left ${ activeSector?.id === sector.id ? "bg-brand-50 text-brand-700 dark:bg-brand-500/20 dark:text-brand-300" : "text-gray-700 hover:bg-gray-100 hover:text-gray-700 dark:text-gray-400 dark:hover:bg-white/5 dark:hover:text-gray-300" }`} > {sector.name} {activeSector?.id === sector.id && ( )} ))}
); }