gendesign/frontend/src/hooks/useParcelAnalyzeQuery.ts

32 lines
1.1 KiB
TypeScript

"use client";
import { useQuery } from "@tanstack/react-query";
import { apiFetch } from "@/lib/api";
import type { ParcelAnalysis } from "@/types/site-finder";
/**
* TanStack Query-based hook for analysis page route `/site-finder/analysis/[cad]`.
*
* Unlike `useSiteAnalysis` (mutation-based, triggered on form submit), this hook
* drives the analysis page where `cad` is a URL param — so we use `useQuery`
* for automatic fetch + caching.
*
* Note: the 202 Accepted on-demand fetch flow is NOT handled here — the analysis
* page assumes geometry is already available (the legacy page handles on-demand
* fetching and can redirect to this route once done). For now, a 202 response
* will surface as an error state.
*/
export function useParcelAnalyzeQuery(cad: string) {
return useQuery<ParcelAnalysis, Error>({
queryKey: ["parcel-analyze", cad],
queryFn: () =>
apiFetch<ParcelAnalysis>(
`/api/v1/parcels/${encodeURIComponent(cad)}/analyze`,
{ method: "POST" },
),
enabled: !!cad,
staleTime: 5 * 60 * 1000, // 5 min — backend analysis is expensive
retry: 1,
});
}