Files
co-lite/solvway-admin-portal/app/components/dashboard/kpi-tile.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

166 lines
5.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { ArrowDown, ArrowRight, ArrowUp } from 'lucide-react'
import type { LucideIcon } from 'lucide-react'
import { Card, CardContent } from '@/app/components/ui/card'
import { cn } from '@/app/lib/utils'
/** Which token the tile's icon chip and sparkline draw from. */
export type KpiTone = 'primary' | 'accent' | 'chart-2' | 'chart-4' | 'chart-5' | 'muted'
const TONE_CHIP: Record<KpiTone, string> = {
primary: 'bg-primary/10 text-primary',
// Solid fill: the brand gold cannot carry a glyph as a tint on white.
accent: 'bg-accent text-accent-foreground',
'chart-2': 'bg-chart-2/10 text-chart-2',
'chart-4': 'bg-chart-4/10 text-chart-4',
'chart-5': 'bg-chart-5/10 text-chart-5',
muted: 'bg-muted text-muted-foreground',
}
const TONE_STROKE: Record<KpiTone, string> = {
primary: 'var(--primary)',
accent: 'var(--accent-strong)',
'chart-2': 'var(--chart-2)',
'chart-4': 'var(--chart-4)',
'chart-5': 'var(--chart-5)',
muted: 'var(--muted-foreground)',
}
/**
* Decorative trend line. Plain SVG rather than Recharts: it needs no measurement
* (so it renders correctly in jsdom), costs nothing, and per §5 it is
* aria-hidden — the number beside it is the accessible content.
*
* The gradient is a fill under a data line, which §11 permits; it is not page
* decoration.
*/
function Sparkline({ points, tone, id }: { points: number[]; tone: KpiTone; id: string }) {
if (points.length < 2) return null
const min = Math.min(...points)
const max = Math.max(...points)
const span = max - min || 1
const coords = points.map((value, i) => ({
x: (i / (points.length - 1)) * 100,
y: 26 - ((value - min) / span) * 22,
}))
const line = coords.map((p, i) => `${i === 0 ? 'M' : 'L'}${p.x.toFixed(2)},${p.y.toFixed(2)}`).join(' ')
const area = `${line} L100,28 L0,28 Z`
const stroke = TONE_STROKE[tone]
return (
<svg
viewBox="0 0 100 28"
preserveAspectRatio="none"
className="h-8 w-full"
aria-hidden="true"
focusable="false"
>
<defs>
<linearGradient id={id} x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor={stroke} stopOpacity="0.28" />
<stop offset="100%" stopColor={stroke} stopOpacity="0" />
</linearGradient>
</defs>
<path d={area} fill={`url(#${id})`} stroke="none" />
<path
d={line}
fill="none"
stroke={stroke}
strokeWidth="2"
vectorEffect="non-scaling-stroke"
strokeLinecap="round"
/>
</svg>
)
}
export interface KpiTileProps {
label: string
value: string
/** Fractional change vs the comparison period, e.g. 0.124 for +12.4%; null shows "—" (no base to compare). */
delta?: number | null
/** Stated in words — a bare percentage is unreadable (§5). */
comparisonLabel?: string
/** Read out in place of the "—" shown when delta is null. */
noComparisonLabel?: string
sparkline?: number[]
icon?: LucideIcon
tone?: KpiTone
}
export function KpiTile({
label,
value,
delta,
comparisonLabel,
noComparisonLabel,
sparkline,
icon: Icon,
tone = 'primary',
}: KpiTileProps) {
// §5: colour is never the only signal — an arrow glyph and an explicit sign
// carry the same information. Exactly zero reads neutral, not green.
const direction = delta == null ? 'none' : delta > 0 ? 'up' : delta < 0 ? 'down' : 'flat'
const DeltaIcon = direction === 'up' ? ArrowUp : direction === 'down' ? ArrowDown : ArrowRight
const gradientId = `spark-${label.replace(/\W+/g, '-').toLowerCase()}`
return (
<Card className="gap-0 overflow-hidden py-0">
<CardContent className="flex min-w-0 flex-col gap-2.5 px-4 py-4">
{/* The label has the row to itself: a wide delta beside it covered a long label. */}
<div className="flex min-w-0 items-center gap-2">
{Icon && (
<span
aria-hidden="true"
className={cn(
'flex size-7 shrink-0 items-center justify-center rounded-md',
TONE_CHIP[tone],
)}
>
<Icon className="size-4" />
</span>
)}
<p className="text-xs leading-tight font-semibold tracking-wide text-muted-foreground uppercase">
{label}
</p>
</div>
<p className="tabular text-2xl leading-tight whitespace-nowrap font-medium text-foreground">{value}</p>
{(delta !== undefined || comparisonLabel) && (
<div className="flex flex-wrap items-center gap-x-1.5 gap-y-1">
{delta === null && (
<span className="inline-flex shrink-0 items-center rounded-full bg-muted px-1.5 py-0.5 text-xs font-medium text-muted-foreground">
<span aria-hidden="true">—</span>
{noComparisonLabel && <span className="sr-only">{noComparisonLabel}</span>}
</span>
)}
{direction !== 'none' && delta != null && (
<span
className={cn(
'inline-flex shrink-0 items-center gap-0.5 rounded-full px-1.5 py-0.5 text-xs font-medium',
direction === 'up' && 'bg-positive/10 text-positive',
direction === 'down' && 'bg-negative/10 text-negative',
direction === 'flat' && 'bg-muted text-muted-foreground',
)}
>
<DeltaIcon aria-hidden="true" className="size-3.5" />
<span className="tabular">
{delta > 0 ? '+' : delta < 0 ? '−' : ''}
{Math.abs(delta * 100).toFixed(1)}%
</span>
</span>
)}
{comparisonLabel && <p className="text-xs text-muted-foreground">{comparisonLabel}</p>}
</div>
)}
</CardContent>
{sparkline && sparkline.length > 1 && (
<Sparkline points={sparkline} tone={tone} id={gradientId} />
)}
</Card>
)
}