43 lines
1 KiB
TypeScript
43 lines
1 KiB
TypeScript
/**
|
|
* Placeholder - will be extracted from frontend/src/hooks/
|
|
*/
|
|
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
interface GeocodeResult {
|
|
lat: number;
|
|
lon: number;
|
|
display_name: string;
|
|
place_id: number;
|
|
}
|
|
|
|
interface GeocodingOptions {
|
|
enabled?: boolean;
|
|
}
|
|
|
|
/**
|
|
* Hook for geocoding addresses using Nominatim or local geocoding API
|
|
*/
|
|
export function useGeocoding(address: string, options: GeocodingOptions = {}) {
|
|
const { enabled = true } = options;
|
|
|
|
return useQuery({
|
|
queryKey: ['geocode', address],
|
|
queryFn: async (): Promise<GeocodeResult | null> => {
|
|
if (!address.trim()) return null;
|
|
|
|
// Use local geocoding API first, fallback to Nominatim
|
|
const response = await fetch(
|
|
`http://localhost:8002/geocode?address=${encodeURIComponent(address)}`
|
|
);
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Geocoding failed: ${response.statusText}`);
|
|
}
|
|
|
|
return response.json();
|
|
},
|
|
enabled: enabled && !!address.trim(),
|
|
staleTime: 1000 * 60 * 60, // Cache for 1 hour
|
|
});
|
|
}
|