SPB Git

Repository Public

City.tsx 8.4 KB 193 lines tsx

1// Une ville : chiffres réels, grandeurs disponibles, logements, villes voisines.23import { Link, useParams } from "react-router";4import { useQuery } from "@tanstack/react-query";5import { ArrowRight, Map as MapIcon } from "lucide-react";6import { fetchVille, priceOk } from "@/lib/api";7import { cn } from "@/lib/cn";8import { cleanCity, fmtInt, fmtMoney, plural } from "@/lib/format";9import { useTitle } from "@/lib/hooks";10import { LoukaEmptyState } from "@/brand/EmptyState";11import { ErrorState, Facts, SectionHead } from "@/components/annuaire/shared";12import { ListingCard, ListingCardSkeleton } from "@/components/ListingCard";13import { ButtonLink, Skeleton } from "@/ui/primitives";1415const GRID = "lk-stagger grid grid-cols-1 gap-x-5 gap-y-8 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4";1617export default function City() {18  const { ville = "", type } = useParams();19  const q = useQuery({20    queryKey: ["ville", ville, type ?? ""],21    queryFn: () => fetchVille(ville, type),22    staleTime: 3e5,23    retry: false,24  });25  const d = q.data;26  const city = cleanCity(d?.city);27  const unit = d?.unit_type ?? null;2829  useTitle(city ? `${unit ? `${unit} à louer` : "Logements à louer"} à ${city}` : "Logements à louer par ville");3031  if (q.isError) {32    const missing = /404/.test(String(q.error));33    return (34      <div className="container-x py-10">35        {missing ? (36          <LoukaEmptyState37            kind="closed"38            title="Ville introuvable"39            text="Aucun logement n'est répertorié sous ce nom en ce moment. Parcourez la liste des villes ou cherchez directement sur la carte."40            action={41              <div className="flex flex-wrap justify-center gap-2">42                <ButtonLink to="/villes" variant="primary">43                  Toutes les villes44                </ButtonLink>45                <ButtonLink to="/recherche" variant="outline">46                  Explorer la carte47                </ButtonLink>48              </div>49            }50          />51        ) : (52          <ErrorState onRetry={() => q.refetch()} />53        )}54      </div>55    );56  }5758  const mapHref = d ? `/recherche?city=${encodeURIComponent(d.city)}${unit ? `&unit_type=${encodeURIComponent(unit)}` : ""}` : "/recherche";59  const types = d?.types ?? [];60  const neighbors = (d?.neighbors ?? []).filter((n) => cleanCity(n.city));61  const more = d ? d.n - d.listings.length : 0;62  // les chiffres de la ville décrivent toutes les grandeurs ; avec un filtre, on le dit63  const shown = d && unit ? types.find((t) => t.slug === type)?.n : d?.n;6465  return (66    <>67      <header className="container-x pb-7 pt-6 lg:pt-10">68        <Link to="/villes" className="mb-3 inline-flex min-h-11 items-center gap-1.5 text-[0.88rem] text-ink-3 transition-colors duration-150 hover:text-ink">69          <span aria-hidden>←</span> Toutes les villes70        </Link>71        {q.isPending ? (72          <div className="min-h-[21rem] space-y-4 sm:min-h-[11.5rem]">73            <Skeleton className="h-10 w-3/4 max-w-xl" />74            <Skeleton className="h-5 w-1/2 max-w-sm" />75          </div>76        ) : (77          <div className="flex flex-wrap items-end justify-between gap-x-10 gap-y-5">78            <div className="min-w-0 max-w-2xl">79              <p className="eyebrow">{unit ? `${unit} à louer` : "Logements à louer"}</p>80              <h1 className="mt-2.5 text-[1.85rem] leading-[1.08] sm:text-[2.5rem]">{unit ? `${unit} à louer à ${city}` : `Logements à louer à ${city}`}</h1>81            </div>82            <ButtonLink to={mapHref} variant="primary">83              <MapIcon className="size-4" /> Voir sur la carte84            </ButtonLink>85          </div>86        )}87        {d && (88          <Facts89            className="mt-6"90            items={[91              { value: fmtInt(d.n), label: `${d.n > 1 ? "logements disponibles" : "logement disponible"}${unit ? " dans la ville" : ""}` },92              ...(unit && shown != null ? [{ value: fmtInt(shown), label: unit }] : []),93              ...(priceOk(d.med) ? [{ value: fmtMoney(d.med), label: `loyer médian${unit ? ", toutes grandeurs" : ""}` }] : []),94              ...(priceOk(d.avg) ? [{ value: fmtMoney(d.avg), label: "loyer moyen" }] : []),95            ]}96          />97        )}98      </header>99100      <div className="container-x space-y-14">101        {types.length > 1 && (102          <nav103            aria-label="Grandeurs disponibles"104            className="no-scrollbar -mx-[clamp(1rem,4vw,2.5rem)] flex gap-1.5 overflow-x-auto px-[clamp(1rem,4vw,2.5rem)] lg:mx-0 lg:flex-wrap lg:px-0"105          >106            <TypeLink to={`/ville/${ville}`} active={!type} label="Toutes" n={d?.n} />107            {types.map((t) => (108              <TypeLink key={t.slug} to={`/ville/${ville}/${t.slug}`} active={type === t.slug} label={t.unit_type} n={t.n} />109            ))}110          </nav>111        )}112113        <section aria-label="Logements" className={types.length > 1 ? "!mt-6" : ""}>114          {q.isPending ? (115            <div className={GRID}>116              {Array.from({ length: 8 }, (_, i) => (117                <ListingCardSkeleton key={i} />118              ))}119            </div>120          ) : d && d.listings.length ? (121            <>122              <div className={GRID}>123                {d.listings.map((l, i) => (124                  <ListingCard key={l.uid} listing={l} eager={i < 4} />125                ))}126              </div>127              <div className="mt-10 flex flex-wrap items-center justify-between gap-4 border-t border-line pt-5">128                <p className="num text-[0.9rem] text-ink-3">129                  {more > 0130                    ? `${plural(more, "autre logement", "autres logements")} à ${city}, avec filtres et carte interactive.`131                    : `Tous les logements répertoriés à ${city} sont affichés.`}132                </p>133                <ButtonLink to={mapHref} variant={more > 0 ? "primary" : "outline"}>134                  {more > 0 ? `Voir les ${fmtInt(d.n)} logements` : "Ouvrir la carte"} <ArrowRight className="size-4" />135                </ButtonLink>136              </div>137            </>138          ) : (139            <LoukaEmptyState140              kind="closed"141              title="Aucun logement pour cette grandeur"142              text="L'offre change plusieurs fois par jour. Consultez les autres grandeurs ou une ville voisine."143              action={144                <ButtonLink to={`/ville/${ville}`} variant="outline">145                  Tous les logements à {city}146                </ButtonLink>147              }148            />149          )}150        </section>151152        {neighbors.length > 0 && (153          <section aria-labelledby="voisines">154            <SectionHead id="voisines" title="D'autres villes" text="Nombre de logements à louer et loyer moyen affiché." to="/villes" cta="Toutes les villes" />155            <ul className="mt-3 grid grid-cols-1 gap-x-12 sm:grid-cols-2 lg:grid-cols-3">156              {neighbors.map((n) => (157                <li key={n.slug}>158                  <Link159                    to={`/ville/${n.slug}`}160                    className="group flex min-h-12 items-baseline justify-between gap-4 border-b border-line py-3 transition-colors duration-150 hover:border-ink-3"161                  >162                    <span className="truncate font-display text-[1.12rem] transition-colors duration-150 group-hover:text-brand">{cleanCity(n.city)}</span>163                    <span className="num flex shrink-0 items-baseline gap-3 text-[0.84rem] text-ink-3">164                      <span>{fmtInt(n.n)}</span>165                      <span className="w-[4.4rem] text-right font-medium text-ink">{priceOk(n.avg_price) ? fmtMoney(n.avg_price) : "—"}</span>166                    </span>167                  </Link>168                </li>169              ))}170            </ul>171          </section>172        )}173      </div>174    </>175  );176}177178function TypeLink({ to, active, label, n }: { to: string; active: boolean; label: string; n?: number }) {179  return (180    <Link181      to={to}182      aria-current={active ? "page" : undefined}183      className={cn(184        "inline-flex h-11 shrink-0 items-center gap-1.5 rounded-[8px] border px-3 text-[0.88rem] transition-colors duration-150 lg:h-9",185        active ? "border-ink bg-ink font-medium text-bg" : "border-line-2 bg-surface text-ink-2 hover:border-ink-3 hover:text-ink",186      )}187    >188      {label}189      {n != null && <span className={cn("num text-[0.78rem]", active ? "opacity-70" : "text-ink-3")}>{fmtInt(n)}</span>}190    </Link>191  );192}193
Files main
Menu

Theme