// Entreprise panel v2 — dramatic header, kinetic info cells const { useState, useEffect, useRef } = React; const GEO_CACHE2 = new Map(); let LAST_GEO_REQ2 = 0; async function geocode2(addr, postalCode) { if (!addr) return null; const cacheKey = addr + '|' + (postalCode || ''); if (GEO_CACHE2.has(cacheKey)) return GEO_CACHE2.get(cacheKey); const wait = Math.max(0, 1100 - (Date.now() - LAST_GEO_REQ2)); if (wait) await new Promise(r => setTimeout(r, wait)); LAST_GEO_REQ2 = Date.now(); try { const suffix = postalCode ? `, ${postalCode}, Canada` : ', Québec, Canada'; const q = encodeURIComponent(addr + suffix); const r = await fetch(`https://nominatim.openstreetmap.org/search?format=json&limit=1&q=${q}`, { headers: { 'Accept': 'application/json' } }); if (!r.ok) throw new Error('geo fail'); const j = await r.json(); const hit = j[0]; if (!hit) { GEO_CACHE2.set(cacheKey, null); return null; } const point = { lat: parseFloat(hit.lat), lng: parseFloat(hit.lon) }; GEO_CACHE2.set(cacheKey, point); return point; } catch (e) { GEO_CACHE2.set(cacheKey, null); return null; } } function MapView({ entreprise, onFail }) { const ref = useRef(null); const mapRef = useRef(null); const [state, setState] = useState('loading'); useEffect(() => { let cancelled = false; setState('loading'); (async () => { let point = null; // Priorité 1 : coordonnées explicites dans les données if (entreprise.latitude != null && entreprise.longitude != null) { point = { lat: entreprise.latitude, lng: entreprise.longitude }; } // Priorité 2 : géocodage de l'adresse (comportement actuel) else if (entreprise.adresse) { point = await geocode2(entreprise.adresse, entreprise.code_postal); } if (cancelled) return; if (!point) { setState('fail'); onFail?.(); return; } if (!ref.current) return; if (mapRef.current) { mapRef.current.remove(); mapRef.current = null; } const map = L.map(ref.current, { zoomControl: true, attributionControl: false, scrollWheelZoom: false }); mapRef.current = map; const regionBounds = L.latLngBounds([45.0, -75.0], [49.0, -68.0]); map.fitBounds(regionBounds, { padding: [10, 10] }); L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 16, attribution: '© OpenStreetMap', }).addTo(map); const icon = L.divIcon({ className: '', html: '
', iconSize: [24, 24], iconAnchor: [12, 12], }); L.marker([point.lat, point.lng], { icon }).addTo(map); setTimeout(() => { if (cancelled) return; map.flyTo([point.lat, point.lng], 8, { duration: 1.2 }); }, 350); setState('ready'); })(); return () => { cancelled = true; if (mapRef.current) { mapRef.current.remove(); mapRef.current = null; } }; }, [entreprise.latitude, entreprise.longitude, entreprise.adresse, entreprise.code_postal]); return ({entreprise.description}
)}