Repository Public
1// Mini-carte de la fiche : le logement (symbole de la porte) et ce qui2// l'entoure, sur un fond calme. Pas de zoom à la molette : la page défile.34import { useEffect, useRef, useState } from "react";5import type { Map as MbMap, Marker } from "mapbox-gl";6import type { Theme } from "@/lib/store";7import { BRAND, DOOR_PATHS } from "@/brand/geometry";8import { createMap, loadMapbox, mapSupported, presetFor, setPreset } from "./mapbox";9import "./map.css";1011export interface FichePoint {12 key: string;13 lng: number;14 lat: number;15 label: string;16 /** couleur CSS du point */17 color: string;18}1920/** Marqueur du logement : l'épingle de la porte (géométrie de brand/geometry.ts), aux couleurs du thème. */21function homeEl() {22 const wrap = document.createElement("div");23 wrap.style.zIndex = "4";24 const el = document.createElement("div");25 el.className = "lk-pin";26 el.innerHTML =27 `<svg width="27" height="27" viewBox="0 0 64 64" aria-hidden="true"><path d="${DOOR_PATHS.frame}" fill="none" stroke="${BRAND.orange}" stroke-width="5" stroke-linejoin="round"/>` +28 `<path d="${DOOR_PATHS.panel}" fill="var(--door)"/><circle cx="37.6" cy="34.5" r="1.9" fill="${BRAND.orange}"/>` +29 `<path d="${DOOR_PATHS.threshold}" stroke="var(--door)" stroke-width="3" stroke-linecap="round" fill="none"/></svg>`;30 wrap.appendChild(el);31 return wrap;32}3334function dotEl(p: FichePoint) {35 const el = document.createElement("button");36 el.type = "button";37 el.title = p.label;38 el.setAttribute("aria-label", p.label);39 el.dataset.key = p.key;40 el.style.cssText = `width:12px;height:12px;border-radius:50%;background:${p.color};border:2px solid var(--surface);box-shadow:0 1px 3px rgba(24,28,26,.35);cursor:pointer;padding:0;`;41 return el;42}4344export function FicheMap({45 lng,46 lat,47 points,48 activeKey,49 theme,50 onReady,51 className,52}: {53 lng: number;54 lat: number;55 points: FichePoint[];56 activeKey?: string | null;57 theme: Theme;58 onReady?: (map: MbMap) => void;59 className?: string;60}) {61 const el = useRef<HTMLDivElement>(null);62 const mapRef = useRef<MbMap | null>(null);63 const markers = useRef<Map<string, Marker>>(new Map());64 const [ready, setReady] = useState(false);65 const [failed, setFailed] = useState(false);66 const readyCb = useRef(onReady);67 readyCb.current = onReady;6869 useEffect(() => {70 if (!el.current) return;71 if (!mapSupported()) {72 setFailed(true);73 return;74 }75 let dead = false;76 let map: MbMap | null = null;77 Promise.all([78 createMap(el.current, presetFor(theme), {79 center: [lng, lat],80 zoom: 14.8,81 pitch: 0,82 bearing: 0,83 scrollZoom: false,84 dragRotate: true,85 maxBounds: undefined,86 }),87 loadMapbox(),88 ])89 .then(([m, mapboxgl]) => {90 if (dead) return m.remove();91 map = m;92 mapRef.current = m;93 m.on("error", (e) => {94 const status = (e.error as { status?: number } | undefined)?.status;95 if (status === 401 || status === 403) setFailed(true);96 });97 new mapboxgl.Marker({ element: homeEl(), anchor: "bottom", offset: [0, -8] }).setLngLat([lng, lat]).addTo(m);98 m.on("style.load", () => {99 setReady(true);100 readyCb.current?.(m);101 });102 })103 .catch(() => setFailed(true));104 return () => {105 dead = true;106 markers.current.clear();107 map?.remove();108 mapRef.current = null;109 };110 // la carte est créée une fois par adresse111 // eslint-disable-next-line react-hooks/exhaustive-deps112 }, [lng, lat]);113114 useEffect(() => {115 const m = mapRef.current;116 if (ready && m) setPreset(m, presetFor(theme));117 }, [theme, ready]);118119 // points d'intérêt120 useEffect(() => {121 const m = mapRef.current;122 if (!ready || !m) return;123 let cancelled = false;124 loadMapbox().then((mapboxgl) => {125 if (cancelled) return;126 markers.current.forEach((mk) => mk.remove());127 markers.current.clear();128 for (const p of points) {129 const mk = new mapboxgl.Marker({ element: dotEl(p), anchor: "center" })130 .setLngLat([p.lng, p.lat])131 .setPopup(new mapboxgl.Popup({ offset: 12, closeButton: false, maxWidth: "240px" }).setHTML(132 `<div style="background:var(--surface);color:var(--ink);border:1px solid var(--line-2);border-radius:8px;padding:6px 10px;font-size:13px;font-weight:500;box-shadow:var(--shadow-lg)">${p.label.replace(/[<>&]/g, "")}</div>`,133 ))134 .addTo(m);135 markers.current.set(p.key, mk);136 }137 });138 return () => {139 cancelled = true;140 };141 }, [points, ready]);142143 // mise en évidence depuis la liste144 useEffect(() => {145 markers.current.forEach((mk, key) => {146 const node = mk.getElement();147 node.style.transform = `${node.style.transform.replace(/ scale\([^)]*\)/, "")}`;148 const inner = node as HTMLElement;149 inner.style.zIndex = key === activeKey ? "5" : "";150 inner.style.outline = key === activeKey ? "2px solid var(--accent)" : "";151 inner.style.outlineOffset = key === activeKey ? "2px" : "";152 });153 }, [activeKey, points]);154155 if (failed) {156 return (157 <div className={className}>158 <div className="grid size-full place-items-center bg-surface-2 p-6 text-center text-sm text-ink-3">La carte ne peut pas s'afficher dans ce navigateur.</div>159 </div>160 );161 }162 return (163 <div className={className}>164 <div ref={el} className="size-full" />165 </div>166 );167}168