import { useState, useEffect } from 'react'; import { useQuery } from '@tanstack/react-query'; import { StopLocation } from '../../types'; interface SearchBoxProps { onSelect: (stop: StopLocation) => void; endpoint: string; placeholder?: string; } export const SearchBox = ({ onSelect, endpoint, placeholder = 'Search for a station...' }: SearchBoxProps) => { const [input, setInput] = useState(''); const [query, setQuery] = useState(''); useEffect(() => { const timer = setTimeout(() => setQuery(input.trim()), 350); return () => clearTimeout(timer); }, [input]); const { data, isFetching } = useQuery<{ StopLocation: StopLocation[] }>({ queryKey: ['search', endpoint, query], queryFn: () => fetch(`${endpoint}?q=${encodeURIComponent(query)}`).then((r) => r.json()), enabled: query.length > 0, staleTime: 60_000, }); const results = data?.StopLocation ?? []; return (
setInput(e.target.value)} placeholder={placeholder} className="w-full bg-black text-white px-4 py-2 border border-white outline-none text-lg" />
{isFetching &&

Searching...

} {!isFetching && results.map((stop) => (
onSelect(stop)} className="flex justify-between items-center py-3 border-b border-zinc-700 cursor-pointer text-lg" > {stop.name} {stop.id}
))} {!isFetching && query && results.length === 0 && (

No stations found.

)}
); };