forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataQualityBadge.tsx
More file actions
161 lines (147 loc) · 4.7 KB
/
Copy pathDataQualityBadge.tsx
File metadata and controls
161 lines (147 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use client';
import { useMemo, useState } from 'react';
import { analyseDataQuality, qualityColor, qualityIcon, type QualityReport } from '@/lib/data-quality';
interface DataQualityBadgeProps {
labels: string[];
values: number[];
/** Optional label shown in the tooltip header */
metricName?: string;
}
/** Small traffic-light badge that shows data quality for a chart. */
export function DataQualityBadge({ labels, values, metricName }: DataQualityBadgeProps) {
const [open, setOpen] = useState(false);
const report = useMemo(() => analyseDataQuality(labels, values), [labels, values]);
const color = qualityColor(report.level);
const icon = qualityIcon(report.level);
return (
<span style={{ position: 'relative', display: 'inline-flex', alignItems: 'center' }}>
<button
type="button"
onClick={() => setOpen((v) => !v)}
title={`Data quality: ${report.score}%`}
aria-label={`Data quality ${report.score}%`}
style={{
display: 'inline-flex',
alignItems: 'center',
gap: 4,
borderRadius: 999,
border: `1px solid ${color}44`,
background: `${color}18`,
color,
padding: '2px 8px',
fontSize: 11,
fontWeight: 700,
cursor: 'pointer',
}}
>
<span>{icon}</span>
<span>{report.score}%</span>
</button>
{open && (
<QualityTooltip report={report} metricName={metricName} onClose={() => setOpen(false)} />
)}
</span>
);
}
function QualityTooltip({
report,
metricName,
onClose,
}: {
report: QualityReport;
metricName?: string;
onClose: () => void;
}) {
const color = qualityColor(report.level);
return (
<>
<div
style={{ position: 'fixed', inset: 0, zIndex: 90 }}
onClick={onClose}
aria-hidden="true"
/>
<div
role="tooltip"
style={{
position: 'absolute',
top: '110%',
left: 0,
zIndex: 100,
minWidth: 260,
background: '#fff',
border: '1px solid #e2e8f0',
borderRadius: 12,
boxShadow: '0 8px 24px rgba(15,23,42,0.12)',
padding: '14px 16px',
}}
>
<div style={{ fontWeight: 800, fontSize: 13, marginBottom: 6 }}>
{metricName ? `${metricName} — ` : ''}Data Quality
</div>
{/* Score bar */}
<div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 10 }}>
<div
style={{
flex: 1,
height: 6,
borderRadius: 999,
background: '#e2e8f0',
overflow: 'hidden',
}}
>
<div
style={{ width: `${report.score}%`, height: '100%', background: color, borderRadius: 999 }}
/>
</div>
<span style={{ fontSize: 12, fontWeight: 700, color }}>{report.score}%</span>
</div>
<p style={{ fontSize: 12, color: '#475569', margin: '0 0 8px' }}>{report.summary}</p>
{report.gaps.length > 0 && (
<div style={{ fontSize: 11, color: '#ef4444', marginBottom: 4 }}>
<strong>Gaps (>24 h):</strong>{' '}
{report.gaps
.slice(0, 3)
.map((g) => g.label)
.join(', ')}
{report.gaps.length > 3 ? ` +${report.gaps.length - 3} more` : ''}
</div>
)}
{report.flatRuns.length > 0 && (
<div style={{ fontSize: 11, color: '#f59e0b' }}>
<strong>Suspicious flat data</strong> at {report.flatRuns.length} point
{report.flatRuns.length > 1 ? 's' : ''}
</div>
)}
</div>
</>
);
}
// ── Chart wrapper that adds a quality badge ───────────────────────────────────
interface WithDataQualityProps {
labels: string[];
values: number[];
metricName?: string;
children: React.ReactNode;
}
/**
* Wraps a chart card header with a data quality badge.
* Usage:
* <WithDataQuality labels={...} values={...} metricName="Daily Gists">
* <DailyGistsChart />
* </WithDataQuality>
*/
export function WithDataQuality({ labels, values, metricName, children }: WithDataQualityProps) {
return (
<div>
<div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 8 }}>
{metricName && (
<span style={{ fontSize: 12, fontWeight: 600, color: '#64748b', textTransform: 'uppercase', letterSpacing: '0.05em' }}>
{metricName}
</span>
)}
<DataQualityBadge labels={labels} values={values} metricName={metricName} />
</div>
{children}
</div>
);
}