import { useRef, useEffect, useState } from "react"; import { Box, Typography, Stack, useTheme, GlobalStyles } from "@mui/material"; import Map, { Marker, Popup } from "react-map-gl/maplibre"; import type { MapRef } from "react-map-gl/maplibre"; import type { FlatGeoCheck } from "@/Types/GeoCheck"; import "maplibre-gl/dist/maplibre-gl.css"; interface GeoChecksMapProps { geoChecks: FlatGeoCheck[]; } export const GeoChecksMap = ({ geoChecks }: GeoChecksMapProps) => { const mapRef = useRef(null); const [selectedCheck, setSelectedCheck] = useState(null); const theme = useTheme(); const isDarkMode = theme.palette.mode === "dark"; const mapPopupStyles = ( ); useEffect(() => { if (geoChecks.length === 0 || !mapRef.current) return; const bounds = geoChecks.reduce( (acc, check) => { return { minLng: Math.min(acc.minLng, check.location.longitude), maxLng: Math.max(acc.maxLng, check.location.longitude), minLat: Math.min(acc.minLat, check.location.latitude), maxLat: Math.max(acc.maxLat, check.location.latitude), }; }, { minLng: Infinity, maxLng: -Infinity, minLat: Infinity, maxLat: -Infinity, } ); if (bounds.minLng !== Infinity) { mapRef.current.fitBounds( [ [bounds.minLng, bounds.minLat], [bounds.maxLng, bounds.maxLat], ], { padding: 50, duration: 1000 } ); } }, [geoChecks]); const getMarkerColor = (status: boolean): string => { return status ? theme.palette.success.main : theme.palette.error.main; }; const formatResponseTime = (timing: number): string => { return `${timing.toFixed(0)}ms`; }; return ( <> {mapPopupStyles} {geoChecks.map((check, index) => ( { e.originalEvent.stopPropagation(); setSelectedCheck(check); }} >
))} {selectedCheck && ( setSelectedCheck(null)} closeOnClick={false} > {selectedCheck.location.city}, {selectedCheck.location.country} Status: {" "} {selectedCheck.status ? "Up" : "Down"} Status Code: {" "} {selectedCheck.statusCode} Response Time: {" "} {formatResponseTime(selectedCheck.timings.total)} {new Date(selectedCheck.createdAt).toLocaleString()} )} ); };