SPB Git

Repository Public

Score.tsx 6.5 KB 172 lines tsx

1// Scores de quartier (0-100) : anneau, barres par axe, radar.23import { Bike, Footprints, ShoppingBag, TrainFront, Trees } from "lucide-react";4import { cn } from "@/lib/cn";5import { useSeenOnce } from "@/lib/motion";67export const SCORE_AXES = [8  { key: "walk", label: "Marche", icon: Footprints, hint: "Commerces et services à pied" },9  { key: "transit", label: "Transport", icon: TrainFront, hint: "Métro, bus, fréquence" },10  { key: "bike", label: "Vélo", icon: Bike, hint: "Réseau cyclable à proximité" },11  { key: "calme", label: "Calme", icon: Trees, hint: "Éloignement du bruit, parcs" },12  { key: "services", label: "Services", icon: ShoppingBag, hint: "Écoles, santé, épiceries" },13] as const;1415export type ScoreKey = (typeof SCORE_AXES)[number]["key"];1617/** Couleur d'un score : forêt (bon), pierre (moyen), ocre puis rouge (faible). */18export function scoreColor(s: number | null | undefined): string {19  if (s == null) return "var(--ink-3)";20  if (s >= 65) return "var(--brand)";21  if (s >= 50) return "var(--fair)";22  if (s >= 35) return "var(--high)";23  return "var(--bad)";24}2526export function ScoreRing({27  value,28  size = 56,29  stroke = 5,30  className,31  label,32}: {33  value: number | null | undefined;34  size?: number;35  stroke?: number;36  className?: string;37  label?: string;38}) {39  const r = (size - stroke) / 2;40  const c = 2 * Math.PI * r;41  const pct = value == null ? 0 : Math.min(100, Math.max(0, value)) / 100;42  const color = scoreColor(value);43  // l'anneau se remplit une fois, à l'entrée dans la fenêtre44  const [ref, seen] = useSeenOnce<HTMLDivElement>();45  return (46    <div47      ref={ref}48      className={cn("relative grid shrink-0 place-items-center", className)}49      style={{ width: size, height: size }}50      role="img"51      aria-label={`${label ?? "Score"} : ${value == null ? "non disponible" : `${Math.round(value)} sur 100`}`}52    >53      <svg width={size} height={size} className="-rotate-90 overflow-visible">54        <circle cx={size / 2} cy={size / 2} r={r} fill="none" stroke="var(--surface-3)" strokeWidth={stroke} />55        <circle56          cx={size / 2}57          cy={size / 2}58          r={r}59          fill="none"60          stroke={color}61          strokeWidth={stroke}62          strokeLinecap="round"63          strokeDasharray={c}64          strokeDashoffset={seen ? c * (1 - pct) : c}65          style={{ transition: "stroke-dashoffset 0.9s cubic-bezier(0.2,0.7,0.2,1)" }}66        />67      </svg>68      <span className="num absolute font-sans font-semibold text-ink" style={{ fontSize: size * 0.34 }}>69        {value == null ? "–" : Math.round(value)}70      </span>71    </div>72  );73}7475/** Pastille compacte pour les cartes d'annonce. */76export function ScoreBadge({ value, className }: { value: number | null | undefined; className?: string }) {77  if (value == null) return null;78  return (79    <span80      className={cn(81        "num inline-flex items-center gap-1 rounded-full bg-surface-2 px-2 py-1 text-[0.72rem] font-semibold leading-none text-ink-2",82        className,83      )}84      title={`Score de quartier : ${Math.round(value)} / 100`}85    >86      <span className="size-1.5 rounded-full" style={{ background: scoreColor(value) }} />87      {Math.round(value)}88    </span>89  );90}9192/** Radar à cinq axes. */93export function ScoreRadar({94  scores,95  size = 240,96}: {97  scores: Partial<Record<ScoreKey, number | null | undefined>>;98  size?: number;99}) {100  const cx = size / 2;101  const cy = size / 2;102  const R = size / 2 - 46;103  const pt = (i: number, v: number) => {104    const a = (Math.PI * 2 * i) / SCORE_AXES.length - Math.PI / 2;105    return [cx + Math.cos(a) * R * v, cy + Math.sin(a) * R * v] as const;106  };107  const poly = SCORE_AXES.map((a, i) => pt(i, Math.max(0.04, (scores[a.key] ?? 0) / 100)).join(",")).join(" ");108  const [ref, seen] = useSeenOnce<SVGSVGElement>();109  return (110    <svg ref={ref} data-drawn={seen ? "true" : "false"} width={size} height={size} viewBox={`0 0 ${size} ${size}`} role="img" aria-label="Profil du quartier">111      {[0.25, 0.5, 0.75, 1].map((k) => (112        <polygon113          key={k}114          points={SCORE_AXES.map((_, i) => pt(i, k).join(",")).join(" ")}115          fill="none"116          stroke="var(--line-2)"117          strokeWidth={1}118        />119      ))}120      {SCORE_AXES.map((_, i) => {121        const [x, y] = pt(i, 1);122        return <line key={i} x1={cx} y1={cy} x2={x} y2={y} stroke="var(--line)" strokeWidth={1} />;123      })}124      <polygon className="lk-draw-pop" points={poly} fill="var(--brand)" fillOpacity={0.14} stroke="var(--brand)" strokeWidth={1.75} strokeLinejoin="round" />125      {SCORE_AXES.map((a, i) => {126        const [x, y] = pt(i, Math.max(0.04, (scores[a.key] ?? 0) / 100));127        const [lx, ly] = pt(i, 1.16);128        return (129          <g key={a.key}>130            <circle className="lk-draw-fade" cx={x} cy={y} r={3.5} fill="var(--surface)" stroke="var(--brand)" strokeWidth={1.75} />131            <text x={lx} y={ly} textAnchor={lx < cx - 6 ? "end" : lx > cx + 6 ? "start" : "middle"} dominantBaseline="middle" fontSize={11} fontWeight={600} fill="var(--ink-2)">132              {a.label}133            </text>134          </g>135        );136      })}137    </svg>138  );139}140141/** Une ligne par axe : icône, libellé, barre, valeur. */142export function ScoreBars({ scores }: { scores: Partial<Record<ScoreKey, number | null | undefined>> }) {143  const [ref, seen] = useSeenOnce<HTMLUListElement>();144  return (145    <ul ref={ref} data-drawn={seen ? "true" : "false"} className="space-y-3.5">146      {SCORE_AXES.map(({ key, label, icon: Icon, hint }, i) => {147        const v = scores[key];148        return (149          <li key={key} className="flex items-center gap-3">150            <span className="grid size-9 shrink-0 place-items-center rounded-[8px] bg-surface-2 text-ink-2">151              <Icon className="size-4.5" />152            </span>153            <div className="min-w-0 flex-1">154              <div className="flex items-baseline justify-between gap-3">155                <span className="text-sm font-medium text-ink">{label}</span>156                <span className="num text-sm font-semibold text-ink">{v == null ? "—" : Math.round(v)}</span>157              </div>158              <div className="mt-1.5 h-1.5 overflow-hidden rounded-full bg-surface-3">159                <div160                  className="lk-draw-bar h-full rounded-full"161                  style={{ width: `${v ?? 0}%`, background: scoreColor(v), ["--i" as string]: i }}162                />163              </div>164              <div className="mt-1 text-xs text-ink-3">{hint}</div>165            </div>166          </li>167        );168      })}169    </ul>170  );171}172
Files main
Menu

Theme