diff --git a/src/components/Map/MapView.jsx b/src/components/Map/MapView.jsx index 08c2e2f..ae1011a 100644 --- a/src/components/Map/MapView.jsx +++ b/src/components/Map/MapView.jsx @@ -176,7 +176,7 @@ export default function MapView({ theme = "dark" }) { { title: draftTitle.trim(), snippet: draftBody.trim() || draftTitle.trim(), - category: draftCategory.toUpperCase(), + category: (draftCategory === "Events" ? "EVENT" : draftCategory.toUpperCase()), sub_category: draftSubCategory || "ALL", lat, lon: lng, diff --git a/src/components/Map/mapFilter.js b/src/components/Map/mapFilter.js index 941d880..4dd2889 100644 --- a/src/components/Map/mapFilter.js +++ b/src/components/Map/mapFilter.js @@ -8,7 +8,7 @@ export function categoryCode(mainFilter) { case "Friends": return "FRIENDS"; case "Events": - return "EVENT"; // backend: EVENT + return "EVENT"; // backend attendu case "Market": return "MARKET"; case "All": @@ -40,6 +40,29 @@ export function matchesSubFilter(post, subFilter) { return sub === subFilter.toLowerCase(); } +// Parse date backend safely: +// - "YYYY-MM-DD HH:MM:SS" => treated as LOCAL (pas de "Z" qui décale) +// - ISO string garde ce que JS sait faire +function parseCreatedAt(createdAt) { + if (!createdAt) return null; + + if (createdAt instanceof Date) return createdAt; + + const s = String(createdAt).trim(); + if (!s) return null; + + // "YYYY-MM-DD HH:MM:SS" (sans timezone) + if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(s)) { + // local time + const isoLocal = s.replace(" ", "T"); + const d = new Date(isoLocal); + return Number.isNaN(d.getTime()) ? null : d; + } + + const d = new Date(s); + return Number.isNaN(d.getTime()) ? null : d; +} + // time filter local – règles demandées : // NOW = dernière 1h // TODAY = dernière 24h @@ -47,26 +70,20 @@ export function matchesSubFilter(post, subFilter) { // PAST = ALL (pas de filtre de temps) export function matchesTimeFilter(createdAt, timeFilter) { if (!timeFilter) return true; - if (!createdAt) return true; - - let d; - if (typeof createdAt === "string") { - // backend renvoie souvent "YYYY-MM-DD HH:MM:SS" - if (createdAt.includes(" ")) { - d = new Date(createdAt.replace(" ", "T") + "Z"); - } else { - d = new Date(createdAt); - } - } else { - d = new Date(createdAt); - } - if (Number.isNaN(d.getTime())) return true; // PAST = ALL => aucun filtre temps if (timeFilter === "PAST") return true; - const now = new Date(); - const diffMs = now.getTime() - d.getTime(); + const d = parseCreatedAt(createdAt); + if (!d) return true; + + const now = Date.now(); + const diffMs = now - d.getTime(); + if (!Number.isFinite(diffMs)) return true; + + // si date future (clock/UTC weird) -> on laisse passer + if (diffMs < 0) return true; + const diffHours = diffMs / 3600000; const diffDays = diffMs / 86400000; diff --git a/src/components/Map/useMapCore.js b/src/components/Map/useMapCore.js index e4ff83e..0ccd14a 100644 --- a/src/components/Map/useMapCore.js +++ b/src/components/Map/useMapCore.js @@ -11,16 +11,13 @@ function getBaseStyle(theme) { switch (t) { case "light": - // fond clair tiles = ["https://a.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png"]; break; case "blue": - // style plus coloré tiles = ["https://a.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png"]; break; case "dark": default: - // fond sombre (comme avant) tiles = ["https://a.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png"]; break; } @@ -49,7 +46,6 @@ export function useMapCore(theme) { const containerRef = useRef(null); const mapRef = useRef(null); - // pour les markers (utilisé aussi par usePostsEngine + MapView) const markersRef = useRef([]); const expandedElRef = useRef(null); @@ -59,7 +55,14 @@ export function useMapCore(theme) { }); const [hasLastView, setHasLastView] = useState(false); - // création de la map + function closeExpandedIfAny() { + const el = expandedElRef.current; + if (el && el.__setExpanded) { + el.__setExpanded(false); + } + expandedElRef.current = null; + } + useEffect(() => { if (!containerRef.current) return; @@ -89,9 +92,7 @@ export function useMapCore(theme) { hadLastView = true; } } - } catch { - // ignore - } + } catch {} setHasLastView(hadLastView); map.addControl(new maplibregl.NavigationControl(), "top-right"); @@ -115,21 +116,21 @@ export function useMapCore(theme) { zoom: map.getZoom(), }) ); - } catch { - // ignore - } + } catch {} }); + // IMPORTANT: click/drag sur la map => fermer le gros post + map.on("click", () => closeExpandedIfAny()); + map.on("dragstart", () => closeExpandedIfAny()); + return () => { clearAllMarkers(markersRef, expandedElRef); map.remove(); mapRef.current = null; }; - // on ne met PAS theme ici, sinon la map serait recréée // eslint-disable-next-line react-hooks/exhaustive-deps }, []); - // quand le thème change → on change juste le style de la map useEffect(() => { const map = mapRef.current; if (!map) return;