import { useState, useEffect } from "react"; import { supabase } from "@/lib/supabaseClient"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Search, Plus, X } from "lucide-react"; import { useDebounce } from "@/hooks/useDebounce"; import { Badge } from "@/components/ui/badge"; interface ListSearchProps { listType: string; onSelect: (entityType: string, entityId: string, entityName: string) => void; onClose: () => void; } interface SearchResult { id: string; name: string; type: "park" | "ride" | "company"; subtitle?: string; } export function ListSearch({ listType, onSelect, onClose }: ListSearchProps) { const [query, setQuery] = useState(""); const [results, setResults] = useState([]); const [loading, setLoading] = useState(false); const debouncedQuery = useDebounce(query, 300); useEffect(() => { if (debouncedQuery.length >= 2) { searchEntities(); } else { setResults([]); } }, [debouncedQuery, listType]); const searchEntities = async () => { setLoading(true); const searchResults: SearchResult[] = []; // Determine which entity types to search based on list type const shouldSearchParks = listType === "parks" || listType === "mixed"; const shouldSearchRides = listType === "rides" || listType === "coasters" || listType === "mixed"; const shouldSearchCompanies = listType === "companies" || listType === "mixed"; // Search parks if (shouldSearchParks) { const { data: parks } = await supabase .from("parks") .select("id, name, park_type, location_id") .ilike("name", `%${debouncedQuery}%`) .limit(10); if (parks) { searchResults.push( ...parks.map((park) => ({ id: park.id, name: park.name, type: "park" as const, subtitle: park.park_type, })) ); } } // Search rides if (shouldSearchRides) { const { data: rides } = await supabase .from("rides") .select("id, name, category, park:parks(name)") .ilike("name", `%${debouncedQuery}%`) .limit(10); if (rides) { interface RideSearchResult { id: string; name: string; park?: { name: string } | null; category?: string | null; } searchResults.push( ...rides.map((ride: RideSearchResult) => ({ id: ride.id, name: ride.name, type: "ride" as const, subtitle: ride.park?.name || ride.category || 'Unknown', })) ); } } // Search companies if (shouldSearchCompanies) { const { data: companies } = await supabase .from("companies") .select("id, name, company_type") .ilike("name", `%${debouncedQuery}%`) .limit(10); if (companies) { searchResults.push( ...companies.map((company) => ({ id: company.id, name: company.name, type: "company" as const, subtitle: company.company_type, })) ); } } setResults(searchResults); setLoading(false); }; return (
setQuery(e.target.value)} placeholder="Search for parks, rides, or companies..." className="pl-9" />
{loading && (
Searching...
)} {!loading && results.length === 0 && debouncedQuery.length >= 2 && (
No results found. Try a different search term.
)} {!loading && results.length > 0 && (
{results.map((result) => (

{result.name}

{result.type} {result.subtitle && ( {result.subtitle} )}
))}
)} {debouncedQuery.length < 2 && (
Type at least 2 characters to search
)}
); }