22 lines
766 B
TypeScript
22 lines
766 B
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { fetchLocations } from "@/lib/location-api";
|
|
import type { Language } from "@/lib/i18n";
|
|
|
|
export function useLocationSearch(query: string, language: Language) {
|
|
const [debouncedQuery, setDebouncedQuery] = useState(query);
|
|
useEffect(() => {
|
|
const timeout = window.setTimeout(() => setDebouncedQuery(query.trim()), 300);
|
|
return () => window.clearTimeout(timeout);
|
|
}, [query]);
|
|
return useQuery({
|
|
queryKey: ["location-search", debouncedQuery, language],
|
|
queryFn: ({ signal }) => fetchLocations(debouncedQuery, language, signal),
|
|
enabled: debouncedQuery.length >= 2,
|
|
staleTime: 24 * 60 * 60 * 1000,
|
|
retry: 1,
|
|
});
|
|
}
|