import { useQuery } from '@tanstack/react-query'; export interface BtcMapPlace { id: number; lat: number; lon: number; icon: string; name: string; address?: string; opening_hours?: string; verified_at?: string; boosted_until?: string; website?: string; phone?: string; description?: string; } interface UseBtcMapPlacesOptions { lat: number; lon: number; radiusKm?: number; enabled?: boolean; } /** * Fetches Bitcoin-accepting places from BTC Map API within a radius */ export function useBtcMapPlaces({ lat, lon, radiusKm = 25, enabled = true }: UseBtcMapPlacesOptions) { return useQuery({ queryKey: ['btcmap-places', lat.toFixed(2), lon.toFixed(2), radiusKm], queryFn: async () => { const fields = 'id,lat,lon,icon,name,address,opening_hours,verified_at,boosted_until,website,phone,description'; const url = `https://api.btcmap.org/v4/places/search?lat=${lat}&lon=${lon}&radius_km=${radiusKm}&fields=${fields}`; console.log('[useBtcMapPlaces] Fetching:', url); const response = await fetch(url); if (!response.ok) { throw new Error(`BTC Map API error: ${response.status}`); } const places: BtcMapPlace[] = await response.json(); console.log('[useBtcMapPlaces] Found', places.length, 'places'); return places; }, enabled: enabled && lat !== 0 && lon !== 0, staleTime: 5 * 60 * 1000, // 5 minutes gcTime: 30 * 60 * 1000, // 30 minutes cache refetchOnWindowFocus: false, }); } /** * Get Material Icons URL for a BTC Map place icon */ export function getBtcMapIconUrl(icon: string): string { return `https://fonts.gstatic.com/s/i/short-term/release/materialsymbolsoutlined/${icon}/default/24px.svg`; } /** * Check if a place was recently verified (within last 6 months) */ export function isRecentlyVerified(verifiedAt?: string): boolean { if (!verifiedAt) return false; const sixMonthsAgo = new Date(); sixMonthsAgo.setMonth(sixMonthsAgo.getMonth() - 6); return new Date(verifiedAt) > sixMonthsAgo; } /** * Check if a place is currently boosted */ export function isBoosted(boostedUntil?: string): boolean { if (!boostedUntil) return false; return new Date(boostedUntil) > new Date(); }