import React, { useId, useMemo } from "react"; import { getLegendStops, getUnitForParam, PRECIPITATION_ZERO_STYLE } from "@/utils/colors"; import { normalizePrecipitationHours, PRECIPITATION_HOURS } from "@/utils/precipitation"; import type { PrecipitationHours } from "@/utils/precipitation"; interface MapLegendProps { parameter: string; values?: number[]; precipitationHours: PrecipitationHours; setPrecipitationHours: (hours: PrecipitationHours) => void; noReports?: boolean; periodCounts?: Record | null; availabilityScope?: string; rainfallReportCount?: number; } interface LegendStop { val: number; color: string; showLabel: boolean; } export const MapLegend: React.FC = ({ parameter, values, precipitationHours, setPrecipitationHours, noReports, periodCounts, availabilityScope = "All countries", rainfallReportCount = 0 }) => { const allStops = useMemo(() => getLegendStops(parameter), [parameter]); const unit = getUnitForParam(parameter); const gradientId = useId(); const periodId = useId(); // Clip legend stops to the value range visible on screen const stops = useMemo(() => { if (!values || values.length === 0) { return allStops; } const minVal = Math.min(...values); const maxVal = Math.max(...values); // stops are sorted HIGH → LOW (reversed palette) // Find the range of stops that covers [minVal, maxVal], // plus one extra stop on each end for the boundary color blocks. let startIndex = -1; let endIndex = -1; for (let i = 0; i < allStops.length; i++) { // First stop whose value is <= maxVal (upper boundary of visible range) if (startIndex === -1 && allStops[i].val <= maxVal) { startIndex = i; } // First stop whose value <= minVal (lower boundary of visible range) if (endIndex === -1 && allStops[i].val <= minVal) { endIndex = i; } } if (startIndex === -1) startIndex = 0; if (endIndex === -1) endIndex = allStops.length - 1; // Add one extra stop above (lower index = higher value) and below startIndex = Math.max(0, startIndex - 1); endIndex = Math.min(allStops.length - 1, endIndex + 1); if (startIndex <= endIndex) { return allStops.slice(startIndex, endIndex + 1); } return allStops; }, [allStops, values]); const isTemperature = parameter.includes("temperature"); if (parameter.includes("precipitation")) { // Keep the full scale fixed as the map changes. Each palette interval is // equally tall, with sRGB interpolation matching getColorFromPalette. const ascendingStops = [...allStops].reverse(); return (

Precipitation (mm)

Reported total at selected UTC time.

{periodCounts &&

{availabilityScope}: {periodCounts[precipitationHours]} reporting stations

} {noReports && (

No {precipitationHours}-hour reports{availabilityScope !== "All countries" ? ` for ${availabilityScope}` : ""} at this time. {periodCounts && PRECIPITATION_HOURS.some(hours => periodCounts[hours] > 0) ? ` Available: ${PRECIPITATION_HOURS.filter(hours => periodCounts[hours] > 0).join(", ")} h.` : " Try another UTC time."}

)} {rainfallReportCount > 0 &&

Includes {rainfallReportCount} rainfall-only reports.

} {allStops.map((stop, index) => ( ))}
Continuous precipitation colour scale with uneven value intervals
Amount (mm)Colour
{index === 0 ? `≥${stop.val}` : stop.val}{stop.color}
0 mm
); } return (
{unit}
{stops.map((stop: LegendStop) => (
{stop.showLabel && ( {stop.val} )}
))}
); }; MapLegend.displayName = "MapLegend";