teqo/src/lib/bahiaMapStyle.ts
Francisco Ignácio Fontoura Solla cd8002a6e1 Deliver B12 map viewport fit and hover fixes for filtered Praças.
Aproxima o mapa ao footprint filtrado e corrige hover fora do filtro, ênfase sticky e fill agressivo no Mapa das Praças.
2026-07-21 18:10:32 -03:00

112 lines
2.8 KiB
TypeScript

import type { PathOptions } from 'leaflet'
import {
choroplethFillColor,
choroplethMaxAbsValue,
choroplethMaxValue,
divergingFillColor,
} from '@/lib/choroplethColorScale'
export type ChoroplethValues = Record<string, number>
export type BahiaMapFillMode = 'sequential' | 'diverging'
export type FeatureKeyProperty = 'codarea' | 'code'
export type LayerStyleContext = {
values: ChoroplethValues
fillMode: BahiaMapFillMode
max: number
highlightSet: Set<string>
selectedKey: string | null
hoveredKey: string | null
}
type FeatureStyleInput = {
metric: number
highlighted: boolean
fillMode: BahiaMapFillMode
max: number
}
export const getFeatureStyle = ({
metric,
highlighted,
fillMode,
max,
}: FeatureStyleInput): PathOptions => ({
weight: highlighted ? 2 : 1,
color: highlighted ? '#c51414' : '#a8a29e',
fillColor:
fillMode === 'diverging'
? divergingFillColor(metric, max)
: choroplethFillColor(metric, max),
fillOpacity: metric !== 0 ? 0.78 : 0.35,
})
export const computeChoroplethMax = (
values: ChoroplethValues,
fillMode: BahiaMapFillMode,
scaleMax?: number,
): number =>
scaleMax ??
(fillMode === 'diverging' ? choroplethMaxAbsValue(values) : choroplethMaxValue(values))
export const buildHighlightSet = (highlightKey: string): Set<string> =>
new Set(highlightKey.length > 0 ? highlightKey.split(',').filter(Boolean) : [])
export const canonicalMapKeysKey = (keys: string[]): string =>
keys.length > 0 ? [...keys].sort().join(',') : ''
export const keyPropertyForMode = (mode: 'municipality' | 'territory'): FeatureKeyProperty =>
mode === 'municipality' ? 'codarea' : 'code'
export const featureKeyFromProperties = (
properties: Record<string, string> | undefined,
keyProperty: FeatureKeyProperty,
): string | undefined => properties?.[keyProperty]
export const resolvePathStyle = (context: LayerStyleContext, key: string): PathOptions => {
if (!key) {
return getFeatureStyle({
metric: 0,
highlighted: false,
fillMode: context.fillMode,
max: context.max,
})
}
const activeKey = context.hoveredKey ?? context.selectedKey
const metric = context.values[key] ?? 0
const highlighted = context.highlightSet.has(key) || key === activeKey
return getFeatureStyle({
metric,
highlighted,
fillMode: context.fillMode,
max: context.max,
})
}
export const buildLayerStyleContext = ({
values,
fillMode,
scaleMax,
highlightKey,
selectedKey,
hoveredKey = null,
}: {
values: ChoroplethValues
fillMode: BahiaMapFillMode
scaleMax?: number
highlightKey: string
selectedKey: string | null
hoveredKey?: string | null
}): LayerStyleContext => ({
values,
fillMode,
max: computeChoroplethMax(values, fillMode, scaleMax),
highlightSet: buildHighlightSet(highlightKey),
selectedKey,
hoveredKey,
})