import { createFileRoute } from '@tanstack/react-router' import { useCallback, useEffect, useRef, useState } from 'react' import { AlertCircle, Loader2, Minus, Plus } from 'lucide-react' export const Route = createFileRoute('/location-selector')({ // Optional centre override (?lat=&lng=&zoom=) so the map can be opened for a // specific region; defaults to the Mahabubnagar town centre. validateSearch: (search: Record) => { // TanStack's default search parser is parseSearchWith(JSON.parse), so // `?lat=16.3570899` arrives here as a NUMBER, not a string. Accept both: // requiring a string made every param validate to undefined, which dropped // the query string from the URL entirely and left the map on its default // centre for every region. const num = (value: unknown): number | undefined => { if (typeof value === 'number') return Number.isFinite(value) ? value : undefined if (typeof value !== 'string' || value.trim() === '') return undefined const parsed = Number(value) return Number.isFinite(parsed) ? parsed : undefined } return { lat: num(search.lat), lng: num(search.lng), zoom: num(search.zoom), } }, component: LocationSelectorPage, }) type LatLngLiteral = { lat: number; lng: number } // Minimal structural typings for the small slice of the Google Maps JS API used // here, so this route does not require the @types/google.maps package. interface GoogleMapClickEvent { latLng?: { lat: () => number; lng: () => number } | null } interface GoogleMap { addListener: (event: string, handler: (event: GoogleMapClickEvent) => void) => void setZoom: (zoom: number) => void getZoom: () => number | undefined } interface GoogleMarker { setMap: (map: GoogleMap | null) => void setPosition: (position: LatLngLiteral) => void getPosition: () => { lat: () => number; lng: () => number } | null | undefined addListener: (event: string, handler: () => void) => void } interface GoogleMapConstructor { new (element: HTMLElement, options: Record): GoogleMap } interface GoogleMarkerConstructor { new (options: Record): GoogleMarker } // The resolved `maps` + `marker` libraries (both constructors present). interface GoogleMapsApi { Map: GoogleMapConstructor Marker: GoogleMarkerConstructor } type GoogleImportLibrary = (library: string, ...rest: string[]) => Promise // The global `google.maps` namespace, including the dynamic-import entry point. interface GoogleMapsNamespace { importLibrary?: GoogleImportLibrary // Set by the bootstrap; the API invokes it once the namespace is fully ready. __ib__?: () => void } type ReactNativeWebViewBridge = { postMessage: (message: string) => void } type GoogleWindow = Window & { google?: { maps?: GoogleMapsNamespace } // Injected by react-native-webview when this page is embedded in user-ui. ReactNativeWebView?: ReactNativeWebViewBridge } // Mahabubnagar (Mahbubnagar), Telangana, India — default map center. const MAHABUBNAGAR_CENTER: LatLngLiteral = { lat: 16.7488, lng: 77.9851 } const DEFAULT_ZOOM = 15 // Google Maps JS API key. Provide it via a Vite env var, e.g. in // apps/web-ui/.env: // VITE_GOOGLE_MAPS_API_KEY=your-key const GOOGLE_MAPS_API_KEY = import.meta.env.VITE_GOOGLE_MAPS_API_KEY as string | undefined let googleMapsLoader: Promise | null = null /** * Google's recommended "Dynamic Library Import" bootstrap * (https://goo.gle/js-api-loading). * * It defines `google.maps.importLibrary` synchronously and loads the API with * the `callback` signal, so we never read `google.maps` before it exists. A raw * `