forked from mux-labs/mux-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseRecoveryTimeline.ts
More file actions
205 lines (181 loc) · 4.82 KB
/
Copy pathuseRecoveryTimeline.ts
File metadata and controls
205 lines (181 loc) · 4.82 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
import { useCallback, useMemo, useState } from "react";
import type {
RecoveryEventStatus,
RecoveryTimeline,
RecoveryTimelineEvent,
} from "@/types/recovery";
/**
* Hook for managing recovery timeline state and operations
*
* Features:
* - Manages recovery timeline data
* - Filters events by status
* - Calculates timeline statistics
* - Handles event selection
* - Validates timeline data
*
* @param initialTimeline - Initial recovery timeline data
* @returns Object containing timeline state and utility functions
*
* @example
* const { timeline, selectedEvent, selectEvent, getEventsByStatus } = useRecoveryTimeline(mockTimeline);
*/
export function useRecoveryTimeline(initialTimeline: RecoveryTimeline) {
const [timeline, setTimeline] = useState<RecoveryTimeline>(initialTimeline);
const [selectedEventId, setSelectedEventId] = useState<string | null>(null);
/**
* Validates timeline data and handles invalid states gracefully
*/
const validateTimeline = useCallback((tl: RecoveryTimeline): boolean => {
if (!tl || !tl.id || !tl.walletId) {
console.warn("Invalid timeline: missing required fields");
return false;
}
if (!Array.isArray(tl.events) || tl.events.length === 0) {
console.warn("Invalid timeline: no events");
return false;
}
return true;
}, []);
/**
* Updates the timeline with new data
*/
const updateTimeline = useCallback(
(newTimeline: RecoveryTimeline) => {
if (validateTimeline(newTimeline)) {
setTimeline(newTimeline);
}
},
[validateTimeline],
);
/**
* Selects an event from the timeline
*/
const selectEvent = useCallback((eventId: string | null) => {
setSelectedEventId(eventId);
}, []);
/**
* Gets the currently selected event
*/
const selectedEvent = useMemo(() => {
if (!selectedEventId) return null;
return timeline.events.find((e) => e.id === selectedEventId) || null;
}, [selectedEventId, timeline.events]);
/**
* Filters events by status
*/
const getEventsByStatus = useCallback(
(status: RecoveryEventStatus) => {
return timeline.events.filter((event) => event.status === status);
},
[timeline.events],
);
/**
* Gets all completed events
*/
const completedEvents = useMemo(
() => getEventsByStatus("completed"),
[getEventsByStatus],
);
/**
* Gets all in-progress events
*/
const inProgressEvents = useMemo(
() => getEventsByStatus("in_progress"),
[getEventsByStatus],
);
/**
* Gets all failed events
*/
const failedEvents = useMemo(
() => getEventsByStatus("failed"),
[getEventsByStatus],
);
/**
* Gets all pending events
*/
const pendingEvents = useMemo(
() => getEventsByStatus("pending"),
[getEventsByStatus],
);
/**
* Calculates timeline progress percentage
*/
const progressPercentage = useMemo(() => {
const totalEvents = timeline.events.length;
if (totalEvents === 0) return 0;
const completedCount = completedEvents.length;
return Math.round((completedCount / totalEvents) * 100);
}, [timeline.events.length, completedEvents.length]);
/**
* Checks if timeline is complete
*/
const isComplete = useMemo(() => {
return timeline.status === "completed" && failedEvents.length === 0;
}, [timeline.status, failedEvents.length]);
/**
* Checks if timeline has errors
*/
const hasErrors = useMemo(() => {
return failedEvents.length > 0 || timeline.status === "failed";
}, [failedEvents.length, timeline.status]);
/**
* Gets the first incomplete event
*/
const currentEvent = useMemo(() => {
return inProgressEvents[0] || pendingEvents[0] || failedEvents[0] || null;
}, [inProgressEvents, pendingEvents, failedEvents]);
/**
* Calculates duration between two events
*/
const getEventDuration = useCallback(
(fromIndex: number, toIndex: number): number | null => {
if (
fromIndex < 0 ||
toIndex < 0 ||
fromIndex >= timeline.events.length ||
toIndex >= timeline.events.length
) {
return null;
}
const fromEvent = timeline.events[fromIndex];
const toEvent = timeline.events[toIndex];
if (!fromEvent || !toEvent) return null;
return toEvent.timestamp.getTime() - fromEvent.timestamp.getTime();
},
[timeline.events],
);
/**
* Formats duration in human-readable format
*/
const formatDuration = useCallback((milliseconds: number): string => {
const seconds = Math.floor(milliseconds / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
if (hours > 0) {
return `${hours}h ${minutes % 60}m`;
}
if (minutes > 0) {
return `${minutes}m ${seconds % 60}s`;
}
return `${seconds}s`;
}, []);
return {
timeline,
updateTimeline,
selectedEvent,
selectEvent,
getEventsByStatus,
completedEvents,
inProgressEvents,
failedEvents,
pendingEvents,
progressPercentage,
isComplete,
hasErrors,
currentEvent,
getEventDuration,
formatDuration,
validateTimeline,
};
}