'use client' import { useState, useEffect, useRef } from 'react' import { useRouter } from 'next/navigation' import { Search } from 'lucide-react' import { searchListingsAction } from '@/app/actions' import { useLocale } from 'next-intl' interface Suggestion { id: string nameTr: string nameEn: string nameRu: string slug: string categorySlug: string } export default function LiveSearchInput({ placeholder = "İsim veya adres...", defaultValue = "", className = "" }: { placeholder?: string defaultValue?: string className?: string }) { const [query, setQuery] = useState(defaultValue) const [suggestions, setSuggestions] = useState([]) const [isOpen, setIsOpen] = useState(false) const wrapperRef = useRef(null) const router = useRouter() const locale = useLocale() useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (wrapperRef.current && !wrapperRef.current.contains(event.target as Node)) { setIsOpen(false) } } document.addEventListener('mousedown', handleClickOutside) return () => document.removeEventListener('mousedown', handleClickOutside) }, []) useEffect(() => { if (query.length < 2) { setSuggestions([]) setIsOpen(false) return } const timer = setTimeout(async () => { const results = await searchListingsAction(query) setSuggestions(results) setIsOpen(true) }, 300) return () => clearTimeout(timer) }, [query]) const getLocalizedName = (s: Suggestion) => { return locale === 'ru' ? s.nameRu : locale === 'en' ? s.nameEn : s.nameTr } return (
setQuery(e.target.value)} onFocus={() => { if (suggestions.length > 0) setIsOpen(true) }} className={className ? className : "w-full bg-transparent border-0 focus:ring-0 text-sm py-3 pl-10 pr-3 text-ink placeholder:text-ink/40 outline-none h-full rounded-xl"} />
{isOpen && suggestions.length > 0 && (
    {suggestions.map((s) => (
  • ))}
)}
) }