import { cn } from "@suphark/ui/lib/utils"; export type SparklineTone = "neutral" | "success" | "destructive"; const TONE: Record = { neutral: { line: "stroke-muted-foreground", dot: "fill-muted-foreground" }, success: { line: "stroke-success", dot: "fill-success" }, destructive: { line: "stroke-destructive", dot: "fill-destructive" }, }; /** * เส้นแนวโน้มขนาดเล็กในแถวรายการ/การ์ด (ยกจาก DueBell) — ไม่มีแกน ไม่มี hook ใช้ใน server component ได้ * `null` = ไม่มีข้อมูลช่วงนั้น (เส้นข้ามไป แต่เว้นระยะตามเดิม) · จุดท้ายเส้น = ค่าล่าสุด · ข้อมูลไม่ถึง 2 จุด = ไม่วาด * ส่ง `label` เพื่อให้โปรแกรมอ่านจอรู้ความหมาย (ไม่ส่ง = ซ่อน เมื่อมีตัวเลขกำกับข้าง ๆ อยู่แล้ว) */ export function Sparkline({ values, tone = "neutral", width = 96, height = 28, label, className, }: { values: ReadonlyArray; tone?: SparklineTone; width?: number; height?: number; label?: string; className?: string; }) { const points = values.flatMap((y, x) => typeof y === "number" && Number.isFinite(y) ? [{ x, y }] : [], ); const last = points[points.length - 1]; if (points.length < 2 || !last) return null; const ys = points.map((p) => p.y); const min = Math.min(...ys); const max = Math.max(...ys); const stepX = width / Math.max(1, values.length - 1); // เว้นขอบ 3px ให้เส้นและจุดท้ายไม่ถูกตัด · ค่าเท่ากันหมด = เส้นกลางกรอบ const toY = (y: number) => max === min ? height / 2 : height - 3 - ((y - min) / (max - min)) * (height - 6); const path = points .map((p, i) => `${i === 0 ? "M" : "L"}${(p.x * stepX).toFixed(1)},${toY(p.y).toFixed(1)}`) .join(" "); return ( ); }