import { useState, useMemo } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Badge } from '@/components/ui/badge'; import { Card, CardContent } from '@/components/ui/card'; import { useSkateSpots } from '@/hooks/useSkateSpots'; import { MapPin, Search, X, Plus } from 'lucide-react'; import { useTranslation } from '@/contexts/language'; import { cn } from '@/lib/utils'; interface SpotPickerProps { selectedSpots: string[]; // Array of "pubkey:identifier" strings onSpotsChange: (spots: string[]) => void; maxSpots?: number; label?: string; description?: string; } export function SpotPicker({ selectedSpots, onSpotsChange, maxSpots = 5, label = 'Spots Where You\'re Available', description = 'Select spots where you offer your services', }: SpotPickerProps) { const { t } = useTranslation(); const { data: allSpots, isLoading: spotsLoading } = useSkateSpots(100); const [searchQuery, setSearchQuery] = useState(''); const [isExpanded, setIsExpanded] = useState(false); // Filter spots for search const filteredSpots = useMemo(() => { if (!allSpots) return []; if (!searchQuery) return allSpots.slice(0, 10); const query = searchQuery.toLowerCase(); return allSpots .filter(s => s.title.toLowerCase().includes(query) || s.location?.toLowerCase().includes(query) ) .slice(0, 10); }, [allSpots, searchQuery]); // Get selected spot details const selectedSpotDetails = useMemo(() => { if (!allSpots) return []; return selectedSpots .map(spotId => { const [pubkey, identifier] = spotId.split(':'); return allSpots.find(s => s.pubkey === pubkey && s.identifier === identifier); }) .filter(Boolean); }, [allSpots, selectedSpots]); const toggleSpot = (pubkey: string, identifier: string) => { const spotId = `${pubkey}:${identifier}`; if (selectedSpots.includes(spotId)) { onSpotsChange(selectedSpots.filter(id => id !== spotId)); } else if (selectedSpots.length < maxSpots) { onSpotsChange([...selectedSpots, spotId]); } }; const removeSpot = (spotId: string) => { onSpotsChange(selectedSpots.filter(id => id !== spotId)); }; return (

{description}

{selectedSpots.length}/{maxSpots}
{/* Selected spots */} {selectedSpotDetails.length > 0 && (
{selectedSpotDetails.map((spot) => spot && ( {spot.title} ))}
)} {/* Add spot button / expanded picker */} {selectedSpots.length < maxSpots && ( <> {!isExpanded ? ( ) : (
setSearchQuery(e.target.value)} className="bg-gray-800 border-gray-700 pl-9 h-9" />
{filteredSpots.map((spot) => { const spotId = `${spot.pubkey}:${spot.identifier}`; const isSelected = selectedSpots.includes(spotId); return ( ); })} {spotsLoading ? (

{t('mapUi.loadingSpots')}

) : filteredSpots.length === 0 ? (

{t('mapUi.noSpotsFound')}

) : null}
)} )} {selectedSpots.length === 0 && (

{t('mapUi.addingSpotsHint')}

)}
); }