/** * Trouve-KA — barre de recherche (homepage et en-tête des résultats) * Author: Simon-Pierre Boucher * Contact: contact@spboucher.ai */ "use client"; import { useRouter } from "next/navigation"; import { useEffect, useState, type FormEvent } from "react"; import { cn } from "@/lib/utils"; interface SearchBarProps { initialQuery?: string; autoFocus?: boolean; size?: "md" | "lg"; /** `inline` : bouton dans la barre; `below` : grand bouton sous la barre. */ buttonPlacement?: "inline" | "below"; /** Suggestions québécoises affichées en placeholder, en rotation douce. */ placeholderSuggestions?: string[]; className?: string; } const SUGGESTION_INTERVAL_MS = 4000; export function SearchBar({ initialQuery = "", autoFocus = false, size = "md", buttonPlacement = "inline", placeholderSuggestions, className, }: SearchBarProps) { const router = useRouter(); const [query, setQuery] = useState(initialQuery); const [suggestionIndex, setSuggestionIndex] = useState(0); const suggestionCount = placeholderSuggestions?.length ?? 0; useEffect(() => { if (suggestionCount < 2) return; if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return; const timer = window.setInterval( () => setSuggestionIndex((index) => (index + 1) % suggestionCount), SUGGESTION_INTERVAL_MS, ); return () => window.clearInterval(timer); }, [suggestionCount]); const placeholder = suggestionCount > 0 ? `Essaie « ${placeholderSuggestions![suggestionIndex % suggestionCount]} »…` : "Rechercher…"; function onSubmit(event: FormEvent) { event.preventDefault(); const q = query.trim(); if (!q) return; router.push(`/search?q=${encodeURIComponent(q)}`); } const isLarge = size === "lg"; return (
setQuery(event.target.value)} placeholder={placeholder} autoComplete="off" spellCheck={false} // eslint-disable-next-line jsx-a11y/no-autofocus autoFocus={autoFocus} className={cn( "field appearance-none", isLarge ? "h-14 pl-12 text-lg" : "h-10 pl-10 text-sm", buttonPlacement === "inline" ? isLarge ? "pr-32" : "pr-28" : isLarge ? "pr-6" : "pr-4", )} /> {buttonPlacement === "inline" ? ( ) : null}
{buttonPlacement === "below" ? ( ) : null}
); }