"use client"; import { useEffect, useState, useRef } from 'react'; import dynamic from 'next/dynamic'; import { DEMO_DEVICES, DEMO_MAP_CENTER, DEMO_MAP_ZOOM, DemoDevice } from '@/lib/demo-data'; // Dynamically import Leaflet components (client-side only) const MapContainer = dynamic( () => import('react-leaflet').then((mod) => mod.MapContainer), { ssr: false } ); const TileLayer = dynamic( () => import('react-leaflet').then((mod) => mod.TileLayer), { ssr: false } ); const Marker = dynamic( () => import('react-leaflet').then((mod) => mod.Marker), { ssr: false } ); const Popup = dynamic( () => import('react-leaflet').then((mod) => mod.Popup), { ssr: false } ); const Polyline = dynamic( () => import('react-leaflet').then((mod) => mod.Polyline), { ssr: false } ); export default function DemoMap() { const [devicePositions, setDevicePositions] = useState>(new Map()); const [isClient, setIsClient] = useState(false); const iconCache = useRef>(new Map()); // Initialize on client side useEffect(() => { setIsClient(true); // Initialize all devices at position 0 const initialPositions = new Map(); DEMO_DEVICES.forEach(device => { initialPositions.set(device.id, 0); }); setDevicePositions(initialPositions); }, []); // Animate device movements useEffect(() => { if (!isClient) return; const interval = setInterval(() => { setDevicePositions(prev => { const next = new Map(prev); DEMO_DEVICES.forEach(device => { const currentPos = next.get(device.id) || 0; const nextPos = (currentPos + 1) % device.route.length; next.set(device.id, nextPos); }); return next; }); }, 3000); // Move every 3 seconds return () => clearInterval(interval); }, [isClient]); // Create custom marker icons const getDeviceIcon = (color: string) => { if (typeof window === 'undefined') return null; if (iconCache.current.has(color)) { return iconCache.current.get(color); } const L = require('leaflet'); const icon = L.divIcon({ className: 'custom-marker', html: `
`, iconSize: [24, 24], iconAnchor: [12, 12], }); iconCache.current.set(color, icon); return icon; }; if (!isClient) { return (

Loading demo map...

); } return (
{DEMO_DEVICES.map(device => { const currentPosIdx = devicePositions.get(device.id) || 0; const currentPos = device.route[currentPosIdx]; const pathSoFar = device.route.slice(0, currentPosIdx + 1); return (
{/* Movement trail */} {pathSoFar.length > 1 && ( [loc.lat, loc.lng])} color={device.color} weight={3} opacity={0.6} /> )} {/* Current position marker */}

{device.name}

Position: {currentPosIdx + 1}/{device.route.length}

Lat: {currentPos.lat.toFixed(4)}

Lng: {currentPos.lng.toFixed(4)}

); })}
); }