"use client"; import { useCallback, useEffect, useRef, useState } from "react"; import { useRouter } from "next/navigation"; import { MapView, type MapPoint } from "@/components/map-view"; import { SearchBar } from "@/components/search-bar"; import { ListingCardView } from "@/components/listing-card"; import type { ListingCard } from "@/lib/types"; import { IconClose } from "@/components/icons"; export function CarteView() { const [market, setMarket] = useState<"achat" | "location">("achat"); const [cats, setCats] = useState([]); const [points, setPoints] = useState([]); const [sel, setSel] = useState(null); const [center, setCenter] = useState<[number, number] | undefined>([45.55, -73.65]); const [zoom, setZoom] = useState(11); const [n, setN] = useState(0); const router = useRouter(); const bboxRef = useRef<[number, number, number, number] | null>(null); const timer = useRef | null>(null); const load = useCallback((bbox: [number, number, number, number], z: number) => { bboxRef.current = bbox; if (timer.current) clearTimeout(timer.current); timer.current = setTimeout(async () => { const q = new URLSearchParams({ marche: market, bbox: bbox.join(","), format: "points", tri: "recent" }); if (cats.length) q.set("type", cats.join(",")); const r = await fetch(`/api/search?${q}`).then((r) => r.json()).catch(() => null); if (r?.points) { setN(r.points.length); setPoints(r.points); } }, 250); }, [market, cats]); useEffect(() => { if (bboxRef.current) load(bboxRef.current, zoom); }, [market, cats, load, zoom]); const pick = async (p: MapPoint) => { const r = await fetch(`/api/search?marche=${market}&q=&limit=1&bbox=${p.lng - 1e-6},${p.lat - 1e-6},${p.lng + 1e-6},${p.lat + 1e-6}`).then((r) => r.json()).catch(() => null); const item = r?.items?.find((i: ListingCard) => i.uid === p.uid) ?? r?.items?.[0]; setSel(item ?? null); }; const CATS = market === "location" ? [["logement", "Logements"], ["condo", "Condos"], ["maison", "Maisons"]] : [["maison", "Maisons"], ["condo", "Condos"], ["plex", "Plex"], ["terrain", "Terrains"]]; return (
{ setZoom(z); load(b, z); }} />
{ const r = await fetch(`/api/suggest?q=${encodeURIComponent(city)}`).then((r) => r.json()); const pl = r.places?.[0]; const c = await fetch(`/api/search?marche=${market}&ville=${encodeURIComponent(pl?.name ?? city)}&limit=1&format=points`).then((r) => r.json()); const p = c.points?.[0]; if (p) { setCenter([p.lat, p.lng]); setZoom(12); } }} />
{CATS.map(([v, l]) => )}{n >= 2000 ? "2 000+" : n.toLocaleString("fr-CA")} dans la zone{n >= 2000 ? " ยท zoomez pour tout voir" : ""}
{sel && (
)}
); }