Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

Metric.tsx

121 lines3.0 KB

1import { TrendingUp, TrendingDown, Minus } from 'lucide-react'
2import type { MetricProps } from '@/lib/reports/types'
3​
4const variantClasses = {
5 default: '',
6 success: 'text-[var(--report-success)]',
7 warning: 'text-[var(--report-warning)]',
8 error: 'text-[var(--report-error)]',
9}
10​
11function formatValue(
12 value: number | string,
13 format: MetricProps['format'],
14 prefix?: string,
15 suffix?: string,
16): string {
17 if (typeof value === 'string') {
18 return `${prefix || ''}${value}${suffix || ''}`
19 }
20​
21 let formatted: string
22 // Currency and percent formats include their own symbols, so skip prefix/suffix for those
23 let skipPrefixSuffix = false
24​
25 switch (format) {
26 case 'currency':
27 formatted = new Intl.NumberFormat('en-US', {
28 style: 'currency',
29 currency: 'USD',
30 minimumFractionDigits: 0,
31 maximumFractionDigits: 0,
32 }).format(value)
33 skipPrefixSuffix = true
34 break
35 case 'percent':
36 formatted = new Intl.NumberFormat('en-US', {
37 style: 'percent',
38 minimumFractionDigits: 0,
39 maximumFractionDigits: 1,
40 }).format(value / 100)
41 skipPrefixSuffix = true
42 break
43 case 'compact':
44 formatted = new Intl.NumberFormat('en-US', {
45 notation: 'compact',
46 compactDisplay: 'short',
47 }).format(value)
48 break
49 case 'number':
50 default:
51 formatted = new Intl.NumberFormat('en-US').format(value)
52 break
53 }
54​
55 if (skipPrefixSuffix) {
56 return formatted
57 }
58 return `${prefix || ''}${formatted}${suffix || ''}`
60​
61function getTrendDirection(
62 trend?: string,
63 explicitDirection?: MetricProps['trendDirection'],
64): 'up' | 'down' | 'neutral' {
65 if (explicitDirection) return explicitDirection
66 if (!trend) return 'neutral'
67​
68 if (trend.startsWith('+') || trend.includes('↑')) return 'up'
69 if (trend.startsWith('-') || trend.includes('↓')) return 'down'
70 return 'neutral'
72​
73export function Metric({
74 value,
75 label,
76 trend,
77 trendDirection,
78 format = 'number',
79 prefix,
80 suffix,
81 variant = 'default',
82}: MetricProps) {
83 const direction = getTrendDirection(trend, trendDirection)
84​
85 const trendColorClass =
86 direction === 'up'
87 ? 'text-[var(--report-success)]'
88 : direction === 'down'
89 ? 'text-[var(--report-error)]'
90 : 'text-[var(--report-text-muted)]'
91​
92 const TrendIcon =
93 direction === 'up'
94 ? TrendingUp
95 : direction === 'down'
96 ? TrendingDown
97 : Minus
98​
99 return (
100 <div className="flex flex-col">
101 <span className="text-sm text-[var(--report-text-muted)] mb-1">
102 {label}
103 </span>
104 <div className="flex items-baseline gap-2">
105 <span
106 className={`text-3xl font-bold text-[var(--report-text)] ${variantClasses[variant]}`}
107 >
108 {formatValue(value, format, prefix, suffix)}
109 </span>
110 {trend && (
111 <span
112 className={`flex items-center gap-1 text-sm ${trendColorClass}`}
113 >
114 <TrendIcon className="w-4 h-4" />
115 {trend}
116 </span>
117 )}
118 </div>
119 </div>
120 )