forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitCreationWizard.tsx
More file actions
378 lines (345 loc) · 15.1 KB
/
Copy pathSplitCreationWizard.tsx
File metadata and controls
378 lines (345 loc) · 15.1 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import { useState, useEffect, useCallback, useRef } from 'react';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { Save, ArrowLeft, ArrowRight, CheckCircle } from 'lucide-react';
import { StepIndicator } from './StepIndicator';
import { BasicInfoStep } from './steps/BasicInfoStep';
import { SplitMethodStep } from './steps/SplitMethodStep';
import { ParticipantsStep } from './steps/ParticipantsStep';
import { ItemsStep } from './steps/ItemsStep';
import { TaxTipStep } from './steps/TaxTipStep';
import { ReviewStep } from './steps/ReviewStep';
import {
validateBasicInfo,
validateParticipants,
validateItems,
} from './validators';
import { useWallet } from '../../hooks/use-wallet';
import type { WizardState } from '../../types/wizard';
import { INITIAL_WIZARD_STATE } from '../../types/wizard';
import { calculateWizardSplit } from '../../utils/split-calculations';
import {
createActivityRecord,
createSplit,
getApiErrorMessage,
getApiFieldErrors,
} from '../../utils/api-client';
import { storeSplitParticipantDirectory } from '../../utils/session';
import { draftRegistry } from '../../services/draftRegistry';
const loadDraft = (): WizardState => {
draftRegistry.migrateWizardDraft();
const data = draftRegistry.load('wizard');
return data || INITIAL_WIZARD_STATE;
};
export const SplitCreationWizard = () => {
const { t } = useTranslation();
const navigate = useNavigate();
const announceRef = useRef<HTMLDivElement>(null);
const { activeUserId } = useWallet();
const [wizardState, setWizardState] = useState<WizardState>(loadDraft);
const [currentStep, setCurrentStep] = useState(0);
const [errors, setErrors] = useState<Record<string, string>>({});
const [isSubmitting, setIsSubmitting] = useState(false);
const [draftSaved, setDraftSaved] = useState(false);
const isItemized = wizardState.splitMethod === 'itemized';
const ALL_STEPS = [
{ label: t('wizard.steps.basicInfo') },
{ label: t('wizard.steps.splitMethod') },
{ label: t('wizard.steps.participants') },
...(isItemized ? [{ label: t('wizard.steps.items') }] : []),
{ label: t('wizard.steps.taxTip') },
{ label: t('wizard.steps.review') },
];
// Map logical step index to a stable step id
const STEP_IDS = [
'basicInfo',
'splitMethod',
'participants',
...(isItemized ? ['items'] : []),
'taxTip',
'review',
];
const currentStepId = STEP_IDS[currentStep];
const totalSteps = ALL_STEPS.length;
// Announce step changes to screen readers
useEffect(() => {
if (announceRef.current) {
announceRef.current.textContent = `Step ${currentStep + 1} of ${totalSteps}: ${ALL_STEPS[currentStep].label}`;
}
}, [currentStep, totalSteps, ALL_STEPS]);
// Auto-save draft on every state change
useEffect(() => {
draftRegistry.save('wizard', wizardState, 'wizard', wizardState.title || 'Split Wizard Draft');
}, [wizardState]);
const patch = useCallback((p: Partial<WizardState>) => {
setWizardState((prev) => ({ ...prev, ...p }));
setErrors({});
}, []);
const generateUuid = () => {
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
return crypto.randomUUID();
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (char) => {
const random = Math.random() * 16 | 0;
const value = char === 'x' ? random : (random & 0x3 | 0x8);
return value.toString(16);
});
};
const ensureUuid = (value: string) => {
return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value)
? value
: generateUuid();
};
const validateCurrentStep = (): boolean => {
let stepErrors: Record<string, string> = {};
if (currentStepId === 'basicInfo') {
stepErrors = validateBasicInfo(wizardState, t);
} else if (currentStepId === 'participants') {
stepErrors = validateParticipants(wizardState, t);
} else if (currentStepId === 'items') {
stepErrors = validateItems(wizardState, t);
}
setErrors(stepErrors);
return Object.keys(stepErrors).length === 0;
};
const handleNext = () => {
if (!validateCurrentStep()) return;
if (currentStep < totalSteps - 1) {
setCurrentStep((s) => s + 1);
setErrors({});
}
};
const handleBack = () => {
if (currentStep > 0) {
setCurrentStep((s) => s - 1);
setErrors({});
}
};
const handleSaveDraft = () => {
draftRegistry.save('wizard', wizardState, 'wizard', wizardState.title || 'Split Wizard Draft');
setDraftSaved(true);
setTimeout(() => setDraftSaved(false), 2000);
};
const handleSubmit = async () => {
if (!validateCurrentStep()) return;
if (!activeUserId) {
setErrors({
submit: 'Connect your wallet before creating a split.',
});
return;
}
setIsSubmitting(true);
try {
const participantsWithApiIds = wizardState.participants.map((participant) => ({
...participant,
apiId: ensureUuid(participant.id),
}));
const calculation = calculateWizardSplit({
...wizardState,
participants: participantsWithApiIds.map((participant) => ({
...participant,
id: participant.apiId,
})),
});
const shareMap = new Map(
calculation.shares.map((share) => [share.participantId, share.total]),
);
const createdSplit = await createSplit({
totalAmount: calculation.grandTotal,
description: wizardState.title.trim(),
creatorWalletAddress: activeUserId,
preferredCurrency: wizardState.currency,
participants: participantsWithApiIds.map((participant) => ({
userId: participant.apiId,
amountOwed: shareMap.get(participant.apiId) ?? 0,
walletAddress: participant.walletAddress?.trim() || undefined,
})),
items: wizardState.splitMethod === 'itemized'
? wizardState.items.map((item) => ({
name: item.name.trim(),
quantity: 1,
unitPrice: item.price,
totalPrice: item.price,
assignedToIds: (item.assignedTo.length > 0 ? item.assignedTo : participantsWithApiIds.map((participant) => participant.id))
.map((participantId) => {
const participant = participantsWithApiIds.find((candidate) => candidate.id === participantId);
return participant?.apiId;
})
.filter((participantId): participantId is string => Boolean(participantId)),
}))
: undefined,
});
storeSplitParticipantDirectory(
createdSplit.id,
participantsWithApiIds.reduce<Record<string, { name: string; email?: string; walletAddress?: string }>>(
(directory, participant) => {
directory[participant.apiId] = {
name: participant.name.trim() || participant.apiId,
email: participant.email?.trim() || undefined,
walletAddress: participant.walletAddress?.trim() || undefined,
};
return directory;
},
{},
),
);
await createActivityRecord({
userId: activeUserId,
activityType: 'split_created',
splitId: createdSplit.id,
metadata: {
title: wizardState.title.trim(),
totalAmount: calculation.grandTotal,
currency: wizardState.currency,
participantCount: wizardState.participants.length,
},
}).catch(() => undefined);
draftRegistry.delete('wizard');
navigate(`/split/${createdSplit.id}`);
} catch (error) {
const fieldErrors = getApiFieldErrors(error);
setErrors(
Object.keys(fieldErrors).length > 0
? fieldErrors
: { submit: getApiErrorMessage(error) },
);
} finally {
setIsSubmitting(false);
}
};
const renderStep = () => {
switch (currentStepId) {
case 'basicInfo':
return (
<BasicInfoStep
value={wizardState}
onChange={patch}
errors={errors}
/>
);
case 'splitMethod':
return (
<SplitMethodStep
value={wizardState}
onChange={patch}
/>
);
case 'participants':
return (
<ParticipantsStep
value={wizardState}
onChange={patch}
errors={errors}
/>
);
case 'items':
return (
<ItemsStep
value={wizardState}
onChange={patch}
errors={errors}
/>
);
case 'taxTip':
return (
<TaxTipStep
value={wizardState}
onChange={patch}
errors={errors}
/>
);
case 'review':
return <ReviewStep value={wizardState} />;
default:
return null;
}
};
const isLastStep = currentStep === totalSteps - 1;
return (
<div className="min-h-screen bg-gray-50 dark:bg-gray-900">
{/* Live region for step announcements */}
<div
ref={announceRef}
role="status"
aria-live="polite"
aria-atomic="true"
className="sr-only"
/>
{/* Header */}
<div className="sticky top-0 z-20 bg-white dark:bg-gray-800 border-b border-gray-100 dark:border-gray-700 shadow-sm">
<div className="max-w-lg mx-auto px-4 py-3 flex items-center justify-between">
<button
type="button"
onClick={() => navigate('/dashboard')}
className="p-2 rounded-full text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors focus:outline-none focus:ring-2 focus:ring-[var(--color-primary)]"
aria-label={t('common.backToDashboard')}
>
<ArrowLeft size={20} aria-hidden="true" />
</button>
<h1 className="text-base font-bold text-gray-900 dark:text-gray-100">
{t('wizard.pageTitle')}
</h1>
<button
type="button"
onClick={handleSaveDraft}
className={`flex items-center gap-1.5 text-xs font-semibold px-3 py-1.5 rounded-lg transition-all
${draftSaved
? 'bg-green-100 dark:bg-green-900/30 text-green-600 dark:text-green-400'
: 'bg-gray-100 dark:bg-gray-700 text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-600'
}`}
aria-pressed={draftSaved}
>
<Save size={13} aria-hidden="true" />
{draftSaved ? t('wizard.draftSaved') : t('wizard.saveDraft')}
</button>
</div>
<StepIndicator steps={ALL_STEPS} currentStep={currentStep} />
</div>
{/* Step content */}
<div className="max-w-lg mx-auto px-4 py-6">
{errors.submit ? (
<div className="mb-4 rounded-xl border border-red-200 bg-red-50 px-4 py-3 text-sm text-red-700">
{errors.submit}
</div>
) : null}
{renderStep()}
</div>
{/* Navigation footer */}
<div className="sticky bottom-0 z-20 bg-white dark:bg-gray-800 border-t border-gray-100 dark:border-gray-700 shadow-[0_-2px_12px_rgba(0,0,0,0.06)]">
<div className="max-w-lg mx-auto px-4 py-4 flex items-center gap-3">
{currentStep > 0 && (
<button
type="button"
onClick={handleBack}
className="flex items-center gap-2 px-5 py-3 rounded-xl border border-gray-200 dark:border-gray-600 text-gray-700 dark:text-gray-300 font-semibold text-sm hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors min-h-[44px] focus:outline-none focus:ring-2 focus:ring-[var(--color-primary)]"
>
<ArrowLeft size={16} aria-hidden="true" />
{t('wizard.back')}
</button>
)}
<button
type="button"
onClick={isLastStep ? handleSubmit : handleNext}
disabled={isSubmitting}
aria-busy={isSubmitting}
className="flex-1 flex items-center justify-center gap-2 py-3 rounded-xl bg-[var(--color-primary)] text-white font-bold text-sm hover:opacity-90 active:scale-[0.98] transition-all min-h-[44px] disabled:opacity-60 disabled:cursor-not-allowed focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-[var(--color-primary)]"
>
{isSubmitting ? (
<span className="animate-pulse">{t('wizard.creating')}</span>
) : isLastStep ? (
<>
<CheckCircle size={16} aria-hidden="true" />
{t('wizard.createSplit')}
</>
) : (
<>
{t('wizard.next')}
<ArrowRight size={16} aria-hidden="true" />
</>
)}
</button>
</div>
</div>
</div>
);
};