feat: redesign dashboard around place search
This commit is contained in:
21
hooks/use-location-search.ts
Normal file
21
hooks/use-location-search.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
"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,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user