forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnboardingFlow.tsx
More file actions
167 lines (148 loc) · 5 KB
/
Copy pathOnboardingFlow.tsx
File metadata and controls
167 lines (148 loc) · 5 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
import { useCallback, useEffect, useRef, useState } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { usePrefersReducedMotion } from '../hooks/usePrefersReducedMotion';
import './OnboardingFlow.css';
interface Props {
/** Whether the onboarding modal is visible. */
isOpen: boolean;
/**
* Invoked after the final step. The component writes
* `localStorage.onboarding_completed = 'true'` before calling this so
* returning users skip the flow on subsequent connects.
*/
onComplete: () => void;
/**
* Invoked when the user opts out via the Skip affordance. Skipping
* does NOT mark onboarding as complete — the next session will see
* the flow again, which is intentional. The trade-off is documented
* in `docs/UX_RATIONALE.md` "Single onboarding stepper, not separate
* modals".
*/
onSkip: () => void;
}
const steps = [
{
title: 'Welcome to Creditra',
description: 'Your adaptive credit protocol on Stellar blockchain',
icon: '👋'
},
{
title: 'Credit Evaluation',
description: 'We analyze your on-chain activity to determine your credit limit and terms',
icon: '📊'
},
{
title: 'Flexible Credit Lines',
description: 'Draw and repay credit as needed with dynamic interest rates based on your risk profile',
icon: '💳'
}
];
export const OnboardingFlow = ({ isOpen, onComplete, onSkip }: Props) => {
const [currentStep, setCurrentStep] = useState(0);
const prefersReducedMotion = usePrefersReducedMotion();
useEffect(() => {
if (isOpen) {
setCurrentStep(0);
setDirection('forward');
}
}, [isOpen]);
const isLastStep = currentStep === steps.length - 1;
const isFirstStep = currentStep === 0;
const step = steps[currentStep];
const handleNext = useCallback(() => {
setDirection('forward');
setCurrentStep((current) => {
if (current === steps.length - 1) {
localStorage.setItem('onboarding_completed', 'true');
onComplete();
return current;
}
return current + 1;
});
}, [onComplete]);
const handleBack = useCallback(() => {
setDirection('backward');
setCurrentStep((current) => Math.max(0, current - 1));
}, []);
const handleSkip = useCallback(() => {
localStorage.setItem('onboarding_completed', 'true');
onSkip();
}, [onSkip]);
useEffect(() => {
if (!isOpen) return;
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
event.preventDefault();
handleSkip();
}
if (event.key === 'ArrowRight') {
event.preventDefault();
handleNext();
}
if (event.key === 'ArrowLeft') {
event.preventDefault();
handleBack();
}
};
document.addEventListener('keydown', handleKeyDown);
return () => document.removeEventListener('keydown', handleKeyDown);
}, [handleBack, handleNext, handleSkip, isOpen]);
return (
<div className="onboarding-overlay" role="dialog" aria-modal="true" aria-label="Onboarding">
<div className="onboarding-content">
<button className="skip-btn" onClick={handleSkip} aria-label="Skip onboarding">
Skip
</button>
<div className="progress-label" aria-live="polite">
Step {currentStep + 1} of {steps.length}
</div>
<div className="step-container">
<AnimatePresence mode="wait">
<motion.div
key={currentStep}
className="onboarding-step"
initial={prefersReducedMotion ? { opacity: 1, x: 0 } : { opacity: 0, x: 20 }}
animate={{ opacity: 1, x: 0 }}
exit={prefersReducedMotion ? { opacity: 1, x: 0 } : { opacity: 0, x: -20 }}
transition={{ duration: prefersReducedMotion ? 0 : 0.3 }}
>
<div className="step-icon">{step.icon}</div>
<h2>{step.title}</h2>
<p>{step.description}</p>
</motion.div>
</AnimatePresence>
</div>
<div className="step-indicators" role="list">
{steps.map((_, index) => (
<div
key={index}
role="listitem"
className={`indicator ${index === currentStep ? 'active' : ''} ${index < currentStep ? 'completed' : ''}`}
aria-current={index === currentStep ? 'step' : undefined}
aria-label={`Step ${index + 1}`}
>
{index < currentStep ? '✓' : index + 1}
</div>
))}
</div>
<div className="button-group">
<button
className="secondary-btn"
onClick={handleBack}
disabled={isFirstStep}
aria-label="Go back"
>
Back
</button>
<button
className="primary-btn"
onClick={handleNext}
aria-label={isLastStep ? 'Get started' : 'Next step'}
>
{isLastStep ? 'Get Started' : 'Next'}
</button>
</div>
</div>
</div>
);
};