import { useState, useEffect, useRef } from 'react' import { useQuery } from '@tanstack/react-query' import { Search } from 'lucide-react' import { searchRecipes } from '../api/recipes' import { RecipeCardSmall } from '../components/recipe/RecipeCardSmall' import { EmptyState } from '../components/ui/EmptyState' export function SearchPage() { const [query, setQuery] = useState('') const [debouncedQuery, setDebouncedQuery] = useState('') const inputRef = useRef(null) useEffect(() => { inputRef.current?.focus() }, []) useEffect(() => { const timer = setTimeout(() => setDebouncedQuery(query), 300) return () => clearTimeout(timer) }, [query]) const { data, isLoading } = useQuery({ queryKey: ['search', debouncedQuery], queryFn: () => searchRecipes(debouncedQuery), enabled: debouncedQuery.length >= 2, }) const results = data?.data ?? (Array.isArray(data) ? data as any[] : []) return (
{/* Search Input */}
setQuery(e.target.value)} placeholder="Rezept suchen..." className="w-full pl-10 pr-4 py-3 bg-surface rounded-xl border border-sand text-espresso text-sm focus:outline-none focus:border-primary" />
{/* Results */} {debouncedQuery.length < 2 ? ( ) : isLoading ? (

Suche...

) : results.length === 0 ? ( ) : ( <>

{results.length} Ergebnis{results.length !== 1 ? 'se' : ''}

{results.map((recipe: any) => ( ))}
)}
) }