forked from Fluxora-Org/Fluxora-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamTimeline.tsx
More file actions
378 lines (350 loc) · 12.5 KB
/
Copy pathStreamTimeline.tsx
File metadata and controls
378 lines (350 loc) · 12.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
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 React from "react";
import { useTranslation } from "react-i18next";
import "./StreamTimeline.module.css";
import { usePrefersReducedMotion } from "../hooks/usePrefersReducedMotion";
import { createDateTimeFormat, formatNumber } from "../lib/formatters";
export interface StreamTimelineProps {
startDate: string;
cliffDate: string | null;
currentDate: string;
endDate: string;
withdrawableAmount: number;
totalAmount: number;
status: "active" | "paused" | "completed" | "upcoming";
isLoading?: boolean;
/**
* Set to `true` when rendered inside a compare pane.
* Applies `data-compare="true"` to the container so the
* half-width CSS rules in StreamTimeline.module.css activate,
* compacting bar height, legend, and cliff-label positioning.
*/
compareMode?: boolean;
/**
* When enabled, renders a transaction state demo at the bottom
* of the timeline. The demo simulates pending, confirmed,
* rejected, and timeout outcomes, and enforces duplicate
* submission prevention and retry behavior.
*/
showTransactionDemo?: boolean;
/**
* Controls the simulated outcome for the transaction demo.
* Only used when `showTransactionDemo` is `true`.
* Defaults to `"confirmed`.
*/
transactionDemoOutcome?: "confirmed" | "rejected" | "timeout";
}
type TransactionStatus = "idle" | "pending" | "confirmed" | "rejected" | "timeout";
const TransactionDemo: React.FC<{
mockOutcome: Exclude<TransactionStatus, "idle" | "pending">;
}> = ({ mockOutcome }) => {
const { t } = useTranslation();
const [status, setStatus] = React.useState<TransactionStatus>("idle");
const [message, setMessage] = React.useState(
"Transaction state idle. Click submit to start.",
);
const handleSubmit = () => {
if (status === "pending") return;
setStatus("pending");
setMessage("Transaction pending... Please wait for confirmation.");
};
React.useEffect(() => {
if (status !== "pending") return;
const timer = setTimeout(() => {
const successCount = mockOutcome === "confirmed" ? 1 : 0;
const failureCount =
mockOutcome === "rejected" || mockOutcome === "timeout" ? 1 : 0;
const skippedCount = 0;
setStatus(mockOutcome);
setMessage(
[
t("transactionDemo.successes", { count: successCount }),
t("transactionDemo.failures", { count: failureCount }),
t("transactionDemo.skipped", { count: skippedCount }),
].join(", "),
);
}, 1200);
return () => clearTimeout(timer);
}, [status, mockOutcome, t]);
const isPending = status === "pending";
const isFailed = status === "rejected" || status === "timeout";
const buttonLabel = isPending
? "Submitting..."
: isFailed
? "Retry"
: "Submit Transaction";
return (
<div className="transaction-demo" data-transaction-status={status}>
<h4>Transaction State Demo</h4>
<div className="transaction-demo__status" role="status" aria-live="polite">
{message}
</div>
<button
type="button"
onClick={handleSubmit}
disabled={isPending}
className="transaction-demo__submit"
>
{buttonLabel}
</button>
{status === "confirmed" && (
<button
type="button"
onClick={() => {
setStatus("idle");
setMessage("Transaction state idle. Click submit to start.");
}}
className="transaction-demo__reset"
>
Reset
</button>
)}
</div>
);
};
/**
* StreamTimeline Component
*
* Displays a horizontal timeline visualization of a stream's lifecycle:
* - Cliff period (hatched pattern)
* - Accrual phase (progress fill)
* - Remaining period (empty)
*
* Preconditions:
* - The end date must be strictly after the start date (`totalDuration > 0`).
*
* Accessible to screen readers via:
* - ARIA labels and descriptions
* - Text-based summary (hidden but announced)
* - Semantic HTML structure
*
* WCAG 2.1 AA compliant
*/
export const StreamTimeline: React.FC<StreamTimelineProps> = ({
startDate,
cliffDate,
currentDate,
endDate,
withdrawableAmount,
totalAmount,
status,
isLoading = false,
compareMode = false,
showTransactionDemo = false,
transactionDemoOutcome = "confirmed",
}) => {
const { t } = useTranslation();
const [animateClass, setAnimateClass] = React.useState("");
const prevStatusRef = React.useRef(status);
React.useEffect(() => {
if (prevStatusRef.current !== status) {
prevStatusRef.current = status;
setAnimateClass("");
const req = requestAnimationFrame(() => {
setAnimateClass("timeline-marker-animate");
});
return () => cancelAnimationFrame(req);
}
}, [status]);
// Parse dates
const start = new Date(startDate);
const cliff = cliffDate ? new Date(cliffDate) : null;
const current = new Date(currentDate);
const end = new Date(endDate);
const prefersReducedMotion = usePrefersReducedMotion();
// Validate dates
const totalDuration = end.getTime() - start.getTime();
if (
isNaN(start.getTime()) ||
isNaN(current.getTime()) ||
isNaN(end.getTime()) ||
totalDuration <= 0
) {
return (
<div
className="stream-timeline-container"
role="region"
aria-label="Stream timeline"
data-compare={compareMode ? "true" : undefined}
>
<div className="stream-timeline__error">Invalid date configuration</div>
</div>
);
}
// Calculate segments
const cliffEnd = cliff ? cliff.getTime() : start.getTime();
const currentTime = Math.min(current.getTime(), end.getTime());
// Cliff segment percentage (0-100)
const cliffPercent = cliff
? Math.max(
0,
Math.min(100, ((cliffEnd - start.getTime()) / totalDuration) * 100),
)
: 0;
// Accrual segment percentage (from start to current)
const accrualPercent = Math.max(
0,
Math.min(100, ((currentTime - start.getTime()) / totalDuration) * 100),
);
// Format date for display. Resolves user locale via navigator.language
// (with a validated "en-US" fallback) consistent with src/lib/formatters.ts.
// This mirrors the fix for issue #388 applied elsewhere in the app.
const formatDate = (date: Date): string => {
return createDateTimeFormat({
month: "short",
day: "numeric",
year: "2-digit",
}).format(date);
};
// Format short numeric month/day for the inline segment labels.
// Locale-aware so ordering and separators match the visitor's locale.
const formatShortDate = (date: Date): string => {
return createDateTimeFormat({
month: "numeric",
day: "numeric",
}).format(date);
};
return (
<div
className="stream-timeline-container"
role="region"
aria-label="Stream timeline visualization"
data-compare={compareMode ? "true" : undefined}
>
{/* Accessible text summary for screen readers */}
<div className="stream-timeline__sr-summary" role="doc-subtitle">
<h3 className="sr-only">Timeline Summary</h3>
<ul className="sr-only">
<li>{t("streamTimeline.startDate", { date: formatDate(start) })}</li>
{cliff && (
<li>{t("streamTimeline.cliffEndDate", { date: formatDate(cliff) })}</li>
)}
<li>{t("streamTimeline.currentDate", { date: formatDate(current) })}</li>
<li>{t("streamTimeline.endDate", { date: formatDate(end) })}</li>
<li>{t("streamTimeline.streamStatus", { status })}</li>
<li>{t("streamTimeline.progress", { count: Math.round(accrualPercent), percent: accrualPercent.toFixed(0) })}</li>
<li>{t("streamTimeline.withdrawable", { count: withdrawableAmount, amount: formatNumber(withdrawableAmount) })}</li>
<li>{t("streamTimeline.totalAmount", { count: totalAmount, amount: formatNumber(totalAmount) })}</li>
</ul>
</div>
{/* Visual timeline bar */}
<div
className="stream-timeline-bar"
role="progressbar"
aria-valuenow={Math.round(accrualPercent)}
aria-valuemin={0}
aria-valuemax={100}
aria-label="Stream accrual progress"
data-reduced-motion={prefersReducedMotion ? "true" : "false"}
>
<span aria-live="polite" className="sr-only">
{`Timeline status updated to ${status}`}
</span>
{/* Cliff segment (hatched) */}
{cliff && cliffPercent > 0 && (
<div
className={`stream-timeline-bar__segment stream-timeline-bar__segment--cliff is-${status}`}
style={{ width: `${cliffPercent}%` }}
role="img"
aria-label={`Cliff period: ${formatDate(start)} to ${formatDate(cliff)}`}
>
{cliffPercent > 5 && (
<span className="stream-timeline-bar__segment-label">
{formatShortDate(start)}
</span>
)}
</div>
)}
{/* Accrual segment (progress fill) - only show beyond cliff */}
{accrualPercent > cliffPercent && (
<div
className={`stream-timeline-bar__segment stream-timeline-bar__segment--accrual is-${status}`}
style={{ width: `${accrualPercent - cliffPercent}%` }}
role="img"
aria-label={`Accrual period: ${cliff ? formatDate(cliff) : formatDate(start)} to ${formatDate(current)}`}
>
{accrualPercent - cliffPercent > 8 && (
<span className="stream-timeline-bar__segment-label">
{(((accrualPercent - cliffPercent) / 100) * 100).toFixed(0)}%
</span>
)}
</div>
)}
{/* Remaining segment (empty) */}
{accrualPercent < 100 && (
<div
className={`stream-timeline-bar__segment stream-timeline-bar__segment--remaining is-${status}`}
style={{ width: `${100 - accrualPercent}%` }}
role="img"
aria-label={`Remaining period: ${formatDate(current)} to ${formatDate(end)}`}
/>
)}
{/* Current date marker */}
{current < end && current > start && (
<div
className={`stream-timeline-bar__marker is-${status} ${animateClass}`}
style={{ left: `${accrualPercent}%` }}
role="img"
aria-label={`Current date: ${formatDate(current)}`}
/>
)}
</div>
{/* Date labels */}
<div className="stream-timeline-labels">
<div className="stream-timeline-labels__item stream-timeline-labels__start">
<span className="stream-timeline-labels__date">
{formatDate(start)}
</span>
<span className="stream-timeline-labels__label">Start</span>
</div>
{cliff && (
<div
className="stream-timeline-labels__item stream-timeline-labels__cliff"
style={{ marginLeft: `${cliffPercent}%` }}
>
<span className="stream-timeline-labels__date">
{formatDate(cliff)}
</span>
<span className="stream-timeline-labels__label">Cliff end</span>
</div>
)}
<div className="stream-timeline-labels__item stream-timeline-labels__end">
<span className="stream-timeline-labels__date">
{formatDate(end)}
</span>
<span className="stream-timeline-labels__label">End</span>
</div>
</div>
{/* Legend */}
<div className="stream-timeline-legend">
<div className="stream-timeline-legend__item">
<div className="stream-timeline-legend__swatch stream-timeline-legend__swatch--cliff" />
<span>Cliff period (locked)</span>
</div>
<div className="stream-timeline-legend__item">
<div className="stream-timeline-legend__swatch stream-timeline-legend__swatch--accrual" />
<span>Accrual phase (unlocking)</span>
</div>
<div className="stream-timeline-legend__item">
<div className="stream-timeline-legend__swatch stream-timeline-legend__swatch--remaining" />
<span>Remaining (locked)</span>
</div>
</div>
{/* Loading state */}
{isLoading && (
<div
className="stream-timeline__loading"
aria-live="polite"
aria-label="Loading timeline"
>
<span className="stream-timeline__loading-spinner" />
<span>Loading timeline...</span>
</div>
)}
{/* Transaction state demo (optional) */}
{showTransactionDemo && (
<TransactionDemo mockOutcome={transactionDemoOutcome} />
)}
</div>
);
};
export default StreamTimeline;