"use client"; import React, { useEffect, useRef, useState } from 'react'; import { MapContainer, ImageOverlay, useMap, TileLayer } from 'react-leaflet'; import L from 'leaflet'; import 'leaflet/dist/leaflet.css'; import { DifferenceMapData } from '../types'; import { BASE_URL } from '@/lib/api/base-client'; import { visualizationClient } from '@/lib/api/visualization-client'; interface DifferenceMapViewerProps { differenceMap: DifferenceMapData; errorMapUrl?: string | null; /** @deprecated kept for backwards-compat with Plotly-era callers */ sharedLayout?: Record; /** @deprecated kept for backwards-compat with Plotly-era callers */ onRelayout?: (...args: unknown[]) => void; isFullscreen: boolean; fileIdForBounds?: string | null; variable?: string; } /** Auto-fit the map to the image bounds whenever the URL changes */ function FitBounds({ bounds }: { bounds: L.LatLngBoundsExpression }) { const map = useMap(); const boundsRef = useRef(bounds); useEffect(() => { boundsRef.current = bounds; // @ts-ignore - Leaflet types are notoriously picky about bounds arrays map.fitBounds(L.latLngBounds(bounds), { animate: false }); }, [map, bounds]); return null; } export function DifferenceMapViewer({ differenceMap, errorMapUrl, isFullscreen, fileIdForBounds, variable, }: DifferenceMapViewerProps) { const heightClass = isFullscreen ? 'h-[80vh]' : 'h-[60vh] min-h-[500px]'; const [boundsData, setBoundsData] = useState<[number, number, number, number] | undefined>(undefined); const [isBoundsLoading, setIsBoundsLoading] = useState(!!fileIdForBounds); useEffect(() => { if (fileIdForBounds) { setIsBoundsLoading(true); visualizationClient.getBounds(fileIdForBounds, variable || "C13").then(res => { if (res.success && res.data && res.data.bounds) { const [[south, west], [north, east]] = res.data.bounds; setBoundsData([south, west, north, east]); } }).catch(console.error) .finally(() => setIsBoundsLoading(false)); } else { setIsBoundsLoading(false); } }, [fileIdForBounds, variable]); const fullUrl = errorMapUrl ? (errorMapUrl.startsWith('http') ? errorMapUrl : `${BASE_URL}${errorMapUrl}`) : null; if (isBoundsLoading) { return (

Fetching geographic coordinates...

); } const isValidBounds = boundsData && Array.isArray(boundsData) && boundsData.length === 4 && boundsData.every(n => typeof n === 'number' && !isNaN(n)); const mapBounds: L.LatLngBoundsExpression = isValidBounds ? [[boundsData[0], boundsData[1]], [boundsData[2], boundsData[3]]] : [[8.4, 68.7], [37.6, 97.25]]; return (
{differenceMap.band} (Error/Diff Map)
{fullUrl ? ( ) : (
Error map layer not available. Ensure 2 files are uploaded and processed.
)}
); }