teqo/src/components/campaign/map/BahiaMap.tsx
Francisco Ignácio Fontoura Solla 66feecd66b simplify(B13): corrige classe agregada, bolhas em pane proprio e registra debitos
O /simplify da escala relativa do mapa achou um bug semantico real em
computeAggregateTerritorialClass — somava o headroom dos slugs e comparava
contra a mediana de UMA unidade, classificando um grupo grande como expansao
quando ele e marginal. A mediana agora escala por slugs.length, com teste.

As bolhas sairam do centro do bounding box para o centroide real e ganharam
pane proprio (z-index 450), porque o bringToFront do hover as escondia atras
do poligono. Legenda e pintura passaram a compartilhar o mesmo predicado,
a copy dos quantis parou de prometer faixas de igual largura, e o subtitulo
do painel parou de dizer "em 2026" com 2022 selecionado (defeito do B11).

Fonte unica onde havia duas: NO_DATA_FILL, o raio das bolhas e
formatPlacementOrdinal (ex-formatMunicipalityVoteRank, agora client-safe em
electionFormat.ts).

Os quatro debitos maiores que o cleanup viraram o fill-in B13+
(docs/plans/escala-dry-pos-b13.md); a flakiness dos e2e, reproduzida em main
limpo, foi para o ledger com a causa afinada.
2026-07-26 05:12:37 -03:00

546 lines
17 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use client'
import type { Feature } from 'geojson'
import L from 'leaflet'
import { useEffect, useMemo, useRef, useState } from 'react'
import 'leaflet/dist/leaflet.css'
import { Spinner } from '@/components/ui/Spinner'
import {
loadMunicipalityGeometryModule,
loadTerritoryGeometryModule,
type MunicipalityGeometryModule,
type TerritoryGeometryModule,
} from '@/lib/bahiaGeometries'
import {
bubbleRadius,
buildHighlightSet,
buildLayerStyleContext,
canonicalMapKeysKey,
featureKeyFromProperties,
keyPropertyForMode,
resolvePathStyle,
type BahiaMapFillMode,
type ChoroplethFills,
type ChoroplethValues,
type LayerStyleContext,
} from '@/lib/bahiaMapStyle'
import { choroplethMaxValue } from '@/lib/choroplethColorScale'
import { cn } from '@/lib/utils'
const BAHIA_BOUNDS: L.LatLngBoundsExpression = [
[-18.5, -46.8],
[-8.5, -37.0],
]
/**
* Bubbles get their own pane above the polygons: hovering a município calls
* `bringToFront()` on its path, which in a shared pane would raise the polygon
* over the very bubble it describes.
*/
const BUBBLE_PANE = 'municipality-bubbles'
export type BahiaMapMode = 'municipality' | 'territory'
export type BahiaMapFeatureInfo = {
key: string
name: string
}
type BahiaMapProps = {
mode: BahiaMapMode
values: ChoroplethValues
/** 'diverging': positive = campaign red, negative = blue, zero = white. */
fillMode?: BahiaMapFillMode
/** When set, overrides auto max from `values` (e.g. fixed 1 for 0–100% shares). */
scaleMax?: number
/**
* Discrete class fills by feature key (B13). When provided they replace the
* continuous ramp entirely, and a key that is absent renders as "no data".
* Must be referentially stable across renders, or every render repaints
* every path.
*/
fillByKey?: ChoroplethFills
/**
* B13 — proportional symbols: feature key → magnitude, drawn as a circle at
* the feature's centre with radius ∝ √value. Absent keys get no bubble.
*/
bubbleValues?: ChoroplethValues
/** Bubble fill by feature key; keys without one fall back to the neutral fill. */
bubbleFillByKey?: ChoroplethFills
highlightKeys?: string[]
/** Fit viewport to these keys without highlighting them. */
fitToKeys?: string[]
/** When non-empty, only these keys receive hover/click. */
interactiveKeys?: string[]
selectedKey?: string | null
onFeatureSelect?: (info: BahiaMapFeatureInfo | null) => void
onFeatureActivate?: (key: string) => void
className?: string
heightClassName?: string
ariaLabel: string
}
const getPointerType = (event: L.LeafletMouseEvent, fallback = 'mouse') =>
event.originalEvent instanceof PointerEvent ? event.originalEvent.pointerType : fallback
const fitMapToHighlights = (
map: L.Map,
mode: BahiaMapMode,
highlightSet: Set<string>,
geometryModule: MunicipalityGeometryModule | TerritoryGeometryModule,
) => {
if (highlightSet.size === 0) {
map.fitBounds(BAHIA_BOUNDS, { padding: [16, 16] })
return
}
const highlightedFeatures =
mode === 'municipality'
? [...highlightSet].flatMap((key) => {
const feature = (geometryModule as MunicipalityGeometryModule).getMunicipalityFeature(key)
return feature ? [feature] : []
})
: [...highlightSet].flatMap((key) => {
const feature = (geometryModule as TerritoryGeometryModule).getTerritoryFeature(key)
return feature ? [feature] : []
})
if (highlightedFeatures.length === 0) {
map.fitBounds(BAHIA_BOUNDS, { padding: [16, 16] })
return
}
const highlightBounds = L.geoJSON(highlightedFeatures).getBounds()
if (highlightBounds.isValid()) {
map.fitBounds(highlightBounds, { padding: [24, 24], maxZoom: 10 })
}
}
export const BahiaMap = ({
mode,
values,
fillMode = 'sequential',
scaleMax,
fillByKey,
bubbleValues,
bubbleFillByKey,
highlightKeys = [],
fitToKeys = [],
interactiveKeys = [],
selectedKey = null,
onFeatureSelect,
onFeatureActivate,
className,
heightClassName = 'h-[320px]',
ariaLabel,
}: BahiaMapProps) => {
const containerRef = useRef<HTMLDivElement>(null)
const mapRef = useRef<L.Map | null>(null)
const layerRef = useRef<L.GeoJSON | null>(null)
const geometryModuleRef = useRef<MunicipalityGeometryModule | TerritoryGeometryModule | null>(
null,
)
const pathByKeyRef = useRef<Map<string, L.Path>>(new Map())
const styleContextRef = useRef<LayerStyleContext | null>(null)
const geometryReadyRef = useRef(false)
const touchPinnedRef = useRef(false)
const onFeatureSelectRef = useRef(onFeatureSelect)
const onFeatureActivateRef = useRef(onFeatureActivate)
const interactiveSetRef = useRef<Set<string> | null>(null)
const lastFittedViewportKeyRef = useRef<string | null>(null)
const stylePropsRef = useRef({
values,
fillMode,
scaleMax,
fillByKey,
highlightKey: '',
selectedKey,
})
const [status, setStatus] = useState<'loading' | 'ready' | 'error'>('loading')
const highlightKey = useMemo(() => canonicalMapKeysKey(highlightKeys), [highlightKeys])
const fitKey = useMemo(() => canonicalMapKeysKey(fitToKeys), [fitToKeys])
const interactiveKey = useMemo(() => canonicalMapKeysKey(interactiveKeys), [interactiveKeys])
const viewportKey = useMemo(() => fitKey || highlightKey, [fitKey, highlightKey])
const interactiveSet = useMemo(
() => (interactiveKey ? buildHighlightSet(interactiveKey) : null),
[interactiveKey],
)
interactiveSetRef.current = interactiveSet
stylePropsRef.current = {
values,
fillMode,
scaleMax,
fillByKey,
highlightKey,
selectedKey,
}
useEffect(() => {
onFeatureSelectRef.current = onFeatureSelect
onFeatureActivateRef.current = onFeatureActivate
}, [onFeatureActivate, onFeatureSelect])
const isKeyInteractive = (key: string) => {
const set = interactiveSetRef.current
return set === null || set.has(key)
}
const resolvePathStyleForFeature = (context: LayerStyleContext, key: string) => ({
...resolvePathStyle(context, key),
interactive: isKeyInteractive(key),
})
const restyleFeature = (key: string) => {
const context = styleContextRef.current
const path = pathByKeyRef.current.get(key)
if (!context || !path) return
path.setStyle(resolvePathStyleForFeature(context, key))
}
const restyleAllPaths = () => {
const context = styleContextRef.current
if (!context) return
for (const [key, path] of pathByKeyRef.current) {
path.setStyle(resolvePathStyleForFeature(context, key))
}
}
const setHoveredKey = (
nextKey: string | null,
bringToFrontLayer?: L.Path,
clearSelection = false,
) => {
const context = styleContextRef.current
if (!context) return
const previousKey = context.hoveredKey
if (previousKey === nextKey) return
context.hoveredKey = nextKey
if (clearSelection && previousKey && context.selectedKey === previousKey) {
context.selectedKey = null
}
if (previousKey) {
restyleFeature(previousKey)
}
if (nextKey) {
restyleFeature(nextKey)
;(bringToFrontLayer ?? pathByKeyRef.current.get(nextKey))?.bringToFront()
return
}
if (context.selectedKey && context.selectedKey !== previousKey) {
restyleFeature(context.selectedKey)
}
}
useEffect(() => {
const container = containerRef.current
if (!container || mapRef.current) return
const map = L.map(container, {
zoomControl: true,
attributionControl: true,
scrollWheelZoom: false,
})
map.fitBounds(BAHIA_BOUNDS, { padding: [16, 16] })
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
maxZoom: 18,
}).addTo(map)
mapRef.current = map
const resizeObserver =
typeof ResizeObserver !== 'undefined'
? new ResizeObserver(() => {
map.invalidateSize()
})
: null
resizeObserver?.observe(container)
return () => {
resizeObserver?.disconnect()
map.remove()
mapRef.current = null
layerRef.current = null
geometryModuleRef.current = null
pathByKeyRef.current = new Map()
styleContextRef.current = null
geometryReadyRef.current = false
}
}, [])
useEffect(() => {
const map = mapRef.current
if (!map) return
let cancelled = false
setStatus('loading')
geometryReadyRef.current = false
lastFittedViewportKeyRef.current = null
const renderLayer = async () => {
try {
const geometryModule =
mode === 'municipality'
? await loadMunicipalityGeometryModule()
: await loadTerritoryGeometryModule()
if (cancelled) return
layerRef.current?.remove()
pathByKeyRef.current = new Map()
geometryModuleRef.current = geometryModule
const keyProperty = keyPropertyForMode(mode)
const syncMountStyleContext = () => {
styleContextRef.current = buildLayerStyleContext({
...stylePropsRef.current,
hoveredKey: null,
})
}
syncMountStyleContext()
const mountContext = styleContextRef.current!
const layer = L.geoJSON([...geometryModule.features], {
style: (feature?: Feature) => {
const properties = feature?.properties as Record<string, string> | undefined
const key = featureKeyFromProperties(properties, keyProperty)
const context = styleContextRef.current ?? mountContext
return resolvePathStyleForFeature(context, key ?? '')
},
onEachFeature: (feature, featureLayer) => {
if (!(featureLayer instanceof L.Path)) return
const properties = feature.properties as Record<string, string> | undefined
const key = featureKeyFromProperties(properties, keyProperty)
if (!key) return
const name = properties?.name ?? key
pathByKeyRef.current.set(key, featureLayer)
featureLayer.on('mouseover', (event) => {
if (!isKeyInteractive(key)) return
const pointerType = getPointerType(event)
if (pointerType === 'mouse') {
touchPinnedRef.current = false
} else if (touchPinnedRef.current) {
return
}
if (styleContextRef.current?.hoveredKey === key) return
setHoveredKey(key, featureLayer)
onFeatureSelectRef.current?.({ key, name })
})
featureLayer.on('mouseout', () => {
if (touchPinnedRef.current) return
if (!isKeyInteractive(key)) return
setHoveredKey(null, undefined, true)
onFeatureSelectRef.current?.(null)
})
featureLayer.on('click', (event) => {
if (!isKeyInteractive(key)) return
touchPinnedRef.current = getPointerType(event, 'mouse') === 'touch'
setHoveredKey(key, featureLayer)
onFeatureActivateRef.current?.(key)
onFeatureSelectRef.current?.({ key, name })
})
},
})
layer.addTo(map)
layerRef.current = layer
map.invalidateSize()
syncMountStyleContext()
restyleAllPaths()
const fitIdentity = `${mode}:${viewportKey}`
fitMapToHighlights(map, mode, buildHighlightSet(viewportKey), geometryModule)
lastFittedViewportKeyRef.current = fitIdentity
geometryReadyRef.current = true
setStatus('ready')
} catch {
if (!cancelled) setStatus('error')
}
}
void renderLayer()
return () => {
cancelled = true
layerRef.current?.remove()
layerRef.current = null
pathByKeyRef.current = new Map()
geometryModuleRef.current = null
geometryReadyRef.current = false
}
// Geometry layer rebuilds only when `mode` changes; metric/hover updates use separate effects.
// eslint-disable-next-line react-hooks/exhaustive-deps -- intentional [mode]-only mount
}, [mode])
useEffect(() => {
if (!geometryReadyRef.current) return
const fitIdentity = `${mode}:${viewportKey}`
if (lastFittedViewportKeyRef.current === fitIdentity) return
const map = mapRef.current
const geometryModule = geometryModuleRef.current
if (!map || !geometryModule || !layerRef.current) return
fitMapToHighlights(map, mode, buildHighlightSet(viewportKey), geometryModule)
lastFittedViewportKeyRef.current = fitIdentity
}, [viewportKey, mode])
useEffect(() => {
if (!interactiveKey || pathByKeyRef.current.size === 0) return
restyleAllPaths()
// Restyle helpers only read refs and are recreated per render — depending
// on them would re-run this effect on every render.
// eslint-disable-next-line react-hooks/exhaustive-deps -- interactiveKey-only restyle
}, [interactiveKey])
useEffect(() => {
const previousContext = styleContextRef.current
const context = buildLayerStyleContext({
values,
fillMode,
scaleMax,
fillByKey,
highlightKey,
selectedKey,
hoveredKey: previousContext?.hoveredKey ?? null,
})
styleContextRef.current = context
if (pathByKeyRef.current.size === 0) return
restyleAllPaths()
// `selectedKey` changes use the dedicated 2-path effect below (not full O(n) restyle).
// eslint-disable-next-line react-hooks/exhaustive-deps -- selectedKey excluded on purpose
}, [values, fillMode, scaleMax, fillByKey, highlightKey, mode])
useEffect(() => {
const map = mapRef.current
if (!map || !bubbleValues || status !== 'ready') return
const maxValue = choroplethMaxValue(bubbleValues)
if (maxValue <= 0) return
if (!map.getPane(BUBBLE_PANE)) {
const pane = map.createPane(BUBBLE_PANE)
pane.style.zIndex = '450'
pane.style.pointerEvents = 'none'
}
// Smallest first so the big prizes end up drawn on top of the crowd
// instead of being covered by whichever key the object happened to list last.
const ordered = Object.entries(bubbleValues)
.filter(([, value]) => value > 0)
.sort(([, left], [, right]) => left - right)
const markers: L.CircleMarker[] = []
for (const [key, value] of ordered) {
const path = pathByKeyRef.current.get(key)
const fillColor = bubbleFillByKey?.[key]
if (!(path instanceof L.Polygon) || !fillColor) continue
markers.push(
// Polygon centroid, not the bounding box's centre: a concave or
// L-shaped município would put a box centre outside its own outline.
L.circleMarker(path.getCenter(), {
pane: BUBBLE_PANE,
radius: bubbleRadius(value, maxValue),
fillColor,
fillOpacity: 0.8,
color: '#ffffff',
weight: 0.75,
// Deliberately not interactive: every bubble sits on top of its own
// município, so letting the pointer fall through keeps ONE hover
// target and one readout instead of two stacked ones.
interactive: false,
}),
)
}
const group = L.layerGroup(markers).addTo(map)
return () => {
group.remove()
}
// `mode` rebuilds the geometry layer this reads centroids from, so the
// markers must be rebuilt with it or they would sit at stale positions.
}, [bubbleValues, bubbleFillByKey, status, mode])
useEffect(() => {
const context = styleContextRef.current
if (!context || pathByKeyRef.current.size === 0) {
if (context) context.selectedKey = selectedKey
return
}
if (context.selectedKey === selectedKey) return
const previousSelected = context.selectedKey
context.selectedKey = selectedKey
if (context.hoveredKey) return
if (previousSelected) {
restyleFeature(previousSelected)
}
if (selectedKey) {
restyleFeature(selectedKey)
}
// Restyle helpers only read refs and are recreated per render — depending
// on them would re-run this effect on every render.
// eslint-disable-next-line react-hooks/exhaustive-deps -- selectedKey-only O(2) restyle
}, [selectedKey])
return (
<div className={cn('relative w-full', className)}>
<div
ref={containerRef}
role="img"
aria-label={ariaLabel}
aria-busy={status === 'loading'}
className={cn(
'w-full overflow-hidden rounded-lg border border-border bg-muted/30 [&_.leaflet-interactive]:cursor-pointer',
heightClassName,
)}
/>
{status === 'loading' ? (
<div className="absolute inset-0 flex items-center justify-center" role="status">
<span className="flex items-center gap-2 rounded-md bg-background/85 px-3 py-2 text-sm text-muted-foreground shadow-sm">
<Spinner aria-hidden="true" />
Carregando mapa…
</span>
</div>
) : null}
{status === 'error' ? (
<p className="absolute inset-0 flex items-center justify-center bg-background/80 px-4 text-center text-sm text-muted-foreground">
Não foi possível carregar o mapa. Tente recarregar a página.
</p>
) : null}
</div>
)
}