@oxyhq/services 5.16.29 → 5.16.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/commonjs/index.js +64 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/ui/hooks/auth/index.js +37 -0
- package/lib/commonjs/ui/hooks/auth/index.js.map +1 -0
- package/lib/commonjs/ui/hooks/auth/useUsernameValidation.js +171 -0
- package/lib/commonjs/ui/hooks/auth/useUsernameValidation.js.map +1 -0
- package/lib/commonjs/ui/hooks/index.js +20 -0
- package/lib/commonjs/ui/hooks/index.js.map +1 -1
- package/lib/commonjs/ui/hooks/mutations/index.js +12 -0
- package/lib/commonjs/ui/hooks/mutations/index.js.map +1 -1
- package/lib/commonjs/ui/hooks/mutations/useAccountMutations.js +45 -1
- package/lib/commonjs/ui/hooks/mutations/useAccountMutations.js.map +1 -1
- package/lib/commonjs/ui/hooks/queries/index.js +12 -0
- package/lib/commonjs/ui/hooks/queries/index.js.map +1 -1
- package/lib/commonjs/ui/hooks/queries/queryKeys.js +3 -1
- package/lib/commonjs/ui/hooks/queries/queryKeys.js.map +1 -1
- package/lib/commonjs/ui/hooks/queries/useAccountQueries.js +43 -1
- package/lib/commonjs/ui/hooks/queries/useAccountQueries.js.map +1 -1
- package/lib/commonjs/ui/screens/PrivacySettingsScreen.js +76 -97
- package/lib/commonjs/ui/screens/PrivacySettingsScreen.js.map +1 -1
- package/lib/module/index.js +3 -3
- package/lib/module/index.js.map +1 -1
- package/lib/module/ui/hooks/auth/index.js +7 -0
- package/lib/module/ui/hooks/auth/index.js.map +1 -0
- package/lib/module/ui/hooks/auth/useUsernameValidation.js +167 -0
- package/lib/module/ui/hooks/auth/useUsernameValidation.js.map +1 -0
- package/lib/module/ui/hooks/index.js +1 -0
- package/lib/module/ui/hooks/index.js.map +1 -1
- package/lib/module/ui/hooks/mutations/index.js +1 -1
- package/lib/module/ui/hooks/mutations/index.js.map +1 -1
- package/lib/module/ui/hooks/mutations/useAccountMutations.js +42 -0
- package/lib/module/ui/hooks/mutations/useAccountMutations.js.map +1 -1
- package/lib/module/ui/hooks/queries/index.js +1 -1
- package/lib/module/ui/hooks/queries/index.js.map +1 -1
- package/lib/module/ui/hooks/queries/queryKeys.js +3 -1
- package/lib/module/ui/hooks/queries/queryKeys.js.map +1 -1
- package/lib/module/ui/hooks/queries/useAccountQueries.js +40 -0
- package/lib/module/ui/hooks/queries/useAccountQueries.js.map +1 -1
- package/lib/module/ui/screens/PrivacySettingsScreen.js +77 -98
- package/lib/module/ui/screens/PrivacySettingsScreen.js.map +1 -1
- package/lib/typescript/index.d.ts +4 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/ui/hooks/auth/index.d.ts +6 -0
- package/lib/typescript/ui/hooks/auth/index.d.ts.map +1 -0
- package/lib/typescript/ui/hooks/auth/useUsernameValidation.d.ts +32 -0
- package/lib/typescript/ui/hooks/auth/useUsernameValidation.d.ts.map +1 -0
- package/lib/typescript/ui/hooks/index.d.ts +1 -0
- package/lib/typescript/ui/hooks/index.d.ts.map +1 -1
- package/lib/typescript/ui/hooks/mutations/index.d.ts +1 -1
- package/lib/typescript/ui/hooks/mutations/index.d.ts.map +1 -1
- package/lib/typescript/ui/hooks/mutations/useAccountMutations.d.ts +12 -0
- package/lib/typescript/ui/hooks/mutations/useAccountMutations.d.ts.map +1 -1
- package/lib/typescript/ui/hooks/queries/index.d.ts +1 -1
- package/lib/typescript/ui/hooks/queries/index.d.ts.map +1 -1
- package/lib/typescript/ui/hooks/queries/queryKeys.d.ts +2 -0
- package/lib/typescript/ui/hooks/queries/queryKeys.d.ts.map +1 -1
- package/lib/typescript/ui/hooks/queries/useAccountQueries.d.ts +12 -0
- package/lib/typescript/ui/hooks/queries/useAccountQueries.d.ts.map +1 -1
- package/lib/typescript/ui/screens/PrivacySettingsScreen.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +6 -0
- package/src/ui/hooks/auth/index.ts +6 -0
- package/src/ui/hooks/auth/useUsernameValidation.ts +177 -0
- package/src/ui/hooks/index.ts +2 -1
- package/src/ui/hooks/mutations/index.ts +2 -0
- package/src/ui/hooks/mutations/useAccountMutations.ts +36 -0
- package/src/ui/hooks/queries/index.ts +2 -0
- package/src/ui/hooks/queries/queryKeys.ts +2 -0
- package/src/ui/hooks/queries/useAccountQueries.ts +34 -0
- package/src/ui/screens/PrivacySettingsScreen.tsx +67 -101
- package/lib/commonjs/ui/context/hooks/useSessionManagement.js +0 -281
- package/lib/commonjs/ui/context/hooks/useSessionManagement.js.map +0 -1
- package/lib/commonjs/ui/context/hooks/useStorage.js +0 -79
- package/lib/commonjs/ui/context/hooks/useStorage.js.map +0 -1
- package/lib/module/ui/context/hooks/useSessionManagement.js +0 -276
- package/lib/module/ui/context/hooks/useSessionManagement.js.map +0 -1
- package/lib/module/ui/context/hooks/useStorage.js +0 -74
- package/lib/module/ui/context/hooks/useStorage.js.map +0 -1
- package/lib/typescript/ui/context/hooks/useSessionManagement.d.ts +0 -41
- package/lib/typescript/ui/context/hooks/useSessionManagement.d.ts.map +0 -1
- package/lib/typescript/ui/context/hooks/useStorage.d.ts +0 -22
- package/lib/typescript/ui/context/hooks/useStorage.d.ts.map +0 -1
- package/src/ui/context/hooks/useSessionManagement.ts +0 -401
- package/src/ui/context/hooks/useStorage.ts +0 -104
|
@@ -1,401 +0,0 @@
|
|
|
1
|
-
import { useCallback, useMemo, useRef, useState } from 'react';
|
|
2
|
-
import type { ApiError, User } from '../../../models/interfaces';
|
|
3
|
-
import type { ClientSession } from '../../../models/session';
|
|
4
|
-
import { mergeSessions, normalizeAndSortSessions, sessionsArraysEqual } from '../../../utils/sessionUtils';
|
|
5
|
-
import { fetchSessionsWithFallback, mapSessionsToClient, validateSessionBatch } from '../../utils/sessionHelpers';
|
|
6
|
-
import { getStorageKeys, type StorageInterface } from '../../utils/storageHelpers';
|
|
7
|
-
import { handleAuthError, isInvalidSessionError } from '../../utils/errorHandlers';
|
|
8
|
-
import type { OxyServices } from '../../../core';
|
|
9
|
-
import type { QueryClient } from '@tanstack/react-query';
|
|
10
|
-
import { clearQueryCache } from '../../hooks/queryClient';
|
|
11
|
-
|
|
12
|
-
export interface UseSessionManagementOptions {
|
|
13
|
-
oxyServices: OxyServices;
|
|
14
|
-
storage: StorageInterface | null;
|
|
15
|
-
storageKeyPrefix?: string;
|
|
16
|
-
loginSuccess: (user: User) => void;
|
|
17
|
-
logoutStore: () => void;
|
|
18
|
-
applyLanguagePreference: (user: User) => Promise<void>;
|
|
19
|
-
onAuthStateChange?: (user: User | null) => void;
|
|
20
|
-
onError?: (error: ApiError) => void;
|
|
21
|
-
setAuthError?: (message: string | null) => void;
|
|
22
|
-
logger?: (message: string, error?: unknown) => void;
|
|
23
|
-
setTokenReady?: (ready: boolean) => void;
|
|
24
|
-
queryClient?: QueryClient | null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface UseSessionManagementResult {
|
|
28
|
-
sessions: ClientSession[];
|
|
29
|
-
activeSessionId: string | null;
|
|
30
|
-
setActiveSessionId: (sessionId: string | null) => void;
|
|
31
|
-
updateSessions: (incoming: ClientSession[], options?: { merge?: boolean }) => void;
|
|
32
|
-
switchSession: (sessionId: string) => Promise<User>;
|
|
33
|
-
refreshSessions: (activeUserId?: string) => Promise<void>;
|
|
34
|
-
clearSessionState: () => Promise<void>;
|
|
35
|
-
saveActiveSessionId: (sessionId: string) => Promise<void>;
|
|
36
|
-
trackRemovedSession: (sessionId: string) => void;
|
|
37
|
-
storageKeys: ReturnType<typeof getStorageKeys>;
|
|
38
|
-
isRefreshInFlight: boolean;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const DEFAULT_SAVE_ERROR_MESSAGE = 'Failed to save session data';
|
|
42
|
-
const CLEAR_STORAGE_ERROR = 'Failed to clear storage';
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Manage session state, persistence, and high-level multi-session operations.
|
|
46
|
-
*
|
|
47
|
-
* @param options - Session management configuration
|
|
48
|
-
*/
|
|
49
|
-
export const useSessionManagement = ({
|
|
50
|
-
oxyServices,
|
|
51
|
-
storage,
|
|
52
|
-
storageKeyPrefix,
|
|
53
|
-
loginSuccess,
|
|
54
|
-
logoutStore,
|
|
55
|
-
applyLanguagePreference,
|
|
56
|
-
onAuthStateChange,
|
|
57
|
-
onError,
|
|
58
|
-
setAuthError,
|
|
59
|
-
logger,
|
|
60
|
-
setTokenReady,
|
|
61
|
-
queryClient,
|
|
62
|
-
}: UseSessionManagementOptions): UseSessionManagementResult => {
|
|
63
|
-
const [sessions, setSessions] = useState<ClientSession[]>([]);
|
|
64
|
-
const [activeSessionId, setActiveSessionId] = useState<string | null>(null);
|
|
65
|
-
|
|
66
|
-
const refreshInFlightRef = useRef<Promise<void> | null>(null);
|
|
67
|
-
const removedSessionsRef = useRef<Set<string>>(new Set());
|
|
68
|
-
const lastRefreshRef = useRef<number>(0);
|
|
69
|
-
|
|
70
|
-
const storageKeys = useMemo(() => getStorageKeys(storageKeyPrefix), [storageKeyPrefix]);
|
|
71
|
-
|
|
72
|
-
const saveSessionIds = useCallback(
|
|
73
|
-
async (sessionIds: string[]): Promise<void> => {
|
|
74
|
-
if (!storage) return;
|
|
75
|
-
try {
|
|
76
|
-
const uniqueIds = Array.from(new Set(sessionIds));
|
|
77
|
-
await storage.setItem(storageKeys.sessionIds, JSON.stringify(uniqueIds));
|
|
78
|
-
} catch (error) {
|
|
79
|
-
if (logger) {
|
|
80
|
-
logger(DEFAULT_SAVE_ERROR_MESSAGE, error);
|
|
81
|
-
} else if (__DEV__) {
|
|
82
|
-
console.warn('Failed to save session IDs:', error);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
},
|
|
86
|
-
[logger, storage, storageKeys.sessionIds],
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
const updateSessions = useCallback(
|
|
90
|
-
(incoming: ClientSession[], options: { merge?: boolean } = {}): void => {
|
|
91
|
-
setSessions((prevSessions) => {
|
|
92
|
-
const processed = options.merge
|
|
93
|
-
? mergeSessions(prevSessions, incoming, activeSessionId, false)
|
|
94
|
-
: normalizeAndSortSessions(incoming, activeSessionId, false);
|
|
95
|
-
|
|
96
|
-
if (storage) {
|
|
97
|
-
void saveSessionIds(processed.map((session) => session.sessionId));
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (sessionsArraysEqual(prevSessions, processed)) {
|
|
101
|
-
return prevSessions;
|
|
102
|
-
}
|
|
103
|
-
return processed;
|
|
104
|
-
});
|
|
105
|
-
},
|
|
106
|
-
[activeSessionId, saveSessionIds, storage],
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
const saveActiveSessionId = useCallback(
|
|
110
|
-
async (sessionId: string): Promise<void> => {
|
|
111
|
-
if (!storage) return;
|
|
112
|
-
try {
|
|
113
|
-
await storage.setItem(storageKeys.activeSessionId, sessionId);
|
|
114
|
-
} catch (error) {
|
|
115
|
-
handleAuthError(error, {
|
|
116
|
-
defaultMessage: DEFAULT_SAVE_ERROR_MESSAGE,
|
|
117
|
-
code: 'SESSION_PERSISTENCE_ERROR',
|
|
118
|
-
onError,
|
|
119
|
-
setAuthError,
|
|
120
|
-
logger,
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
},
|
|
124
|
-
[logger, onError, setAuthError, storage, storageKeys.activeSessionId],
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
const removeActiveSessionId = useCallback(async (): Promise<void> => {
|
|
128
|
-
if (!storage) return;
|
|
129
|
-
try {
|
|
130
|
-
await storage.removeItem(storageKeys.activeSessionId);
|
|
131
|
-
} catch (error) {
|
|
132
|
-
handleAuthError(error, {
|
|
133
|
-
defaultMessage: DEFAULT_SAVE_ERROR_MESSAGE,
|
|
134
|
-
code: 'SESSION_PERSISTENCE_ERROR',
|
|
135
|
-
onError,
|
|
136
|
-
setAuthError,
|
|
137
|
-
logger,
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
}, [logger, onError, setAuthError, storage, storageKeys.activeSessionId]);
|
|
141
|
-
|
|
142
|
-
const clearSessionStorage = useCallback(async (): Promise<void> => {
|
|
143
|
-
if (!storage) return;
|
|
144
|
-
try {
|
|
145
|
-
await storage.removeItem(storageKeys.activeSessionId);
|
|
146
|
-
await storage.removeItem(storageKeys.sessionIds);
|
|
147
|
-
// Clear identity sync state
|
|
148
|
-
await storage.removeItem('oxy_identity_synced').catch(() => {});
|
|
149
|
-
} catch (error) {
|
|
150
|
-
handleAuthError(error, {
|
|
151
|
-
defaultMessage: CLEAR_STORAGE_ERROR,
|
|
152
|
-
code: 'STORAGE_ERROR',
|
|
153
|
-
onError,
|
|
154
|
-
setAuthError,
|
|
155
|
-
logger,
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
}, [logger, onError, setAuthError, storage, storageKeys.activeSessionId, storageKeys.sessionIds]);
|
|
159
|
-
|
|
160
|
-
const clearSessionState = useCallback(async (): Promise<void> => {
|
|
161
|
-
setSessions([]);
|
|
162
|
-
setActiveSessionId(null);
|
|
163
|
-
logoutStore();
|
|
164
|
-
|
|
165
|
-
// Clear TanStack Query cache (in-memory)
|
|
166
|
-
if (queryClient) {
|
|
167
|
-
queryClient.clear();
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// Clear persisted query cache
|
|
171
|
-
if (storage) {
|
|
172
|
-
try {
|
|
173
|
-
await clearQueryCache(storage);
|
|
174
|
-
} catch (error) {
|
|
175
|
-
if (logger) {
|
|
176
|
-
logger('Failed to clear persisted query cache', error);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
await clearSessionStorage();
|
|
182
|
-
onAuthStateChange?.(null);
|
|
183
|
-
}, [clearSessionStorage, logoutStore, onAuthStateChange, queryClient, storage, logger]);
|
|
184
|
-
|
|
185
|
-
const activateSession = useCallback(
|
|
186
|
-
async (sessionId: string, user: User): Promise<void> => {
|
|
187
|
-
await oxyServices.getTokenBySession(sessionId);
|
|
188
|
-
setTokenReady?.(true);
|
|
189
|
-
setActiveSessionId(sessionId);
|
|
190
|
-
loginSuccess(user);
|
|
191
|
-
await saveActiveSessionId(sessionId);
|
|
192
|
-
await applyLanguagePreference(user);
|
|
193
|
-
onAuthStateChange?.(user);
|
|
194
|
-
},
|
|
195
|
-
[
|
|
196
|
-
applyLanguagePreference,
|
|
197
|
-
loginSuccess,
|
|
198
|
-
onAuthStateChange,
|
|
199
|
-
oxyServices,
|
|
200
|
-
saveActiveSessionId,
|
|
201
|
-
setTokenReady,
|
|
202
|
-
],
|
|
203
|
-
);
|
|
204
|
-
|
|
205
|
-
const trackRemovedSession = useCallback((sessionId: string) => {
|
|
206
|
-
removedSessionsRef.current.add(sessionId);
|
|
207
|
-
setTimeout(() => {
|
|
208
|
-
removedSessionsRef.current.delete(sessionId);
|
|
209
|
-
}, 5000);
|
|
210
|
-
}, []);
|
|
211
|
-
|
|
212
|
-
const findReplacementSession = useCallback(
|
|
213
|
-
async (sessionIds: string[]): Promise<User | null> => {
|
|
214
|
-
if (!sessionIds.length) {
|
|
215
|
-
return null;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
const validationResults = await validateSessionBatch(oxyServices, sessionIds, {
|
|
219
|
-
maxConcurrency: 3,
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
const validSession = validationResults.find((result) => result.valid);
|
|
223
|
-
if (!validSession) {
|
|
224
|
-
return null;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const validation = await oxyServices.validateSession(validSession.sessionId, {
|
|
228
|
-
useHeaderValidation: true,
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
if (!validation?.valid || !validation.user) {
|
|
232
|
-
return null;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const user = validation.user as User;
|
|
236
|
-
await activateSession(validSession.sessionId, user);
|
|
237
|
-
return user;
|
|
238
|
-
},
|
|
239
|
-
[activateSession, oxyServices],
|
|
240
|
-
);
|
|
241
|
-
|
|
242
|
-
const switchSession = useCallback(
|
|
243
|
-
async (sessionId: string): Promise<User> => {
|
|
244
|
-
try {
|
|
245
|
-
const validation = await oxyServices.validateSession(sessionId, { useHeaderValidation: true });
|
|
246
|
-
if (!validation?.valid) {
|
|
247
|
-
throw new Error('Session is invalid or expired');
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
if (!validation.user) {
|
|
251
|
-
throw new Error('User data not available from session validation');
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
const user = validation.user as User;
|
|
255
|
-
await activateSession(sessionId, user);
|
|
256
|
-
|
|
257
|
-
try {
|
|
258
|
-
const deviceSessions = await fetchSessionsWithFallback(oxyServices, sessionId, {
|
|
259
|
-
fallbackUserId: user.id,
|
|
260
|
-
logger,
|
|
261
|
-
});
|
|
262
|
-
updateSessions(deviceSessions, { merge: true });
|
|
263
|
-
} catch (error) {
|
|
264
|
-
if (__DEV__) {
|
|
265
|
-
console.warn('Failed to synchronize sessions after switch:', error);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
return user;
|
|
270
|
-
} catch (error) {
|
|
271
|
-
const invalidSession = isInvalidSessionError(error);
|
|
272
|
-
|
|
273
|
-
if (invalidSession) {
|
|
274
|
-
updateSessions(sessions.filter((session) => session.sessionId !== sessionId), {
|
|
275
|
-
merge: false,
|
|
276
|
-
});
|
|
277
|
-
if (sessionId === activeSessionId) {
|
|
278
|
-
const otherSessionIds = sessions
|
|
279
|
-
.filter(
|
|
280
|
-
(session) =>
|
|
281
|
-
session.sessionId !== sessionId && !removedSessionsRef.current.has(session.sessionId),
|
|
282
|
-
)
|
|
283
|
-
.map((session) => session.sessionId);
|
|
284
|
-
|
|
285
|
-
const replacementUser = await findReplacementSession(otherSessionIds);
|
|
286
|
-
if (replacementUser) {
|
|
287
|
-
return replacementUser;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
handleAuthError(error, {
|
|
293
|
-
defaultMessage: 'Failed to switch session',
|
|
294
|
-
code: invalidSession ? 'INVALID_SESSION' : 'SESSION_SWITCH_ERROR',
|
|
295
|
-
onError,
|
|
296
|
-
setAuthError,
|
|
297
|
-
logger,
|
|
298
|
-
});
|
|
299
|
-
throw error instanceof Error ? error : new Error('Failed to switch session');
|
|
300
|
-
}
|
|
301
|
-
},
|
|
302
|
-
[
|
|
303
|
-
activateSession,
|
|
304
|
-
activeSessionId,
|
|
305
|
-
findReplacementSession,
|
|
306
|
-
logger,
|
|
307
|
-
loginSuccess,
|
|
308
|
-
onError,
|
|
309
|
-
oxyServices,
|
|
310
|
-
sessions,
|
|
311
|
-
setAuthError,
|
|
312
|
-
updateSessions,
|
|
313
|
-
],
|
|
314
|
-
);
|
|
315
|
-
|
|
316
|
-
const refreshSessions = useCallback(
|
|
317
|
-
async (activeUserId?: string): Promise<void> => {
|
|
318
|
-
if (!activeSessionId) return;
|
|
319
|
-
|
|
320
|
-
if (refreshInFlightRef.current) {
|
|
321
|
-
await refreshInFlightRef.current;
|
|
322
|
-
return;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
const now = Date.now();
|
|
326
|
-
if (now - lastRefreshRef.current < 500) {
|
|
327
|
-
return;
|
|
328
|
-
}
|
|
329
|
-
lastRefreshRef.current = now;
|
|
330
|
-
|
|
331
|
-
const refreshPromise = (async () => {
|
|
332
|
-
try {
|
|
333
|
-
const deviceSessions = await fetchSessionsWithFallback(oxyServices, activeSessionId, {
|
|
334
|
-
fallbackUserId: activeUserId,
|
|
335
|
-
logger,
|
|
336
|
-
});
|
|
337
|
-
updateSessions(deviceSessions, { merge: true });
|
|
338
|
-
} catch (error) {
|
|
339
|
-
if (isInvalidSessionError(error)) {
|
|
340
|
-
const otherSessions = sessions
|
|
341
|
-
.filter(
|
|
342
|
-
(session) =>
|
|
343
|
-
session.sessionId !== activeSessionId &&
|
|
344
|
-
!removedSessionsRef.current.has(session.sessionId),
|
|
345
|
-
)
|
|
346
|
-
.map((session) => session.sessionId);
|
|
347
|
-
|
|
348
|
-
const replacementUser = await findReplacementSession(otherSessions);
|
|
349
|
-
if (!replacementUser) {
|
|
350
|
-
await clearSessionState();
|
|
351
|
-
}
|
|
352
|
-
return;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
handleAuthError(error, {
|
|
356
|
-
defaultMessage: 'Failed to refresh sessions',
|
|
357
|
-
code: 'SESSION_REFRESH_ERROR',
|
|
358
|
-
onError,
|
|
359
|
-
setAuthError,
|
|
360
|
-
logger,
|
|
361
|
-
});
|
|
362
|
-
} finally {
|
|
363
|
-
refreshInFlightRef.current = null;
|
|
364
|
-
lastRefreshRef.current = Date.now();
|
|
365
|
-
}
|
|
366
|
-
})();
|
|
367
|
-
|
|
368
|
-
refreshInFlightRef.current = refreshPromise;
|
|
369
|
-
await refreshPromise;
|
|
370
|
-
},
|
|
371
|
-
[
|
|
372
|
-
activeSessionId,
|
|
373
|
-
clearSessionState,
|
|
374
|
-
findReplacementSession,
|
|
375
|
-
logger,
|
|
376
|
-
onError,
|
|
377
|
-
oxyServices,
|
|
378
|
-
sessions,
|
|
379
|
-
setAuthError,
|
|
380
|
-
updateSessions,
|
|
381
|
-
],
|
|
382
|
-
);
|
|
383
|
-
|
|
384
|
-
const isRefreshInFlight = Boolean(refreshInFlightRef.current);
|
|
385
|
-
|
|
386
|
-
return {
|
|
387
|
-
sessions,
|
|
388
|
-
activeSessionId,
|
|
389
|
-
setActiveSessionId,
|
|
390
|
-
updateSessions,
|
|
391
|
-
switchSession,
|
|
392
|
-
refreshSessions,
|
|
393
|
-
clearSessionState,
|
|
394
|
-
saveActiveSessionId,
|
|
395
|
-
trackRemovedSession,
|
|
396
|
-
storageKeys,
|
|
397
|
-
isRefreshInFlight,
|
|
398
|
-
};
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
-
import type { ApiError } from '../../../models/interfaces';
|
|
3
|
-
import { createPlatformStorage, type StorageInterface } from '../../utils/storageHelpers';
|
|
4
|
-
import { extractErrorMessage } from '../../utils/errorHandlers';
|
|
5
|
-
|
|
6
|
-
export interface UseStorageOptions {
|
|
7
|
-
onError?: (error: ApiError) => void;
|
|
8
|
-
logger?: (message: string, error?: unknown) => void;
|
|
9
|
-
errorCode?: string;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface UseStorageResult {
|
|
13
|
-
storage: StorageInterface | null;
|
|
14
|
-
isReady: boolean;
|
|
15
|
-
error: string | null;
|
|
16
|
-
refresh: () => Promise<StorageInterface | null>;
|
|
17
|
-
withStorage: <T>(callback: (storage: StorageInterface) => Promise<T>) => Promise<T | null>;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const DEFAULT_ERROR_CODE = 'STORAGE_INIT_ERROR';
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* React hook that exposes a platform-agnostic storage reference.
|
|
24
|
-
* Handles initialization, error propagation, and lazy re-initialization.
|
|
25
|
-
*
|
|
26
|
-
* @param options - Optional configuration for error reporting and logging
|
|
27
|
-
*/
|
|
28
|
-
export const useStorage = ({
|
|
29
|
-
onError,
|
|
30
|
-
logger,
|
|
31
|
-
errorCode = DEFAULT_ERROR_CODE,
|
|
32
|
-
}: UseStorageOptions = {}): UseStorageResult => {
|
|
33
|
-
const [storage, setStorage] = useState<StorageInterface | null>(null);
|
|
34
|
-
const [error, setError] = useState<string | null>(null);
|
|
35
|
-
const initializingRef = useRef<Promise<StorageInterface | null> | null>(null);
|
|
36
|
-
|
|
37
|
-
const notifyError = useCallback(
|
|
38
|
-
(err: unknown) => {
|
|
39
|
-
const message = extractErrorMessage(err, 'Failed to initialize storage');
|
|
40
|
-
setError(message);
|
|
41
|
-
|
|
42
|
-
if (logger) {
|
|
43
|
-
logger(message, err);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
onError?.({
|
|
47
|
-
message,
|
|
48
|
-
code: errorCode,
|
|
49
|
-
status: 500,
|
|
50
|
-
});
|
|
51
|
-
},
|
|
52
|
-
[errorCode, logger, onError],
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
const createStorageInstance = useCallback(async (): Promise<StorageInterface | null> => {
|
|
56
|
-
try {
|
|
57
|
-
const platformStorage = await createPlatformStorage();
|
|
58
|
-
setStorage(platformStorage);
|
|
59
|
-
setError(null);
|
|
60
|
-
return platformStorage;
|
|
61
|
-
} catch (err) {
|
|
62
|
-
notifyError(err);
|
|
63
|
-
setStorage(null);
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
}, [notifyError]);
|
|
67
|
-
|
|
68
|
-
const refresh = useCallback(async (): Promise<StorageInterface | null> => {
|
|
69
|
-
if (!initializingRef.current) {
|
|
70
|
-
initializingRef.current = createStorageInstance().finally(() => {
|
|
71
|
-
initializingRef.current = null;
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return initializingRef.current;
|
|
76
|
-
}, [createStorageInstance]);
|
|
77
|
-
|
|
78
|
-
useEffect(() => {
|
|
79
|
-
refresh().catch((err) => {
|
|
80
|
-
notifyError(err);
|
|
81
|
-
});
|
|
82
|
-
}, [refresh, notifyError]);
|
|
83
|
-
|
|
84
|
-
const withStorage = useCallback(
|
|
85
|
-
async <T,>(callback: (resolvedStorage: StorageInterface) => Promise<T>): Promise<T | null> => {
|
|
86
|
-
const resolvedStorage = storage ?? (await refresh());
|
|
87
|
-
if (!resolvedStorage) {
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
return callback(resolvedStorage);
|
|
91
|
-
},
|
|
92
|
-
[refresh, storage],
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
return {
|
|
96
|
-
storage,
|
|
97
|
-
isReady: Boolean(storage) && !error,
|
|
98
|
-
error,
|
|
99
|
-
refresh,
|
|
100
|
-
withStorage,
|
|
101
|
-
};
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
|