import React, { useMemo, useState } from 'react'; import { Building2, Check, ChevronDown, Search } from 'lucide-react'; import { cn } from '@/lib/utils'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; const locationDisplayName = (loc) => (loc?.locationname && loc.locationname.trim()) || loc?.address || 'Unnamed location'; const isValidLocation = (loc) => { const name = String(loc?.locationname || '').toLowerCase(); return !name.includes('retired') && !name.includes('do not use') && !name.includes('test row'); }; export function HubLocationSelect({ locations = [], selectedLocationId = '', onSelectLocation, disabled = false, placeholder = 'Select Hub Location', variant = 'header', // 'header' | 'field' className, }) { const [search, setSearch] = useState(''); const validLocations = useMemo(() => { return (locations || []) .filter(isValidLocation) .sort((a, b) => { if (Boolean(a.isprimary) !== Boolean(b.isprimary)) return a.isprimary ? -1 : 1; return locationDisplayName(a).localeCompare(locationDisplayName(b)); }); }, [locations]); const filtered = useMemo(() => { if (!search.trim()) return validLocations; const q = search.toLowerCase(); return validLocations.filter( (loc) => locationDisplayName(loc).toLowerCase().includes(q) || loc.address?.toLowerCase().includes(q) || loc.city?.toLowerCase().includes(q) || loc.pincode?.includes(q) ); }, [validLocations, search]); const selectedLoc = useMemo(() => { return validLocations.find( (l) => String(l.tenantlocationid || l.locationid) === String(selectedLocationId) ); }, [validLocations, selectedLocationId]); const displayLabel = selectedLoc ? locationDisplayName(selectedLoc) : placeholder; if (variant === 'field') { return ( !open && setSearch('')}> Select Pickup Hub / Kitchen Location
setSearch(e.target.value)} placeholder="Search location or address…" onClick={(e) => e.stopPropagation()} onKeyDown={(e) => e.stopPropagation()} className="h-8 w-full rounded-lg border border-slate-200 bg-slate-50 pl-8 pr-2 text-xs text-slate-900 placeholder:text-slate-400 outline-none focus:border-slate-400 focus:bg-white" />
{filtered.map((loc) => { const id = String(loc.tenantlocationid || loc.locationid); const isSelected = String(selectedLocationId) === id; return ( onSelectLocation(id)} className={cn( 'flex items-start justify-between gap-2 px-2.5 py-2 text-xs cursor-pointer rounded-lg mb-0.5', isSelected ? 'bg-blue-50 text-blue-900 font-semibold' : 'hover:bg-slate-50' )} >

{locationDisplayName(loc)}

{loc.isprimary && ( PRIMARY )}
{(loc.address || loc.city) && (

{[loc.address, loc.city, loc.pincode].filter(Boolean).join(' · ')}

)}
{isSelected && }
); })} {filtered.length === 0 && (

No matching locations found

)}
); } // Header quick picker variant return ( !open && setSearch('')}> Switch Pickup Hub / Kitchen
setSearch(e.target.value)} placeholder="Search kitchen or hub…" onClick={(e) => e.stopPropagation()} onKeyDown={(e) => e.stopPropagation()} className="h-8 w-full rounded-lg border border-slate-200 bg-slate-50 pl-8 pr-2 text-xs text-slate-900 placeholder:text-slate-400 outline-none focus:border-slate-400 focus:bg-white" />
{filtered.map((loc) => { const id = String(loc.tenantlocationid || loc.locationid); const isSelected = String(selectedLocationId) === id; return ( onSelectLocation(id)} className={cn( 'flex items-start justify-between gap-2 px-2.5 py-2 text-xs cursor-pointer rounded-lg mb-0.5', isSelected ? 'bg-blue-50 text-blue-900 font-semibold' : 'hover:bg-slate-50' )} >

{locationDisplayName(loc)}

{loc.isprimary && ( PRIMARY )}
{(loc.address || loc.city) && (

{[loc.address, loc.city].filter(Boolean).join(' · ')}

)}
{isSelected && }
); })} {filtered.length === 0 && (

No matching locations found

)}
); }