import { useEffect, useState } from 'react'; import { createPortal } from 'react-dom'; import { Polyline, Marker, Popup, Circle, Tooltip, useMapEvents } from 'react-leaflet'; import L from 'leaflet'; import { mapItemService } from '../../services/mapItemService'; import { CABLE_COLORS, type MapItem, type CableType } from '../../types/mapItem'; import { ItemContextMenu } from './ItemContextMenu'; import { useDrawingStore } from '../../stores/drawingStore'; // Calculate distance between two lat/lng points in meters using Haversine formula function calculateDistance(lat1: number, lng1: number, lat2: number, lng2: number): number { const R = 6371e3; // Earth's radius in meters const φ1 = (lat1 * Math.PI) / 180; const φ2 = (lat2 * Math.PI) / 180; const Δφ = ((lat2 - lat1) * Math.PI) / 180; const Δλ = ((lng2 - lng1) * Math.PI) / 180; const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) + Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ / 2) * Math.sin(Δλ / 2); const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return R * c; // Distance in meters } // Format distance for display function formatDistance(meters: number): string { if (meters < 1) { return `${Math.round(meters * 100)} cm`; } else if (meters < 1000) { return `${Math.round(meters * 10) / 10} m`; } else { return `${Math.round(meters / 100) / 10} km`; } } interface MapItemsLayerProps { mapId: string; refreshTrigger: number; readOnly?: boolean; } // Custom marker icons for devices using CSS const switchIcon = new L.DivIcon({ html: `
`, className: 'custom-device-marker', iconSize: [40, 40], iconAnchor: [20, 40], }); const indoorApIcon = new L.DivIcon({ html: ``, className: 'custom-device-marker', iconSize: [40, 40], iconAnchor: [20, 40], }); const outdoorApIcon = new L.DivIcon({ html: ``, className: 'custom-device-marker', iconSize: [40, 40], iconAnchor: [20, 40], }); const infoIcon = new L.DivIcon({ html: ``, className: 'custom-info-marker', iconSize: [20, 20], iconAnchor: [10, 20], }); export function MapItemsLayer({ mapId, refreshTrigger, readOnly = false }: MapItemsLayerProps) { const [items, setItems] = useState