"use client";
import { useEffect, useRef, useState } from "react";
import { MapContainer, TileLayer, useMap, Pane } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import { io } from "socket.io-client";
import L from "leaflet";
import { MapBounds } from "@/app/utils/map";
if (typeof window !== "undefined") {
delete L.Icon.Default.prototype._getIconUrl;
L.Icon.Default.mergeOptions({
iconRetinaUrl:
"https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon-2x.png",
iconUrl:
"https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon.png",
shadowUrl:
"https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-shadow.png",
});
}
const FlightsLayer = ({ flightsData, updateInterval = 5000 }) => {
const map = useMap();
const flightsRef = useRef({});
const drawMarker = (lat, lon, icon) => {
const longitudes = [lon, lon - 360, lon + 360];
return longitudes.map((lng) => {
const marker = L.marker([lat, lng], { icon }).addTo(map);
return marker;
});
};
useEffect(() => {
if (!map) return;
const now = Date.now();
flightsData.forEach((f) => {
if (!f.lat || !f.lon) return;
const flightId = f.hex || f.flight || `${f.lat}_${f.lon}`;
const direction = f.track || f.nav_heading || f.true_heading || 0;
const icon = createPlaneIcon(direction);
if (!flightsRef.current[flightId]) {
const markers = drawMarker(f.lat, f.lon, icon);
const popupContent = `
Flight: ${f.flight?.trim() || "Unknown"}
Registration: ${f.r || "Unknown"}
Aircraft: ${f.desc || f.t || "Unknown"}
Altitude: ${f.alt_baro || "Unknown"} ft
Speed: ${f.gs || "Unknown"} kts
Track: ${f.track || "Unknown"}°
`;
markers[0].bindPopup(popupContent);
flightsRef.current[flightId] = {
prev: { lat: f.lat, lon: f.lon },
next: { lat: f.lat, lon: f.lon },
startTime: now,
endTime: now + updateInterval,
markers,
flightInfo: f,
};
} else {
const flight = flightsRef.current[flightId];
flight.prev = flight.next;
flight.next = { lat: f.lat, lon: f.lon };
flight.startTime = now;
flight.endTime = now + updateInterval;
flight.flightInfo = f;
const newIcon = createPlaneIcon(direction);
const popupContent = `
Flight: ${f.flight?.trim() || "Unknown"}
Registration: ${f.r || "Unknown"}
Aircraft: ${f.desc || f.t || "Unknown"}
Altitude: ${f.alt_baro || "Unknown"} ft
Speed: ${f.gs || "Unknown"} kts
Track: ${f.track || "Unknown"}°
`;
flight.markers[0].setPopupContent(popupContent);
flight.markers.forEach((m, i) => {
const lng = f.lon + (i === 1 ? -360 : i === 2 ? 360 : 0);
m.setLatLng([f.lat, lng]);
m.setIcon(newIcon);
});
}
});
const currentFlightIds = new Set(
flightsData.map((f) => f.hex || f.flight || `${f.lat}_${f.lon}`)
);
Object.keys(flightsRef.current).forEach((flightId) => {
if (!currentFlightIds.has(flightId)) {
flightsRef.current[flightId].markers.forEach((m) => map.removeLayer(m));
delete flightsRef.current[flightId];
}
});
}, [flightsData, map, updateInterval]);
useEffect(() => {
let animFrame;
const animate = () => {
const now = Date.now();
Object.values(flightsRef.current).forEach((f) => {
const t = Math.min(1, (now - f.startTime) / (f.endTime - f.startTime));
const lat = f.prev.lat + (f.next.lat - f.prev.lat) * t;
const lon = f.prev.lon + (f.next.lon - f.prev.lon) * t;
f.markers.forEach((m, i) => {
const newLng = lon + (i === 1 ? -360 : i === 2 ? 360 : 0);
m.setLatLng([lat, newLng]);
});
});
animFrame = requestAnimationFrame(animate);
};
animFrame = requestAnimationFrame(animate);
return () => cancelAnimationFrame(animFrame);
}, []);
return null;
};
const InitialBounds = ({ setBoundries }) => {
const map = useMap();
useEffect(() => {
// Wait for map to be ready, then set initial bounds
map.whenReady(() => {
const bounds = map.getBounds();
setBoundries({
southWest: bounds.getSouthWest(),
northEast: bounds.getNorthEast(),
});
});
}, [map, setBoundries]);
return null;
};
const createPlaneIcon = (heading = 0) => {
const html = `