"use client"; import { geoMercator, geoPath } from "d3-geo"; import type { FeatureCollection, Geometry } from "geojson"; import { useEffect, useMemo, useState } from "react"; /** Mini-carte de localisation : la circonscription en accent sur la silhouette du Québec + zoom local. */ export function RidingMap({ code }: { code: number }) { const [geo, setGeo] = useState | null>(null); useEffect(() => { fetch("/api/geo").then((r) => r.json()).then(setGeo).catch(() => {}); }, []); const svg = useMemo(() => { if (!geo) return null; const W = 360, H = 300; const proj = geoMercator().fitExtent([[6, 6], [W - 6, H - 6]], geo); const path = geoPath(proj); const f = geo.features.find((x) => x.properties.code === code); if (!f) return null; const [[x0, y0], [x1, y1]] = path.bounds(f); const local = geoMercator().fitExtent([[8, 8], [W - 8, H - 8]], { type: "FeatureCollection", features: [f] } as FeatureCollection); const lp = geoPath(local); const neighbors = geo.features.filter((g) => { const b = lp.bounds(g); return b[0][0] < W && b[1][0] > 0 && b[0][1] < H && b[1][1] > 0; }); return { W, H, all: geo.features.map((g) => path(g) ?? ""), me: path(f) ?? "", box: [x0, y0, x1 - x0, y1 - y0], localMe: lp(f) ?? "", neighbors: neighbors.map((g) => lp(g) ?? "") }; }, [geo, code]); if (!svg) return
; return (
{svg.all.map((d, i) => )} {svg.neighbors.map((d, i) => )}
); }