forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKycDrawer.tsx
More file actions
335 lines (303 loc) · 11.7 KB
/
Copy pathKycDrawer.tsx
File metadata and controls
335 lines (303 loc) · 11.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
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
/**
* KycDrawer — right-side panel (mobile: bottom sheet) showing KYC progress.
*
* Opened from the header via KycTriggerButton. Displays an ordered list of
* KYC steps with their statuses and a "Resume" CTA that links to the first
* incomplete step. Resumes where the user left off by reading `resumeStepId`
* from KycContext.
*
* Accessibility:
* role="dialog", aria-modal="true", aria-labelledby
* Full focus trap (useFocusTrap), scroll lock, inert backdrop
* Step icons: aria-hidden, status communicated via text + colour + shape
* Progress bar: role="progressbar" with valuenow/valuemin/valuemax
* WCAG 2.1 AA: 1.4.1, 2.1.1, 2.4.3, 4.1.2
*/
import React, { useRef } from 'react';
import { useKyc } from '../context/KycContext';
import { useFocusTrap } from '../hooks/useFocusTrap';
import { useBodyScrollLock } from '../hooks/useBodyScrollLock';
import { useInertBackdrop } from '../hooks/useInertBackdrop';
import type { KycOverallStatus, KycStep, KycStepStatus } from '../types/kyc';
import './KycDrawer.css';
// ─── Props ────────────────────────────────────────────────────────────────────
interface KycDrawerProps {
isOpen: boolean;
onClose: () => void;
/**
* Called when the user presses "Resume". Receives the step id to navigate
* to. The parent (or a router) handles the actual navigation.
*/
onResume: (stepId: string) => void;
/** Ref to the header trigger button for return-focus on close. */
triggerRef?: React.RefObject<HTMLElement | null>;
}
// ─── Status display metadata ──────────────────────────────────────────────────
const STATUS_META: Record<
KycStepStatus,
{ label: string; icon: string; ariaLabel: string }
> = {
not_started: { label: 'Not started', icon: '○', ariaLabel: 'Not started' },
in_progress: { label: 'In progress', icon: '◑', ariaLabel: 'In progress' },
completed: { label: 'Completed', icon: '✓', ariaLabel: 'Completed' },
failed: { label: 'Failed', icon: '✕', ariaLabel: 'Failed – action required' },
pending: { label: 'Under review',icon: '⋯', ariaLabel: 'Pending review' },
};
const OVERALL_STATUS_LABEL: Record<KycOverallStatus, string> = {
not_started: 'Not started',
in_progress: 'In progress',
under_review: 'Under review',
approved: 'Approved',
rejected: 'Rejected',
};
// ─── Helper: human-readable timestamp ────────────────────────────────────────
function fmtTimestamp(iso: string | undefined): string | null {
if (!iso) return null;
try {
return new Intl.DateTimeFormat('en-US', {
month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit',
}).format(new Date(iso));
} catch {
return null;
}
}
// ─── Step row ────────────────────────────────────────────────────────────────
function StepRow({
step,
isCurrentStep,
stepNumber,
}: {
step: KycStep;
isCurrentStep: boolean;
stepNumber: number;
}) {
const meta = STATUS_META[step.status];
const ts = fmtTimestamp(step.updatedAt);
return (
<li
className={`kyc-step kyc-step--${step.status}`}
aria-current={isCurrentStep ? 'step' : undefined}
>
{/* Icon — aria-hidden; status announced by the visually-hidden span */}
<div className="kyc-step__icon" aria-hidden="true">
{step.status === 'not_started' ? stepNumber : meta.icon}
</div>
<div className="kyc-step__content">
<p className="kyc-step__label">
{step.label}
{isCurrentStep && (
<span className="kyc-step__current-tag" aria-hidden="true">
Current
</span>
)}
{/* Screen-reader status announcement */}
<span className="sr-only"> — {meta.ariaLabel}</span>
</p>
<p className="kyc-step__description">{step.description}</p>
{ts && (
<p className="kyc-step__timestamp">
<span className="sr-only">Last updated: </span>
{ts}
</p>
)}
</div>
</li>
);
}
// ─── Main drawer ─────────────────────────────────────────────────────────────
const DRAWER_ID = 'kyc-progress-drawer';
export function KycDrawer({ isOpen, onClose, onResume, triggerRef }: KycDrawerProps) {
const { steps, overallStatus, resumeStepId, completedCount } = useKyc();
const containerRef = useFocusTrap({ isActive: isOpen, triggerRef, onEscape: onClose });
useBodyScrollLock({ isLocked: isOpen });
useInertBackdrop({ isInert: isOpen, modalId: DRAWER_ID });
if (!isOpen) return null;
const totalSteps = steps.length;
const progressPct = totalSteps > 0 ? Math.round((completedCount / totalSteps) * 100) : 0;
const canResume = resumeStepId !== null;
const isFullyDone = overallStatus === 'approved' || overallStatus === 'under_review';
// "Start" vs "Resume": show Start only when nothing has been touched yet.
const hasStarted = overallStatus !== 'not_started';
const handleResume = () => {
if (resumeStepId) {
onResume(resumeStepId);
onClose();
}
};
return (
<div id={DRAWER_ID} className="kyc-drawer-portal">
{/* Backdrop */}
<div
className="kyc-drawer-backdrop"
aria-hidden="true"
onClick={onClose}
/>
{/* Panel */}
<div
ref={containerRef}
role="dialog"
aria-modal="true"
aria-labelledby="kyc-drawer-title"
aria-describedby="kyc-drawer-desc"
className="kyc-drawer"
onKeyDown={e => { if (e.key === 'Escape') onClose(); }}
>
{/* ── Header ── */}
<div className="kyc-drawer__header">
<div className="kyc-drawer__header-text">
<p className="kyc-drawer__kicker">GrantFox · Verification</p>
<h2 id="kyc-drawer-title" className="kyc-drawer__title">
KYC Progress
</h2>
<p id="kyc-drawer-desc" className="kyc-drawer__subtitle">
Complete all steps to unlock your full credit limit.
</p>
</div>
<button
type="button"
className="kyc-drawer__close"
aria-label="Close KYC progress drawer"
onClick={onClose}
>
<svg width="16" height="16" viewBox="0 0 24 24" fill="none"
stroke="currentColor" strokeWidth="2"
strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<line x1="18" y1="6" x2="6" y2="18" />
<line x1="6" y1="6" x2="18" y2="18" />
</svg>
</button>
</div>
{/* ── Status row ── */}
<div className="kyc-drawer__status-row" aria-live="polite" aria-atomic="true">
<span
className={`kyc-status-badge kyc-status-badge--${overallStatus}`}
role="status"
>
{OVERALL_STATUS_LABEL[overallStatus]}
</span>
<span className="kyc-drawer__progress-text" aria-hidden="true">
{completedCount} / {totalSteps} steps
</span>
<span className="sr-only">
{completedCount} of {totalSteps} steps completed.
</span>
</div>
{/* ── Progress bar ── */}
<div
className="kyc-drawer__progress-bar-track"
role="progressbar"
aria-valuenow={progressPct}
aria-valuemin={0}
aria-valuemax={100}
aria-label={`KYC progress: ${progressPct}%`}
>
<div
className="kyc-drawer__progress-bar-fill"
style={{ width: `${progressPct}%` }}
/>
</div>
{/* ── Step list ── */}
<nav aria-label="KYC verification steps" className="kyc-drawer__body">
<ol style={{ listStyle: 'none', margin: 0, padding: 0 }}>
{steps.map((step, i) => (
<StepRow
key={step.id}
step={step}
isCurrentStep={step.id === resumeStepId}
stepNumber={i + 1}
/>
))}
</ol>
</nav>
{/* ── Footer ── */}
<div className="kyc-drawer__footer">
<button
type="button"
className="kyc-drawer__resume-btn"
onClick={handleResume}
disabled={!canResume}
aria-disabled={!canResume}
aria-describedby={!canResume ? 'kyc-resume-hint' : undefined}
>
{isFullyDone ? (
<>
<svg width="14" height="14" viewBox="0 0 24 24" fill="none"
stroke="currentColor" strokeWidth="2.5"
strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<polyline points="20 6 9 17 4 12" />
</svg>
All steps submitted
</>
) : (
<>
<svg width="14" height="14" viewBox="0 0 24 24" fill="none"
stroke="currentColor" strokeWidth="2.5"
strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<polygon points="5 3 19 12 5 21 5 3" />
</svg>
{hasStarted ? 'Resume verification' : 'Start verification'}
</>
)}
</button>
{!canResume && !isFullyDone && (
<p
id="kyc-resume-hint"
className="sr-only"
role="status"
>
All incomplete steps have been submitted for review.
</p>
)}
<a
href="/help#kyc"
className="kyc-drawer__help-link"
onClick={onClose}
>
Need help with verification?
</a>
</div>
</div>
</div>
);
}
// ─── Header trigger button ────────────────────────────────────────────────────
interface KycTriggerButtonProps {
onClick: () => void;
triggerRef: React.RefObject<HTMLButtonElement | null>;
}
/**
* Small header button that opens the KYC drawer.
* Shows a coloured dot indicator when there is outstanding action required.
*/
export function KycTriggerButton({ onClick, triggerRef }: KycTriggerButtonProps) {
const { overallStatus } = useKyc();
// Only show the dot for statuses that need user attention.
const showDot = overallStatus !== 'not_started' && overallStatus !== 'approved';
const dotLabel = showDot
? `KYC ${OVERALL_STATUS_LABEL[overallStatus].toLowerCase()}`
: '';
return (
<button
ref={triggerRef}
type="button"
className="kyc-trigger-btn"
onClick={onClick}
aria-label={`KYC verification${dotLabel ? ` — ${dotLabel}` : ''}`}
aria-haspopup="dialog"
>
{/* Shield icon */}
<svg width="15" height="15" viewBox="0 0 24 24" fill="none"
stroke="currentColor" strokeWidth="2"
strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z" />
</svg>
<span>KYC</span>
{showDot && (
<span
className={`kyc-trigger-btn__dot kyc-trigger-btn__dot--${overallStatus}`}
aria-hidden="true"
/>
)}
</button>
);
}