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 (
Searching...
} {!isFetching && results.map((stop) => (No stations found.
)}