-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathCreditLineDetailDrawer.tsx
More file actions
302 lines (283 loc) · 11.3 KB
/
Copy pathCreditLineDetailDrawer.tsx
File metadata and controls
302 lines (283 loc) · 11.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
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
import React, { useMemo } from 'react';
import { X, ExternalLink, Calendar, Activity } from 'lucide-react';
import { useFocusTrap } from '../hooks/useFocusTrap';
import { useBodyScrollLock } from '../hooks/useBodyScrollLock';
import { useInertBackdrop } from '../hooks/useInertBackdrop';
import { StatusBadge } from './StatusBadge';
import {
COLOR,
fmt,
fmtDate,
utilizationPct,
getUtilizationLevel,
RISK_COLOR,
UTIL_COLOR,
} from '../utils/tokens';
import { timeAgo } from '../utils/dates';
import type { CreditLine } from '../types/creditLine';
import './CreditLineDetailDrawer.css';
interface CreditLineDetailDrawerProps {
line: CreditLine;
onClose: () => void;
}
export function CreditLineDetailDrawer({ line, onClose }: CreditLineDetailDrawerProps) {
// Lock body scroll when drawer is open
useBodyScrollLock({ isLocked: true });
// Make background content inert to screen readers
useInertBackdrop({ isInert: true, modalId: 'cl-details-drawer' });
// Trap focus inside the drawer
const drawerRef = useFocusTrap({
isActive: true,
onEscape: onClose,
});
const pct = utilizationPct(line.utilized, line.limit);
const utilizationLevel = getUtilizationLevel(line.utilized, line.limit);
const available = line.limit - line.utilized;
// Reconstruct running balances for sparkline
const runningBalances = useMemo(() => {
// Sort transactions by date descending (newest first)
const sortedTx = [...line.transactions].sort(
(a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
);
let current = line.utilized;
const history = [current];
for (const tx of sortedTx) {
if (tx.status === 'Failed') continue;
let change = 0;
if (tx.type === 'Draw') change = tx.amount;
else if (tx.type === 'Repay') change = -tx.amount;
else if (tx.type === 'Fee' || tx.type === 'Interest') change = tx.amount;
current -= change;
history.unshift(current);
}
// Ensure all values are non-negative
return history.map((v) => Math.max(0, v));
}, [line]);
// Generate SVG path for the sparkline
const sparklinePath = useMemo(() => {
if (runningBalances.length < 2) return '';
const maxVal = Math.max(...runningBalances, line.limit, 100);
const minVal = 0;
const range = maxVal - minVal;
const width = 360;
const height = 80;
return runningBalances
.map((val, idx) => {
const x = (idx / (runningBalances.length - 1)) * width;
const y = height - 10 - ((val - minVal) / range) * (height - 20);
return `${idx === 0 ? 'M' : 'L'} ${x.toFixed(1)} ${y.toFixed(1)}`;
})
.join(' ');
}, [runningBalances, line.limit]);
return (
<div className="cl-drawer-overlay" onClick={onClose}>
<div
id="cl-details-drawer"
ref={drawerRef}
className="cl-drawer-container"
role="dialog"
aria-modal="true"
aria-labelledby="cl-drawer-title"
aria-describedby="cl-drawer-desc"
onClick={(e) => e.stopPropagation()}
tabIndex={-1}
>
{/* Drawer Header */}
<header className="cl-drawer-header">
<div>
<h2 id="cl-drawer-title" className="cl-drawer-title">
{line.name}
</h2>
<p id="cl-drawer-desc" className="cl-drawer-id">
{line.id}
</p>
</div>
<div className="cl-drawer-header-actions">
<StatusBadge status={line.status} />
<button
className="cl-drawer-close-btn"
onClick={onClose}
aria-label="Close details drawer"
>
<X size={20} />
</button>
</div>
</header>
{/* Drawer Content */}
<div className="cl-drawer-content">
{/* Main metrics summary */}
<section className="cl-drawer-section cl-metrics-grid">
<div className="cl-drawer-metric">
<span className="label">Credit Limit</span>
<span className="value limit-val">{fmt(line.limit)}</span>
</div>
<div className="cl-drawer-metric">
<span className="label">Utilized Balance</span>
<span
className="value utilized-val"
style={{ color: UTIL_COLOR[utilizationLevel] }}
>
{fmt(line.utilized)}
</span>
</div>
<div className="cl-drawer-metric">
<span className="label">Available Limit</span>
<span className="value available-val" style={{ color: COLOR.success }}>
{fmt(available)}
</span>
</div>
<div className="cl-drawer-metric">
<span className="label">Interest Rate</span>
<span className="value">{line.apr.toFixed(2)}% APR</span>
</div>
</section>
{/* Utilization Progress Bar */}
<section className="cl-drawer-section">
<div className="cl-drawer-progress-header">
<span>Utilization Level</span>
<span style={{ color: UTIL_COLOR[utilizationLevel], fontWeight: 600 }}>
{pct}% ({utilizationLevel})
</span>
</div>
<div
className="cl-drawer-progress-track"
role="progressbar"
aria-valuenow={pct}
aria-valuemin={0}
aria-valuemax={100}
aria-label="Utilization percentage"
>
<div
className="cl-drawer-progress-fill"
style={{ width: `${pct}%`, background: UTIL_COLOR[utilizationLevel] }}
/>
</div>
</section>
{/* Sparkline Visualization */}
{runningBalances.length >= 2 && (
<section className="cl-drawer-section">
<h3 className="section-title">
<Activity size={16} aria-hidden="true" /> Utilization Trend
</h3>
<div className="cl-drawer-sparkline-container">
<svg className="cl-drawer-sparkline" viewBox="0 0 360 80" aria-label="Sparkline showing credit line balance history">
<defs>
<linearGradient id="sparkline-gradient" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor="var(--accent)" stopOpacity="0.25" />
<stop offset="100%" stopColor="var(--accent)" stopOpacity="0" />
</linearGradient>
</defs>
{sparklinePath && (
<>
<path
d={sparklinePath}
fill="none"
stroke="var(--accent)"
strokeWidth="2.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
<path
d={`${sparklinePath} L 360 80 L 0 80 Z`}
fill="url(#sparkline-gradient)"
/>
</>
)}
</svg>
<div className="cl-drawer-sparkline-legend">
<span>Start</span>
<span>{fmt(line.utilized)} (Current)</span>
</div>
</div>
</section>
)}
{/* Additional details */}
<section className="cl-drawer-section additional-details">
<h3 className="section-title">Facility Information</h3>
<div className="details-list">
<div className="detail-row">
<span className="label">Risk Score</span>
<span
className="value score-val"
style={{ color: RISK_COLOR(line.riskScore), fontWeight: 600 }}
>
{line.riskScore}
</span>
</div>
<div className="detail-row">
<span className="label">Collateral Backing</span>
<span className="value">{line.collateral || 'Unsecured / None'}</span>
</div>
<div className="detail-row">
<span className="label">Opened Date</span>
<span className="value">{fmtDate(line.openedAt)}</span>
</div>
<div className="detail-row">
<span className="label">Last Activity</span>
<span className="value">{timeAgo(line.updatedAt)}</span>
</div>
{line.nextPaymentDate && line.nextPaymentAmount && (
<div className="detail-row payment-info">
<span className="label flex-label">
<Calendar size={14} aria-hidden="true" /> Next Payment Due
</span>
<span className="value">
{fmt(line.nextPaymentAmount)} on {fmtDate(line.nextPaymentDate)}
</span>
</div>
)}
</div>
</section>
{/* Ledger / Transaction History */}
<section className="cl-drawer-section transactions-history">
<h3 className="section-title">Transaction History</h3>
{line.transactions.length === 0 ? (
<p className="no-transactions">No transactions recorded for this facility.</p>
) : (
<div className="tx-list" role="feed" aria-label="Transaction history feed">
{line.transactions.map((tx) => {
const isPositive = tx.type === 'Repay';
const amountSign = isPositive ? '+' : '-';
const amountColor = isPositive ? COLOR.success : COLOR.text;
return (
<div key={tx.id} className="tx-item" role="article">
<div className="tx-item-header">
<div className="tx-meta">
<span className="tx-type">{tx.type}</span>
<span className="tx-date" title={fmtDate(tx.date)}>
{timeAgo(tx.date)}
</span>
</div>
<span className="tx-amount" style={{ color: amountColor }}>
{amountSign}
{fmt(tx.amount)}
</span>
</div>
{tx.note && <p className="tx-note">{tx.note}</p>}
<div className="tx-footer">
<span className={`tx-status status-${tx.status.toLowerCase()}`}>
{tx.status}
</span>
{tx.txHash && (
<a
href={`https://stellar.expert/explorer/public/tx/${tx.txHash}`}
target="_blank"
rel="noopener noreferrer"
className="tx-explorer-link"
aria-label={`View transaction ${tx.id} on Stellar Explorer`}
>
Explorer <ExternalLink size={12} aria-hidden="true" />
</a>
)}
</div>
</div>
);
})}
</div>
)}
</section>
</div>
</div>
</div>
);
}