SPB Git

Repository Public

Overview.tsx 11.5 KB 254 lines tsx

1// Aperçu : ce qu'on veut savoir en arrivant. Prix, lieu, pièces,2// caractéristiques, disponibilité, puis les actions. Résumé collant (grand3// écran) et barre d'action (mobile).45import type { ReactNode } from "react";6import { Link } from "react-router";7import { ArrowUpRight, Download, Heart, Scale, Share2 } from "lucide-react";8import { priceOk, type FairValueDetail, type Listing } from "@/lib/api";9import { cn } from "@/lib/cn";10import { cleanCity, fmtAgo, fmtAvailability, fmtInt, fmtMoney } from "@/lib/format";11import { prettySource, useSourceNames } from "@/lib/hooks";12import { toggleCompare, toggleFavorite, useCompare, useIsFavorite } from "@/lib/store";13import { VerdictPill } from "@/components/Verdict";14import { buttonClass } from "@/ui/primitives";15import { toast } from "@/ui/toast";1617export const bathroomsOf = (l: Listing) => l.bathrooms ?? null;1819export function placeOf(l: Listing): string {20  const city = cleanCity(l.city);21  const sector = (l.sector || "").replace(/[()]/g, "").trim();22  return [sector && sector !== city ? sector : null, city].filter(Boolean).join(", ");23}2425export function titleOf(l: Listing): string {26  return l.title || l.address || "Logement à louer";27}2829export async function share(l: Listing) {30  const url = window.location.href;31  const title = `${titleOf(l)} · Lou-Ka`;32  try {33    if (navigator.share) {34      await navigator.share({ title, url });35      return;36    }37    await navigator.clipboard.writeText(url);38    toast("Lien copié");39  } catch {40    /* partage annulé */41  }42}4344/** « 4½ · 2 chambres · 1 salle de bain · 947 pi² » */45export function specLine(l: Listing): string {46  const sdb = bathroomsOf(l);47  return [48    l.unit_type || null,49    l.bedrooms != null && l.bedrooms > 0 ? `${Math.round(l.bedrooms)} chambre${l.bedrooms > 1 ? "s" : ""}` : null,50    sdb != null ? `${sdb} salle${sdb > 1 ? "s" : ""} de bain` : null,51    l.area_sqft ? `${fmtInt(l.area_sqft)} pi²` : null,52  ]53    .filter(Boolean)54    .join(" · ");55}5657// --- Actions ------------------------------------------------------------------58const actionBase =59  "inline-flex h-10 shrink-0 items-center justify-center gap-2 rounded-[10px] border border-line-2 text-[0.86rem] font-medium text-ink transition-colors duration-150 hover:border-ink-3 active:scale-[0.98]";60const action = `${actionBase} px-3.5`;61const actionIcon = `${actionBase} w-10`;6263/** Favori : le cœur universel, avec un léger rebond à l'ajout. */64export function SaveAction({ l, compact }: { l: Listing; compact?: boolean }) {65  const fav = useIsFavorite(l.uid);66  return (67    <button68      type="button"69      aria-pressed={fav}70      aria-label={fav ? "Retirer des favoris" : "Ajouter aux favoris"}71      onClick={(e) => {72        const added = toggleFavorite(l);73        const icon = e.currentTarget.querySelector("svg");74        if (added) icon?.animate([{ transform: "scale(1)" }, { transform: "scale(1.3)" }, { transform: "scale(1)" }], { duration: 260, easing: "cubic-bezier(0.2,0.7,0.2,1)" });75        toast(added ? "Ajouté à vos favoris" : "Retiré des favoris");76      }}77      className={cn(compact ? actionIcon : action, fav && "border-accent")}78    >79      <Heart className={cn("size-4", fav && "text-accent")} fill={fav ? "currentColor" : "none"} />80      {!compact && (fav ? "Enregistré" : "Enregistrer")}81    </button>82  );83}8485export function CompareAction({ uid, compact }: { uid: string; compact?: boolean }) {86  const on = useCompare().includes(uid);87  return (88    <button89      type="button"90      aria-pressed={on}91      aria-label={on ? "Retirer du comparateur" : "Ajouter au comparateur"}92      onClick={() => {93        const r = toggleCompare(uid);94        toast(r === "full" ? "Le comparateur contient déjà 4 logements" : r === "added" ? "Ajouté au comparateur" : "Retiré du comparateur");95      }}96      className={cn(compact ? actionIcon : action, on && "border-brand text-brand")}97    >98      <Scale className="size-4" />99      {!compact && (on ? "Dans le comparateur" : "Comparer")}100    </button>101  );102}103104function Fact({ k, v }: { k: string; v: ReactNode }) {105  return (106    <div className="border-t border-line pt-2.5">107      <dt className="text-[0.78rem] text-ink-3">{k}</dt>108      <dd className="mt-0.5 text-[0.96rem] font-medium text-ink">{v}</dd>109    </div>110  );111}112113function KeyFacts({ l }: { l: Listing }) {114  const dispo = fmtAvailability(l.availability_date, true);115  const sdb = bathroomsOf(l);116  const items: [string, string][] = [];117  if (l.unit_type) items.push(["Grandeur", l.unit_type]);118  if (l.bedrooms != null) items.push([l.bedrooms > 1 ? "Chambres" : "Chambre", l.bedrooms === 0 ? "Aire ouverte" : String(Math.round(l.bedrooms))]);119  if (sdb != null) items.push([sdb > 1 ? "Salles de bain" : "Salle de bain", String(sdb)]);120  if (l.area_sqft) items.push(["Superficie", `${fmtInt(l.area_sqft)} pi²`]);121  if (dispo) items.push(["Disponibilité", dispo]);122  if (l.pets) items.push(["Animaux", l.pets === "oui" ? "Acceptés" : l.pets === "non" ? "Refusés" : "Sous conditions"]);123  if (l.furnished != null) items.push(["Ameublement", l.furnished ? "Meublé" : "Non meublé"]);124  if (l.ks_global != null) items.push(["Score de quartier", `${Math.round(l.ks_global)} sur 100`]);125  if (!items.length) return null;126  return (127    <dl className="mt-6 grid grid-cols-2 gap-x-6 gap-y-4 sm:grid-cols-4">128      {items.slice(0, 8).map(([k, v]) => (129        <Fact key={k} k={k} v={<span className="num">{v}</span>} />130      ))}131    </dl>132  );133}134135export function Overview({ l, fv }: { l: Listing; fv: FairValueDetail | undefined }) {136  const names = useSourceNames();137  const ok = priceOk(l.price);138  const verdict = l.fv_verdict ?? fv?.verdict ?? null;139  const dups = l.dup_sources ?? [];140  const fresh = l.active !== 0 && !!l.first_seen && Date.now() / 1000 - l.first_seen < 3 * 86400;141  // l'adresse n'est répétée que si elle ajoute quelque chose au titre et au lieu142  const place = placeOf(l);143  const address = l.address && l.address !== l.title && l.address !== place && l.address !== cleanCity(l.city) ? l.address : null;144  return (145    <section id="apercu" data-section="apercu" className="scroll-mt-32">146      <div className="flex flex-wrap items-center gap-x-3 gap-y-2">147        <VerdictPill verdict={verdict} deviation={l.fv_deviation} />148        {fresh && <span className="rounded-[6px] bg-accent-soft px-2 py-1 text-[0.74rem] font-medium leading-none text-accent-strong">Nouvelle annonce</span>}149        <span className="text-[0.82rem] text-ink-3">Mise à jour {fmtAgo(l.updated_at)}</span>150      </div>151152      <p className="num mt-4 text-[2.1rem] font-semibold leading-none tracking-[-0.02em] text-ink sm:text-[2.5rem]">153        {ok ? fmtMoney(l.price) : <span className="text-[1.5rem] tracking-normal">{l.price_label || "Prix sur demande"}</span>}154        {ok && <span className="ml-2 text-[0.95rem] font-normal tracking-normal text-ink-3">par mois{l.details?.price_from ? ", à partir de" : ""}</span>}155      </p>156      <h1 tabIndex={-1} data-fiche-title className="mt-3 text-[1.55rem] leading-[1.15] outline-none sm:text-[1.95rem]">157        {titleOf(l)}158      </h1>159      <p className="mt-1.5 text-[0.98rem] text-ink-2">{[address, place].filter(Boolean).join(" · ")}</p>160161      <KeyFacts l={l} />162163      <div className="mt-6 flex flex-wrap items-center gap-2">164        <SaveAction l={l} />165        <CompareAction uid={l.uid} />166        <button type="button" onClick={() => share(l)} className={action}>167          <Share2 className="size-4" /> Partager168        </button>169        <a href={`/api/listings/${encodeURIComponent(l.uid)}/pdf`} className={action}>170          <Download className="size-4" /> Fiche PDF171        </a>172      </div>173174      <p className="mt-5 text-[0.84rem] text-ink-3">175        Annonce publiée par{" "}176        <Link to={`/g/${l.source}`} className="text-ink-2 underline decoration-line-2 underline-offset-[3px] hover:text-ink">177          {prettySource(l.source, names)}178        </Link>179        {dups.length > 0 && <>, aussi vue sur {dups.map((d) => prettySource(d, names)).join(", ")}</>}180      </p>181    </section>182  );183}184185/** Résumé collant (grand écran) : précède la barre mobile dans le DOM. */186export function StickySummary({ l, fv }: { l: Listing; fv: FairValueDetail | undefined }) {187  const names = useSourceNames();188  const ok = priceOk(l.price);189  const verdict = l.fv_verdict ?? fv?.verdict ?? null;190  const dispo = fmtAvailability(l.availability_date, true);191  const rows: [string, string][] = [];192  if (l.unit_type) rows.push(["Grandeur", l.unit_type]);193  if (l.area_sqft) rows.push(["Superficie", `${fmtInt(l.area_sqft)} pi²`]);194  if (dispo) rows.push(["Disponibilité", dispo]);195  if (l.fv != null && ok) rows.push(["Juste valeur estimée", fmtMoney(l.fv)]);196  return (197    <div className="rounded-xl border border-line bg-surface p-5">198      <div className="flex items-start justify-between gap-3">199        <p className="num text-[1.7rem] font-semibold leading-none tracking-[-0.015em]">200          {ok ? fmtMoney(l.price) : <span className="text-[1.15rem] tracking-normal">Prix sur demande</span>}201          {ok && <span className="ml-1.5 text-[0.84rem] font-normal tracking-normal text-ink-3">/ mois</span>}202        </p>203        <VerdictPill verdict={verdict} deviation={l.fv_deviation} />204      </div>205      <p className="mt-2 truncate text-[0.88rem] text-ink-3">{[l.unit_type, placeOf(l)].filter(Boolean).join(" · ")}</p>206      {rows.length > 0 && (207        <dl className="mt-4 divide-y divide-line border-y border-line text-[0.88rem]">208          {rows.map(([k, v]) => (209            <div key={k} className="flex justify-between gap-3 py-2.5">210              <dt className="text-ink-3">{k}</dt>211              <dd className="num font-medium text-ink">{v}</dd>212            </div>213          ))}214        </dl>215      )}216      <a href={l.url} target="_blank" rel="noopener noreferrer nofollow" className={buttonClass("primary", "lg", "mt-5 w-full")}>217        Voir l'annonce originale <ArrowUpRight className="size-4" />218      </a>219      <p className="mt-3 text-[0.78rem] leading-relaxed text-ink-3">220        Vous serez dirigé vers {prettySource(l.source, names)}. Lou-Ka ne prend aucune commission.221      </p>222      <div className="mt-4 flex gap-2 border-t border-line pt-4">223        <SaveAction l={l} />224        <CompareAction uid={l.uid} compact />225        <button type="button" onClick={() => share(l)} aria-label="Partager cette fiche" className={actionIcon}>226          <Share2 className="size-4" />227        </button>228      </div>229    </div>230  );231}232233/** Barre d'action mobile, posée au-dessus de la barre d'onglets. */234export function MobileCta({ l, className }: { l: Listing; className?: string }) {235  const ok = priceOk(l.price);236  return (237    <div className={cn("lk-bar-in fixed inset-x-0 bottom-[calc(3.5rem+env(safe-area-inset-bottom))] z-40 border-t border-line bg-bg lg:hidden", className)}>238      <div className="mx-auto flex max-w-xl items-center gap-2.5 px-4 py-2.5">239        <div className="min-w-0 flex-1">240          <p className="num truncate text-[1.05rem] font-semibold leading-tight">241            {ok ? fmtMoney(l.price) : "Prix sur demande"}242            {ok && <span className="ml-1 text-[0.76rem] font-normal text-ink-3">/ mois</span>}243          </p>244          <p className="truncate text-[0.76rem] text-ink-3">{[l.unit_type, placeOf(l)].filter(Boolean).join(" · ")}</p>245        </div>246        <SaveAction l={l} compact />247        <a href={l.url} target="_blank" rel="noopener noreferrer nofollow" className={cn(buttonClass("primary", "md"), "h-10 shrink-0 px-3.5")}>248          Voir l'annonce <ArrowUpRight className="size-4" />249        </a>250      </div>251    </div>252  );253}254
Files main
Menu

Theme