forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepaymentVisualizer.tsx
More file actions
595 lines (537 loc) · 19.4 KB
/
Copy pathRepaymentVisualizer.tsx
File metadata and controls
595 lines (537 loc) · 19.4 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/**
* RepaymentVisualizer
*
* Renders a stacked area chart (principal vs interest) for a repayment schedule,
* with an accessible SR-fallback data table and hover/long-press tooltip.
*
* Approach:
* - Pure SVG — no third-party charting library.
* - Theme tokens from CSS custom properties; colours from src/utils/tokens.ts.
* - WCAG 2.1 AA: focus ring, aria-label, role="img", table fallback, reduced-motion.
*/
import { useState, useRef, useCallback, useId } from 'react';
import { COLOR } from '@/utils/tokens';
// ─── Types ────────────────────────────────────────────────────────────────────
export interface ScheduleRow {
/** 1-based month number */
month: number;
/** Remaining principal at end of month */
principal: number;
/** Cumulative interest paid to end of month */
cumulativeInterest: number;
/** Interest component of this month's payment */
interest: number;
/** Principal component of this month's payment */
principalPaid: number;
}
export interface RepaymentVisualizerProps {
/** Outstanding debt at start */
principal: number;
/** Annual percentage rate (e.g. 8.5 for 8.5 %) */
apr: number;
/** Fixed monthly payment */
monthlyPayment: number;
/** Max months to project (capped at 360) */
maxMonths?: number;
}
// ─── Schedule generator ────────────────────────────────────────────────────────
function buildSchedule(
principal: number,
apr: number,
monthlyPayment: number,
maxMonths: number,
): ScheduleRow[] {
if (principal <= 0 || monthlyPayment <= 0) return [];
const monthlyRate = apr / 100 / 12;
let remaining = principal;
let cumInterest = 0;
const rows: ScheduleRow[] = [];
for (let m = 1; m <= maxMonths && remaining > 0; m++) {
const interest = remaining * monthlyRate;
const principalPaid = Math.min(monthlyPayment - interest, remaining);
cumInterest += interest;
remaining = Math.max(0, remaining - principalPaid);
rows.push({
month: m,
principal: remaining,
cumulativeInterest: cumInterest,
interest,
principalPaid: Math.max(0, principalPaid),
});
if (remaining <= 0) break;
}
return rows;
}
// ─── SVG chart ────────────────────────────────────────────────────────────────
const W = 600;
const H = 220;
const PAD = { top: 16, right: 16, bottom: 36, left: 56 };
const CHART_W = W - PAD.left - PAD.right;
const CHART_H = H - PAD.top - PAD.bottom;
function toPath(points: [number, number][]): string {
if (points.length === 0) return '';
return points.map(([x, y], i) => `${i === 0 ? 'M' : 'L'}${x},${y}`).join(' ');
}
function fmtK(n: number): string {
if (n >= 1_000_000) return `$${(n / 1_000_000).toFixed(1)}M`;
if (n >= 1_000) return `$${(n / 1_000).toFixed(0)}K`;
return `$${n.toFixed(0)}`;
}
interface TooltipData {
month: number;
principal: number;
cumulativeInterest: number;
x: number;
y: number;
}
interface ChartProps {
schedule: ScheduleRow[];
tooltipId: string;
onTooltip: (data: TooltipData | null) => void;
tooltip: TooltipData | null;
}
function StackedAreaChart({ schedule, tooltipId, onTooltip, tooltip }: ChartProps) {
if (schedule.length === 0) return null;
const initPrincipal = schedule[0].principal + schedule[0].principalPaid;
const maxStack = initPrincipal + (schedule[schedule.length - 1]?.cumulativeInterest ?? 0);
const xScale = (i: number) => PAD.left + (i / (schedule.length - 1 || 1)) * CHART_W;
const yScale = (v: number) => PAD.top + CHART_H - (v / maxStack) * CHART_H;
// Area 1: principal remaining (bottom area)
const principalTop: [number, number][] = schedule.map((r, i) => [xScale(i), yScale(r.principal)]);
const principalBase: [number, number][] = [
[xScale(schedule.length - 1), yScale(0)],
[xScale(0), yScale(0)],
];
const principalPath = `${toPath(principalTop)} ${toPath(principalBase)} Z`;
// Area 2: cumulative interest (stacked on top of principal)
const interestTop: [number, number][] = schedule.map((r, i) => [
xScale(i),
yScale(r.principal + r.cumulativeInterest),
]);
const interestBase: [number, number][] = [...principalTop].reverse();
const interestPath = `${toPath(interestTop)} ${toPath(interestBase)} Z`;
// Y-axis ticks
const yTicks = [0, 0.25, 0.5, 0.75, 1].map((f) => ({
value: maxStack * f,
y: yScale(maxStack * f),
}));
// X-axis ticks (up to 6)
const xTickCount = Math.min(schedule.length, 6);
const xTicks = Array.from({ length: xTickCount }, (_, i) => {
const idx = Math.round((i / (xTickCount - 1 || 1)) * (schedule.length - 1));
return { month: schedule[idx].month, x: xScale(idx) };
});
const handleMouseMove = useCallback(
(e: React.MouseEvent<SVGSVGElement>) => {
const rect = e.currentTarget.getBoundingClientRect();
const svgX = ((e.clientX - rect.left) / rect.width) * W;
const relX = svgX - PAD.left;
const idx = Math.round((relX / CHART_W) * (schedule.length - 1));
const clamped = Math.max(0, Math.min(idx, schedule.length - 1));
const row = schedule[clamped];
onTooltip({
month: row.month,
principal: row.principal,
cumulativeInterest: row.cumulativeInterest,
x: xScale(clamped),
y: yScale(row.principal + row.cumulativeInterest),
});
},
[schedule, xScale, yScale, onTooltip],
);
return (
<svg
viewBox={`0 0 ${W} ${H}`}
role="img"
aria-label="Stacked area chart showing principal and cumulative interest over repayment months"
aria-describedby={tooltipId}
style={{ width: '100%', height: 'auto', overflow: 'visible' }}
onMouseMove={handleMouseMove}
onMouseLeave={() => onTooltip(null)}
onTouchEnd={() => onTooltip(null)}
>
<defs>
<linearGradient id="grad-principal" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor={COLOR.accent} stopOpacity="0.7" />
<stop offset="100%" stopColor={COLOR.accent} stopOpacity="0.15" />
</linearGradient>
<linearGradient id="grad-interest" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor={COLOR.warning} stopOpacity="0.7" />
<stop offset="100%" stopColor={COLOR.warning} stopOpacity="0.15" />
</linearGradient>
</defs>
{/* Grid lines */}
{yTicks.map(({ y, value }) => (
<g key={value}>
<line
x1={PAD.left}
y1={y}
x2={W - PAD.right}
y2={y}
stroke={COLOR.border}
strokeWidth="0.5"
strokeDasharray="4 4"
/>
<text
x={PAD.left - 6}
y={y + 4}
textAnchor="end"
fontSize="10"
fill={COLOR.muted}
>
{fmtK(value)}
</text>
</g>
))}
{/* Interest area (painted first — sits below principal visually in stacking) */}
<path d={interestPath} fill="url(#grad-interest)" />
{/* Principal area */}
<path d={principalPath} fill="url(#grad-principal)" />
{/* X-axis ticks */}
{xTicks.map(({ month, x }) => (
<text key={month} x={x} y={H - 6} textAnchor="middle" fontSize="10" fill={COLOR.muted}>
mo {month}
</text>
))}
{/* Tooltip crosshair */}
{tooltip && (
<g>
<line
x1={tooltip.x}
y1={PAD.top}
x2={tooltip.x}
y2={PAD.top + CHART_H}
stroke={COLOR.border}
strokeWidth="1"
/>
<circle cx={tooltip.x} cy={yScale(tooltip.principal)} r="4" fill={COLOR.accent} />
<circle
cx={tooltip.x}
cy={yScale(tooltip.principal + tooltip.cumulativeInterest)}
r="4"
fill={COLOR.warning}
/>
</g>
)}
</svg>
);
}
// ─── Tooltip bubble ────────────────────────────────────────────────────────────
function TooltipBubble({ data }: { data: TooltipData }) {
const fmt = (n: number) =>
new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 }).format(n);
return (
<div
role="status"
aria-live="polite"
style={{
background: 'var(--surface-raised, #1c2230)',
border: `1px solid var(--border, ${COLOR.border})`,
borderRadius: 8,
padding: '0.5rem 0.75rem',
fontSize: '0.75rem',
color: `var(--text, ${COLOR.text})`,
pointerEvents: 'none',
minWidth: 160,
boxShadow: '0 4px 16px rgba(0,0,0,0.4)',
}}
>
<p style={{ fontWeight: 600, marginBottom: 4 }}>Month {data.month}</p>
<p style={{ color: `var(--accent, ${COLOR.accent})` }}>
Principal remaining: {fmt(data.principal)}
</p>
<p style={{ color: `var(--warning, ${COLOR.warning})` }}>
Cumulative interest: {fmt(data.cumulativeInterest)}
</p>
</div>
);
}
// ─── SR table fallback ─────────────────────────────────────────────────────────
function SRTable({ schedule }: { schedule: ScheduleRow[] }) {
const fmt = (n: number) =>
new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 2 }).format(n);
// Limit visible rows; full table always in SR tree
return (
<table
className="sr-only"
aria-label="Repayment schedule data table"
style={{ borderCollapse: 'collapse', width: '100%', fontSize: '0.75rem' }}
>
<caption className="sr-only">Monthly repayment schedule: principal and interest breakdown</caption>
<thead>
<tr>
<th scope="col">Month</th>
<th scope="col">Principal Paid</th>
<th scope="col">Interest Paid</th>
<th scope="col">Remaining Principal</th>
<th scope="col">Cumulative Interest</th>
</tr>
</thead>
<tbody>
{schedule.map((row) => (
<tr key={row.month}>
<td>{row.month}</td>
<td>{fmt(row.principalPaid)}</td>
<td>{fmt(row.interest)}</td>
<td>{fmt(row.principal)}</td>
<td>{fmt(row.cumulativeInterest)}</td>
</tr>
))}
</tbody>
</table>
);
}
// ─── Legend ────────────────────────────────────────────────────────────────────
function Legend() {
return (
<div
style={{ display: 'flex', gap: '1rem', fontSize: '0.75rem', color: `var(--muted, ${COLOR.muted})` }}
aria-hidden="true"
>
<span style={{ display: 'flex', alignItems: 'center', gap: 4 }}>
<span
style={{ width: 12, height: 12, borderRadius: 2, background: COLOR.accent, display: 'inline-block' }}
/>
Principal remaining
</span>
<span style={{ display: 'flex', alignItems: 'center', gap: 4 }}>
<span
style={{ width: 12, height: 12, borderRadius: 2, background: COLOR.warning, display: 'inline-block' }}
/>
Cumulative interest
</span>
</div>
);
}
// ─── Visible fallback table (non-SR) ──────────────────────────────────────────
interface VisibleTableProps {
schedule: ScheduleRow[];
limit?: number;
}
function VisibleTable({ schedule, limit = 12 }: VisibleTableProps) {
const [showAll, setShowAll] = useState(false);
const rows = showAll ? schedule : schedule.slice(0, limit);
const fmt = (n: number) =>
new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 2 }).format(n);
const thStyle: React.CSSProperties = {
textAlign: 'right',
padding: '6px 8px',
fontWeight: 600,
fontSize: '0.7rem',
color: `var(--muted, ${COLOR.muted})`,
borderBottom: `1px solid var(--border, ${COLOR.border})`,
whiteSpace: 'nowrap',
};
const tdStyle: React.CSSProperties = {
textAlign: 'right',
padding: '5px 8px',
fontSize: '0.75rem',
color: `var(--text, ${COLOR.text})`,
};
return (
<div style={{ overflowX: 'auto' }}>
<table
aria-label="Repayment schedule"
style={{ borderCollapse: 'collapse', width: '100%', minWidth: 460 }}
>
<thead>
<tr>
<th scope="col" style={{ ...thStyle, textAlign: 'left' }}>Month</th>
<th scope="col" style={thStyle}>Principal Paid</th>
<th scope="col" style={thStyle}>Interest</th>
<th scope="col" style={thStyle}>Remaining</th>
<th scope="col" style={thStyle}>Cum. Interest</th>
</tr>
</thead>
<tbody>
{rows.map((row, i) => (
<tr
key={row.month}
style={{ background: i % 2 === 0 ? 'transparent' : `var(--surface, ${COLOR.surface})` }}
>
<td style={{ ...tdStyle, textAlign: 'left', color: `var(--muted, ${COLOR.muted})` }}>
{row.month}
</td>
<td style={{ ...tdStyle, color: `var(--accent, ${COLOR.accent})` }}>
{fmt(row.principalPaid)}
</td>
<td style={{ ...tdStyle, color: `var(--warning, ${COLOR.warning})` }}>
{fmt(row.interest)}
</td>
<td style={tdStyle}>{fmt(row.principal)}</td>
<td style={tdStyle}>{fmt(row.cumulativeInterest)}</td>
</tr>
))}
</tbody>
</table>
{schedule.length > limit && (
<button
type="button"
onClick={() => setShowAll((s) => !s)}
style={{
marginTop: '0.5rem',
fontSize: '0.75rem',
color: `var(--accent, ${COLOR.accent})`,
background: 'none',
border: 'none',
cursor: 'pointer',
padding: '4px 0',
}}
aria-expanded={showAll}
>
{showAll ? 'Show fewer rows' : `Show all ${schedule.length} months`}
</button>
)}
</div>
);
}
// ─── Empty state ───────────────────────────────────────────────────────────────
function EmptyState() {
return (
<p
style={{
textAlign: 'center',
padding: '2rem',
color: `var(--muted, ${COLOR.muted})`,
fontSize: '0.875rem',
}}
>
Enter a valid principal, APR, and monthly payment to see the repayment plan.
</p>
);
}
// ─── Main component ────────────────────────────────────────────────────────────
/**
* RepaymentVisualizer displays a stacked area chart (principal vs cumulative
* interest) over the life of a loan, plus an accessible data table.
*/
export function RepaymentVisualizer({
principal,
apr,
monthlyPayment,
maxMonths = 360,
}: RepaymentVisualizerProps) {
const [tooltip, setTooltip] = useState<TooltipData | null>(null);
const tooltipId = useId();
const longPressTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
const schedule = buildSchedule(principal, apr, monthlyPayment, maxMonths);
// Long-press for mobile tooltip
const handleTouchStart = useCallback(
(e: React.TouchEvent<HTMLDivElement>) => {
const touch = e.touches[0];
longPressTimer.current = setTimeout(() => {
// approximate index from touch position
const rect = (e.currentTarget as HTMLDivElement).getBoundingClientRect();
const relX = touch.clientX - rect.left;
const idx = Math.round((relX / rect.width) * (schedule.length - 1));
const clamped = Math.max(0, Math.min(idx, schedule.length - 1));
const row = schedule[clamped];
if (row) {
setTooltip({
month: row.month,
principal: row.principal,
cumulativeInterest: row.cumulativeInterest,
x: 0,
y: 0,
});
}
}, 400);
},
[schedule],
);
const handleTouchEnd = useCallback(() => {
if (longPressTimer.current) clearTimeout(longPressTimer.current);
}, []);
const totalInterest = schedule[schedule.length - 1]?.cumulativeInterest ?? 0;
const termMonths = schedule.length;
return (
<section
aria-label="Repayment plan visualizer"
style={{
background: `var(--surface, ${COLOR.surface})`,
border: `1px solid var(--border, ${COLOR.border})`,
borderRadius: 10,
padding: '1.25rem',
}}
>
<header style={{ marginBottom: '1rem' }}>
<h2
style={{
fontSize: '1rem',
fontWeight: 700,
color: `var(--text, ${COLOR.text})`,
margin: 0,
}}
>
Repayment Plan
</h2>
{schedule.length > 0 && (
<p style={{ fontSize: '0.8rem', color: `var(--muted, ${COLOR.muted})`, marginTop: 4 }}>
{termMonths} month{termMonths !== 1 ? 's' : ''} ·{' '}
{new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
maximumFractionDigits: 0,
}).format(totalInterest)}{' '}
total interest
</p>
)}
</header>
{schedule.length === 0 ? (
<EmptyState />
) : (
<>
{/* Chart */}
<div
style={{ position: 'relative' }}
onTouchStart={handleTouchStart}
onTouchEnd={handleTouchEnd}
>
<StackedAreaChart
schedule={schedule}
tooltipId={tooltipId}
onTooltip={setTooltip}
tooltip={tooltip}
/>
{/* Tooltip bubble — positioned absolutely over chart */}
{tooltip && (
<div
id={tooltipId}
style={{
position: 'absolute',
top: 8,
right: 8,
}}
>
<TooltipBubble data={tooltip} />
</div>
)}
</div>
<Legend />
{/* SR-only full data table (always present for assistive tech) */}
<SRTable schedule={schedule} />
{/* Visible table */}
<details style={{ marginTop: '1rem' }}>
<summary
style={{
cursor: 'pointer',
fontSize: '0.8rem',
color: `var(--accent, ${COLOR.accent})`,
userSelect: 'none',
padding: '4px 0',
outline: 'none',
}}
// Focus ring via global focus.css
className="focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2"
>
Schedule table
</summary>
<div style={{ marginTop: '0.5rem' }}>
<VisibleTable schedule={schedule} />
</div>
</details>
</>
)}
</section>
);
}