-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathDashboardTour.tsx
More file actions
314 lines (286 loc) · 10.9 KB
/
Copy pathDashboardTour.tsx
File metadata and controls
314 lines (286 loc) · 10.9 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
import { useEffect, useRef, useState, useCallback } from 'react';
import type { CSSProperties } from 'react';
import { useFocusTrap } from '../hooks/useFocusTrap';
import './DashboardTour.css';
type Step = 'riskGauge' | 'summaryCards' | 'requestEvaluation' | 'notificationBell';
interface CalloutStyle extends CSSProperties {
'--callout-top'?: string;
'--callout-left'?: string;
'--callout-transform'?: string;
'--arrow-top'?: string;
'--arrow-left'?: string;
}
const STEPS: { id: Step; label: string; hint: string }[] = [
{
id: 'riskGauge',
label: 'Risk Score',
hint: 'Your composite risk score across all credit lines, updated in real time. Higher scores unlock better rates.',
},
{
id: 'summaryCards',
label: 'Credit Summary',
hint: 'An at-a-glance view of your total limit, utilisation, and available credit across every open line.',
},
{
id: 'requestEvaluation',
label: 'Request Evaluation',
hint: 'No credit lines yet? Tap here to start a new evaluation based on your on-chain activity.',
},
{
id: 'notificationBell',
label: 'Notifications',
hint: 'Payment reminders, utilisation warnings, and line-status alerts all land here.',
},
];
const LS_DASHBOARD_TOUR = 'dashboard_tour_completed';
const LS_ONBOARDING = 'onboarding_completed';
export function DashboardTour() {
const [stepIndex, setStepIndex] = useState(0);
const [highlightRect, setHighlightRect] = useState<DOMRect | null>(null);
const [tourActive, setTourActive] = useState(false);
const [targetRect, setTargetRect] = useState<DOMRect | null>(null);
// Mutable cache that always reads the current value without relying on
// a stale closure (useCallback deps are stable setter fn refs).
const lastKnownRectsRef = useRef<Record<Step, DOMRect | null>>({
riskGauge: null,
summaryCards: null,
requestEvaluation: null,
notificationBell: null,
});
const skipRef = useRef<HTMLButtonElement>(null);
const nextRef = useRef<HTMLButtonElement>(null);
const containerRef = useFocusTrap({ isActive: tourActive });
const dismissedRef = useRef(false);
const currentStep = STEPS[stepIndex];
/**
* Reads the live bounding rect for `id`. Falls back to the cached value
* when the DOM element is temporarily absent (e.g. during a React
* re-render that briefly removes conditional content).
*/
const snapshot = useCallback((id: Step): DOMRect | null => {
const el = document.querySelector<HTMLElement>(`[data-tour-target="${id}"]`);
if (!el) return lastKnownRectsRef.current[id] ?? null;
const r = el.getBoundingClientRect();
// Only cache when the rect has non-zero geometry (jsdom returns
// all-zeros for elements not yet laid out).
if (r.width > 0 || r.height > 0 || r.x !== 0 || r.y !== 0) {
lastKnownRectsRef.current[id] = r;
return r;
}
return lastKnownRectsRef.current[id] ?? r;
}, []);
const dismiss = useCallback(() => {
if (dismissedRef.current) return;
dismissedRef.current = true;
try {
localStorage.setItem(LS_DASHBOARD_TOUR, 'true');
} catch {
/* quota or private mode — swallow */
}
setTourActive(false);
setTargetRect(null);
setHighlightRect(null);
}, []);
/**
* Measure all targets synchronously on mount and push into the cache,
* then render the first step's dialog immediately.
*/
useEffect(() => {
const onboardingDone = localStorage.getItem(LS_ONBOARDING) === 'true';
const tourDone = localStorage.getItem(LS_DASHBOARD_TOUR) === 'true';
if (!onboardingDone || tourDone) return;
const ids: Step[] = ['riskGauge', 'summaryCards', 'requestEvaluation', 'notificationBell'];
ids.forEach(id => {
const el = document.querySelector<HTMLElement>(`[data-tour-target="${id}"]`);
if (el) lastKnownRectsRef.current[id] = el.getBoundingClientRect();
});
const firstRect = lastKnownRectsRef.current.riskGauge
?? lastKnownRectsRef.current.summaryCards
?? lastKnownRectsRef.current.requestEvaluation
?? lastKnownRectsRef.current.notificationBell
?? null;
if (firstRect) {
setTargetRect(firstRect);
setHighlightRect(firstRect);
}
setTourActive(true);
}, []);
/**
* Update highlight + target rects whenever the step changes.
* We do this synchronously to avoid a "flash of empty" between step
* transitions. A ResizeObserver keeps the cached rects fresh.
*/
useEffect(() => {
if (!tourActive) return;
// Synchronous DOM read — no rAF needed
const rect = snapshot(currentStep.id);
if (rect) {
setTargetRect(rect);
setHighlightRect(rect);
}
}, [tourActive, stepIndex, currentStep.id, snapshot]);
/**
* Observe each target DOM node for size/layout changes.
* Falls back gracefully without ResizeObserver.
*/
useEffect(() => {
if (!tourActive) return;
let ro: ResizeObserver | null = null;
if (typeof ResizeObserver !== 'undefined') {
ro = new ResizeObserver(() => {
if (dismissedRef.current) return;
const rect = snapshot(currentStep.id);
if (rect) {
setTargetRect(rect);
lastKnownRectsRef.current[currentStep.id] = rect;
setHighlightRect(rect);
}
});
const toc = document.querySelector(`[data-tour-target="${currentStep.id}"]`);
if (toc) ro.observe(toc);
}
const onScroll = () => {
if (dismissedRef.current) return;
const rect = snapshot(currentStep.id);
if (rect) {
setTargetRect(rect);
lastKnownRectsRef.current[currentStep.id] = rect;
setHighlightRect(rect);
}
};
window.addEventListener('scroll', onScroll, true);
window.addEventListener('resize', onScroll);
return () => {
ro?.disconnect();
window.removeEventListener('scroll', onScroll, true);
window.removeEventListener('resize', onScroll);
};
}, [tourActive, stepIndex, currentStep.id, snapshot]);
/**
* Focus the appropriate action button when the dialog first opens or
* when the step changes.
*/
useEffect(() => {
if (!tourActive) return;
const t = setTimeout(() => {
(stepIndex === STEPS.length - 1 ? nextRef : skipRef)?.current?.focus();
}, 60);
return () => clearTimeout(t);
}, [tourActive, stepIndex]);
/**
* Dismiss on Escape key.
*/
useEffect(() => {
if (!tourActive) return;
const handler = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
e.preventDefault();
dismiss();
}
};
document.addEventListener('keydown', handler);
return () => document.removeEventListener('keydown', handler);
}, [tourActive, dismiss]);
if (!tourActive || !highlightRect) return null;
const isLast = stepIndex === STEPS.length - 1;
const hlTop = highlightRect.top;
const hlLeft = highlightRect.left;
const hlWidth = highlightRect.width;
const hlHeight = highlightRect.height;
let calloutStyle: CalloutStyle = {};
let arrowStyle: CSSProperties = {};
const GAP = 14;
if (currentStep.id === 'riskGauge' || currentStep.id === 'requestEvaluation') {
const right = window.innerWidth - highlightRect.right;
const fitsRight = right > 320;
calloutStyle = fitsRight
? { top: `${highlightRect.top + highlightRect.height / 2}px`, left: `${highlightRect.right + GAP}px`, transform: 'translateY(-50%)' }
: { top: `${highlightRect.bottom + GAP}px`, left: `${highlightRect.left}px` };
arrowStyle = fitsRight
? { top: '50%', left: '-6px', transform: 'translateY(-50%) rotate(45deg)' }
: { top: '-6px', left: `${Math.min(highlightRect.width / 2 - 8, 300 - 16)}px`, transform: 'rotate(45deg)' };
} else if (currentStep.id === 'summaryCards') {
const below = window.innerHeight - highlightRect.bottom;
const fitsBelow = below > 220;
calloutStyle = fitsBelow
? { top: `${highlightRect.bottom + GAP}px`, left: `${highlightRect.left}px` }
: { bottom: `${window.innerHeight - highlightRect.top + GAP}px`, left: `${highlightRect.left}px` };
arrowStyle = fitsBelow
? { top: '-6px', left: `${Math.min(highlightRect.width / 2 - 8, 300 - 16)}px`, transform: 'rotate(45deg)' }
: { bottom: '-6px', left: `${Math.min(highlightRect.width / 2 - 8, 300 - 16)}px`, transform: 'rotate(45deg)' };
} else if (currentStep.id === 'notificationBell') {
const fitsRight = window.innerWidth - highlightRect.right > 320;
calloutStyle = fitsRight
? { bottom: `${window.innerHeight - highlightRect.top + GAP}px`, right: `${window.innerWidth - highlightRect.right}px` }
: { bottom: `${window.innerHeight - highlightRect.top + GAP}px`, left: `${highlightRect.left}px` };
arrowStyle = fitsRight
? { bottom: '-6px', right: '16px', transform: 'rotate(45deg)' }
: { bottom: '-6px', left: `${Math.min(highlightRect.width / 2 - 8, 300 - 16)}px`, transform: 'rotate(45deg)' };
}
return (
<div
className="dt-overlay"
role="dialog"
aria-modal="true"
aria-label={`Dashboard tour step ${stepIndex + 1} of ${STEPS.length}: ${currentStep.label}`}
>
<div
className="dt-highlight-ring"
style={{ top: `${hlTop}px`, left: `${hlLeft}px`, width: `${hlWidth}px`, height: `${hlHeight}px` }}
/>
<div
ref={containerRef}
className="dt-callout"
style={calloutStyle}
aria-describedby={`dt-hint-${currentStep.id}`}
>
<div className="dt-callout-arrow" style={arrowStyle} />
<div className="dt-callout-inner">
<p className="dt-step-label">{currentStep.label}</p>
<p className="dt-callout-text" id={`dt-hint-${currentStep.id}`}>
{currentStep.hint}
</p>
<div className="dt-footer">
<span className="dt-step-counter">
{stepIndex + 1} / {STEPS.length}
</span>
<div className="dt-actions">
<button
ref={skipRef}
type="button"
className="dt-skip-btn"
onClick={dismiss}
aria-label="Skip dashboard tour"
>
Skip
</button>
<button
ref={nextRef}
type="button"
className="dt-next-btn"
onClick={() => {
dismissedRef.current = false;
if (stepIndex < STEPS.length - 1) {
setStepIndex(i => i + 1);
} else {
try {
localStorage.setItem(LS_DASHBOARD_TOUR, 'true');
} catch {
/* swallow */
}
setTourActive(false);
setTargetRect(null);
setHighlightRect(null);
}
}}
aria-label={isLast ? 'Finish dashboard tour' : 'Go to next step'}
>
{isLast ? 'Got it' : 'Next'}
</button>
</div>
</div>
</div>
</div>
</div>
);
}