SPB Git

Repository Public

Verdict.tsx 4.6 KB 125 lines tsx

1// Verdict de prix : le prix demandé est comparé au « vrai prix », la valeur2// estimée de la propriété (voir vraiPrixVerdict dans lib/api).34import { cn } from "@/lib/cn";5import { fmtPct, fmtShortPrice } from "@/lib/format";67const fmtShort = (n: number) => (n < 1000 ? `${Math.round(n)} $` : `${fmtShortPrice(n)} $`);8import { useSeenOnce } from "@/lib/motion";910export type Verdict = "sous" | "marche" | "sur";1112export const VERDICT: Record<Verdict, { label: string; long: string; tone: string; soft: string; color: string }> = {13  sous: { label: "Bon prix", long: "Sous le vrai prix", tone: "text-good", soft: "bg-good-soft", color: "var(--good)" },14  marche: { label: "Prix juste", long: "Près du vrai prix", tone: "text-fair", soft: "bg-fair-soft", color: "var(--fair)" },15  sur: { label: "Élevé", long: "Au-dessus du vrai prix", tone: "text-high", soft: "bg-high-soft", color: "var(--high)" },16};1718/** Étiquette de verdict (fond teinté). */19export function VerdictPill({20  verdict,21  deviation,22  size = "sm",23  className,24}: {25  verdict: Verdict | null | undefined;26  deviation?: number | null;27  size?: "sm" | "md";28  /** conservé pour compatibilité : sans effet */29  solid?: boolean;30  className?: string;31}) {32  if (!verdict) return null;33  const v = VERDICT[verdict];34  return (35    <span36      className={cn(37        "inline-flex items-center gap-1.5 rounded-[6px] font-medium leading-none",38        size === "md" ? "px-2.5 py-1.5 text-[0.84rem]" : "px-2 py-1 text-[0.74rem]",39        v.soft,40        v.tone,41        className,42      )}43    >44      {v.label}45      {deviation != null && verdict !== "marche" && <span className="num">{fmtPct(deviation)}</span>}46    </span>47  );48}4950/** Étiquette posée sur une photographie : fond papier fixe, toujours lisible. */51export function VerdictTag({52  verdict,53  deviation,54  plain,55}: {56  verdict: Verdict | null | undefined;57  deviation?: number | null;58  /** hors photographie : texte seul, couleur du thème */59  plain?: boolean;60}) {61  if (!verdict || verdict === "marche") return null;62  const good = verdict === "sous";63  if (plain) {64    return (65      <span className={cn("num shrink-0 text-[0.78rem] font-medium", good ? "text-good" : "text-high")}>66        {VERDICT[verdict].label} {deviation != null && fmtPct(deviation)}67      </span>68    );69  }70  return (71    <span72      className="num inline-flex items-center gap-1 rounded-[6px] bg-white/92 px-1.5 py-1 text-[0.72rem] font-semibold leading-none"73      style={{ color: good ? "#2c6a4c" : "#8f5d10" }}74    >75      {VERDICT[verdict].label}76      {deviation != null && <span className="font-medium">{fmtPct(deviation)}</span>}77    </span>78  );79}8081/** Jauge : position du prix demandé par rapport à la fourchette du vrai prix. */82export function FairValueGauge({83  price,84  fv,85  low,86  high,87  verdict,88}: {89  price: number;90  fv: number;91  low: number;92  high: number;93  verdict: Verdict | null | undefined;94}) {95  const span = Math.max(high - low, 1);96  const min = low - span * 0.45;97  const max = high + span * 0.45;98  const pos = (x: number) => `${Math.min(100, Math.max(0, ((x - min) / (max - min)) * 100))}%`;99  const color = verdict ? VERDICT[verdict].color : "var(--ink-3)";100  // le curseur part de la valeur estimée et vient se poser sur le prix demandé101  const [ref, seen] = useSeenOnce<HTMLDivElement>();102  const at = seen ? price : fv;103  return (104    <div ref={ref} className="relative pb-6 pt-8">105      <div className="relative h-1.5 rounded-full bg-surface-3">106        <div className="absolute inset-y-0 origin-left rounded-full bg-ink-3/35 transition-transform duration-500 ease-[cubic-bezier(0.2,0.7,0.2,1)]" style={{ left: pos(low), right: `calc(100% - ${pos(high)})`, transform: seen ? "none" : "scaleX(0)" }} />107        <div className="absolute -top-1.5 h-4.5 w-px -translate-x-1/2 bg-ink" style={{ left: pos(fv) }} />108        <div109          className="absolute -top-[5px] size-4 -translate-x-1/2 rounded-full border-2 border-surface transition-[left] duration-700 ease-[cubic-bezier(0.3,1.45,0.5,1)]"110          style={{ left: pos(at), background: color }}111        />112      </div>113      <div className="num absolute top-0 -translate-x-1/2 whitespace-nowrap text-[0.72rem] font-medium transition-[left] duration-700 ease-[cubic-bezier(0.3,1.45,0.5,1)]" style={{ left: pos(at), color }}>114        Prix demandé115      </div>116      <div className="num absolute bottom-0 -translate-x-1/2 text-[0.7rem] text-ink-3" style={{ left: pos(low) }}>117        {fmtShort(low)}118      </div>119      <div className="num absolute bottom-0 -translate-x-1/2 text-[0.7rem] text-ink-3" style={{ left: pos(high) }}>120        {fmtShort(high)}121      </div>122    </div>123  );124}125
Files main
Menu

Theme