"use client"; import { useRouter, useSearchParams } from "next/navigation"; import { useCallback, useState } from "react"; import { IconFilter, IconClose } from "./icons"; const SALE_CATS = [["maison", "Maison"], ["condo", "Condo"], ["plex", "Plex"], ["terrain", "Terrain"], ["chalet", "Chalet"], ["commercial", "Commercial"]]; const RENT_CATS = [["logement", "Logement"], ["condo", "Condo"], ["maison", "Maison"], ["chambre", "Chambre"]]; const SALE_PRICES = [200000, 300000, 400000, 500000, 600000, 750000, 1000000, 1500000, 2000000]; const RENT_PRICES = [800, 1000, 1200, 1500, 1800, 2000, 2500, 3000, 4000]; export function useFilterNav() { const router = useRouter(); const sp = useSearchParams(); const set = useCallback((patch: Record) => { const n = new URLSearchParams(sp.toString()); for (const [k, v] of Object.entries(patch)) { if (v == null || v === "") n.delete(k); else n.set(k, v); } n.delete("page"); router.push(`/recherche?${n.toString()}`, { scroll: false }); }, [router, sp]); return { sp, set }; } export function FilterBar() { const { sp, set } = useFilterNav(); const rent = sp.get("marche") === "location"; const cats = (sp.get("type") ?? "").split(",").filter(Boolean); const [more, setMore] = useState(false); const toggleCat = (c: string) => set({ type: (cats.includes(c) ? cats.filter((x) => x !== c) : [...cats, c]).join(",") || null }); const PRICES = rent ? RENT_PRICES : SALE_PRICES; const active = ["type", "prix_min", "prix_max", "chambres", "sdb", "superficie", "annee"].filter((k) => sp.get(k)).length; const fmt = (v: number) => rent ? `${v.toLocaleString("fr-CA")} $` : v >= 1e6 ? `${(v / 1e6).toLocaleString("fr-CA")} M$` : `${v / 1000} k$`; return (
{(rent ? RENT_CATS : SALE_CATS).map(([v, l]) => )} {active > 0 && }
{more && (
{!rent && }
)}
); }