import { useTranslation } from 'react-i18next'; import { formatValue } from '../../lib/format'; import { ts } from '../../i18n/server'; import { DENSITY_GRADIENT, DENSITY_GRADIENT_DARK, getEnumPaletteForFeature, getFeatureGradient, } from '../../lib/consts'; import { gradientToCss } from '../../lib/utils'; import { CloseIcon } from '../ui/icons/CloseIcon'; import { TickerValue } from '../ui/TickerValue'; function ResetScaleIcon({ className = 'w-4 h-4' }: { className?: string }) { return ( ); } function requireFeatureName(featureName: string | undefined): string { if (!featureName) { throw new Error('Enum legend requested without a feature name'); } return featureName; } function requireEnumPalette( palette: [number, number, number][] | null ): [number, number, number][] { if (!palette) { throw new Error('Enum legend requested without a palette'); } return palette; } function EnumSwatches({ values, palette, }: { values: string[]; palette: [number, number, number][]; }) { return (
{values.map((label, i) => { const color = palette[i % palette.length]; return (
{ts(label)}
); })}
); } function InlineEnumSwatches({ values, palette, }: { values: string[]; palette: [number, number, number][]; }) { return (
{values.map((label, i) => { const color = palette[i % palette.length]; return (
{ts(label)}
); })}
); } export default function MapLegend({ featureLabel, range, showCancel, onCancel, mode, enumValues, featureName, theme = 'light', inline = false, suffix, raw, totalCount, onResetScale, resetScaleDisabled = false, }: { featureLabel: string; range: [number, number]; showCancel: boolean; onCancel: () => void; mode: 'feature' | 'density'; enumValues?: string[]; featureName?: string; theme?: 'light' | 'dark'; inline?: boolean; suffix?: string; raw?: boolean; totalCount?: number; onResetScale?: () => void; resetScaleDisabled?: boolean; }) { const { t } = useTranslation(); const isEnum = enumValues && enumValues.length > 0; const showResetScale = Boolean(onResetScale) && !isEnum; const enumPalette = isEnum ? getEnumPaletteForFeature(requireFeatureName(featureName), enumValues) : null; const densityGradient = theme === 'dark' ? DENSITY_GRADIENT_DARK : DENSITY_GRADIENT; const gradientStyle = mode === 'density' ? gradientToCss(densityGradient) : gradientToCss(getFeatureGradient(featureName)); const fmt = raw ? { raw: true } : undefined; const rangeMin = mode === 'density' ? ( ) : isEnum ? null : ( ); const rangeMax = mode === 'density' ? ( ) : isEnum ? null : ( ); if (inline) { return (
{featureLabel} {showResetScale && ( )} {showCancel && ( )} {isEnum ? ( ) : (
{rangeMin}
{rangeMax}
)}
); } return (
{featureLabel} {(showResetScale || showCancel) && (
{showResetScale && ( )} {showCancel && ( )}
)}
{isEnum ? ( ) : ( <>
{rangeMin} {rangeMax}
)} {totalCount != null && (
{t('common.total')}
)}
); }