forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPayoffProjection.tsx
More file actions
270 lines (254 loc) · 10.3 KB
/
Copy pathPayoffProjection.tsx
File metadata and controls
270 lines (254 loc) · 10.3 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
import { useState, useMemo } from 'react';
import { Calendar, TrendingDown, Clock, DollarSign, Pencil } from 'lucide-react';
import {
computePayoffProjection,
computeDefaultMonthlyPayment,
formatMonths,
formatPayoffDate,
} from '@/utils/payoffProjection';
import { formatMoney } from '@/utils/amountValidation';
interface PayoffProjectionProps {
currentDebt: number;
apr: number;
repayAmount: number;
limit: number;
nextPaymentAmount?: number;
}
export function PayoffProjection({
currentDebt,
apr,
repayAmount,
limit,
nextPaymentAmount,
}: PayoffProjectionProps) {
const [isEditingPayment, setIsEditingPayment] = useState(false);
const [customMonthlyPayment, setCustomMonthlyPayment] = useState<number | null>(null);
const defaultMonthlyPayment = useMemo(
() => computeDefaultMonthlyPayment(currentDebt, apr, nextPaymentAmount),
[currentDebt, apr, nextPaymentAmount],
);
const monthlyPayment = customMonthlyPayment ?? defaultMonthlyPayment;
const projection = useMemo(
() => computePayoffProjection(currentDebt, apr, monthlyPayment, repayAmount, limit),
[currentDebt, apr, monthlyPayment, repayAmount, limit],
);
const hasAmount = repayAmount > 0;
const currentDate = formatPayoffDate(projection.currentMonths, false);
const newDate = formatPayoffDate(projection.newMonths, projection.isFullPayoff);
const utilizationColor =
projection.currentUtilizationPct > 80
? 'bg-red-500'
: projection.currentUtilizationPct > 50
? 'bg-yellow-500'
: 'bg-green-500';
const newUtilizationColor =
projection.newUtilizationPct > 80
? 'bg-red-500'
: projection.newUtilizationPct > 50
? 'bg-yellow-500'
: 'bg-green-500';
return (
<section
className="space-y-4 rounded-lg border border-border bg-surface p-4 sm:p-5"
aria-label="Payoff projection"
role="region"
>
<div className="flex items-center justify-between">
<h3 className="text-sm font-semibold text-foreground">
Payoff Projection
</h3>
{hasAmount && (
<span
className="inline-flex items-center gap-1 rounded-full px-2.5 py-0.5 text-xs font-semibold text-success"
style={{ background: 'rgba(63,185,80,0.12)' }}
role="status"
aria-live="polite"
aria-label={
projection.isFullPayoff
? 'Full payoff'
: `${projection.monthsSaved} months sooner`
}
>
<TrendingDown className="h-3.5 w-3.5" aria-hidden="true" />
{projection.isFullPayoff
? 'Full payoff'
: projection.monthsSaved > 0
? `${projection.monthsSaved} mo sooner`
: 'No change'}
</span>
)}
</div>
{!hasAmount ? (
<p className="text-sm text-muted">
Enter a repayment amount to see how it affects your payoff timeline.
</p>
) : (
<div className="space-y-4">
<div
className="grid gap-4 rounded-lg border border-border bg-background/60 p-4 sm:grid-cols-2"
role="group"
aria-label="Payoff timeline comparison"
>
<div>
<p className="text-xs font-medium text-muted">Current payoff</p>
<p className="mt-1 flex items-center gap-1.5 text-base font-semibold text-foreground">
<Calendar className="h-4 w-4 text-muted" aria-hidden="true" />
{currentDate}
</p>
<p className="mt-0.5 text-xs text-muted">
{formatMonths(projection.currentMonths)} of payments
</p>
</div>
<div>
<p className="text-xs font-medium text-muted">With this repayment</p>
<p className="mt-1 flex items-center gap-1.5 text-base font-semibold text-foreground">
<Calendar className="h-4 w-4 text-accent" aria-hidden="true" />
{newDate}
</p>
<p className="mt-0.5 text-xs text-muted">
{formatMonths(projection.newMonths)} of payments
</p>
</div>
</div>
<div className="grid gap-3 sm:grid-cols-2">
<div className="rounded-lg border border-border bg-background/60 p-3">
<div className="flex items-center gap-1.5">
<Clock className="h-4 w-4 text-accent" aria-hidden="true" />
<p className="text-xs font-medium text-muted">Months saved</p>
</div>
<p
className="mt-1 text-2xl font-bold text-accent"
role="status"
aria-live="polite"
>
{projection.monthsSaved > 0
? projection.monthsSaved
: projection.isFullPayoff
? '—'
: '0'}
</p>
</div>
<div className="rounded-lg border border-border bg-background/60 p-3">
<div className="flex items-center gap-1.5">
<DollarSign className="h-4 w-4 text-success" aria-hidden="true" />
<p className="text-xs font-medium text-muted">Interest saved</p>
</div>
<p
className="mt-1 text-2xl font-bold text-success"
role="status"
aria-live="polite"
>
{projection.interestSaved > 0
? formatMoney(projection.interestSaved)
: projection.isFullPayoff
? '—'
: formatMoney(0)}
</p>
</div>
</div>
<div className="space-y-2">
<div className="flex items-center justify-between">
<p className="text-xs font-medium text-muted">
Utilization
</p>
<span className="text-xs text-muted">
{projection.currentUtilizationPct}% → {projection.newUtilizationPct}%
</span>
</div>
<div className="relative h-2 w-full overflow-hidden rounded-full bg-border">
<div
className={`absolute left-0 top-0 h-full rounded-full transition-all duration-300 ${utilizationColor}`}
style={{ width: `${projection.currentUtilizationPct}%` }}
role="progressbar"
aria-valuenow={projection.currentUtilizationPct}
aria-valuemin={0}
aria-valuemax={100}
aria-label="Current utilization"
/>
</div>
<div className="relative h-2 w-full overflow-hidden rounded-full bg-border">
<div
className={`absolute left-0 top-0 h-full rounded-full transition-all duration-300 ${newUtilizationColor}`}
style={{ width: `${projection.newUtilizationPct}%` }}
role="progressbar"
aria-valuenow={projection.newUtilizationPct}
aria-valuemin={0}
aria-valuemax={100}
aria-label="New utilization after repayment"
/>
</div>
</div>
</div>
)}
<div className="rounded-lg border border-border bg-background/40 p-3">
{!isEditingPayment ? (
<div className="flex items-center justify-between">
<div>
<p className="text-xs font-medium text-muted">Monthly payment</p>
<p className="mt-0.5 text-sm font-semibold text-foreground">
{formatMoney(monthlyPayment)}
</p>
</div>
<button
type="button"
onClick={() => setIsEditingPayment(true)}
className="inline-flex h-8 w-8 items-center justify-center rounded-md text-muted transition-colors hover:bg-border hover:text-foreground focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent"
aria-label="Edit monthly payment amount"
>
<Pencil className="h-4 w-4" aria-hidden="true" />
</button>
</div>
) : (
<div className="flex items-center gap-2">
<div className="relative flex-1">
<span
className="pointer-events-none absolute left-2.5 top-1/2 -translate-y-1/2 text-sm text-muted"
aria-hidden="true"
>
$
</span>
<input
id="edit-monthly-payment"
type="number"
value={customMonthlyPayment ?? monthlyPayment}
onChange={(e) => {
const val = Number.parseFloat(e.target.value);
setCustomMonthlyPayment(Number.isFinite(val) && val > 0 ? val : null);
}}
className="w-full rounded-md border border-border bg-background px-3 py-1.5 pl-6 text-sm text-foreground outline-none transition-colors focus:border-accent focus:ring-1 focus:ring-accent"
aria-label="Assumed monthly payment"
min={1}
step={1}
autoFocus
/>
</div>
<button
type="button"
onClick={() => {
setIsEditingPayment(false);
if (customMonthlyPayment === null) {
setCustomMonthlyPayment(null);
}
}}
className="inline-flex h-8 items-center rounded-md px-2.5 text-xs font-medium text-accent transition-colors hover:bg-accent/10 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent"
>
Done
</button>
{customMonthlyPayment !== null && (
<button
type="button"
onClick={() => {
setCustomMonthlyPayment(null);
setIsEditingPayment(false);
}}
className="inline-flex h-8 items-center rounded-md px-2.5 text-xs font-medium text-muted transition-colors hover:bg-border focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent"
>
Reset
</button>
)}
</div>
)}
</div>
</section>
);
}