@opengeni/react 2.5.0-canary.1 → 3.0.0-canary.0
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/README.md +15 -0
- package/dist/accounts.d.ts +64 -0
- package/dist/accounts.js +746 -0
- package/dist/accounts.js.map +1 -0
- package/dist/{browser-viewer-GXUICIAG.js → browser-viewer-AEFHUYZK.js} +3 -3
- package/dist/{chunk-MXJGO7LE.js → chunk-D3UIAJKY.js} +52 -2
- package/dist/chunk-D3UIAJKY.js.map +1 -0
- package/dist/{chunk-QGBJ6GHG.js → chunk-ELIYWBZR.js} +4 -4
- package/dist/{chunk-6H6S5V4Z.js → chunk-HQVCKCLG.js} +758 -871
- package/dist/chunk-HQVCKCLG.js.map +1 -0
- package/dist/{chunk-T7VAYG2I.js → chunk-MZ2G2XBA.js} +54 -2
- package/dist/chunk-MZ2G2XBA.js.map +1 -0
- package/dist/{chunk-X4JAFD5R.js → chunk-OAM4WAAM.js} +4 -4
- package/dist/{chunk-KSDXER4X.js → chunk-RGALCTWO.js} +64 -15
- package/dist/chunk-RGALCTWO.js.map +1 -0
- package/dist/chunk-YFKJ7VZH.js +268 -0
- package/dist/chunk-YFKJ7VZH.js.map +1 -0
- package/dist/{chunk-ATQJEWCA.js → chunk-YJFGAFBD.js} +124 -50
- package/dist/chunk-YJFGAFBD.js.map +1 -0
- package/dist/components/composer.d.ts +8 -0
- package/dist/components/file-browser.d.ts +4 -1
- package/dist/components/sandbox-files.d.ts +4 -1
- package/dist/components/sandbox-workspace.d.ts +5 -0
- package/dist/composer.js +3 -3
- package/dist/{computer-viewer-ZOP4Q7A7.js → computer-viewer-FWEZISW4.js} +3 -3
- package/dist/embedded-session-client.d.ts +23 -0
- package/dist/file-node-visibility.d.ts +15 -0
- package/dist/hooks/use-file-attachments.d.ts +9 -2
- package/dist/human-input.d.ts +3 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +139 -53
- package/dist/index.js.map +1 -1
- package/dist/interaction.js +3 -3
- package/dist/session-ui.js +4 -4
- package/dist/session.d.ts +2 -0
- package/dist/session.js +7 -5
- package/dist/timeline/index.d.ts +1 -0
- package/dist/timeline/screenshot-lightbox.d.ts +16 -1
- package/package.json +6 -2
- package/src/accounts.tsx +970 -0
- package/src/components/chat-composer.tsx +88 -84
- package/src/components/composer.tsx +61 -9
- package/src/components/file-browser.tsx +33 -20
- package/src/components/human-input-surface.tsx +10 -6
- package/src/components/message-timeline.tsx +37 -9
- package/src/components/sandbox-files.tsx +42 -2
- package/src/components/sandbox-workspace.tsx +10 -0
- package/src/embedded-session-client.ts +104 -0
- package/src/file-node-visibility.ts +66 -0
- package/src/hooks/use-composer.ts +64 -2
- package/src/hooks/use-file-attachments.ts +38 -6
- package/src/hooks/use-human-input.ts +31 -4
- package/src/human-input.ts +12 -1
- package/src/index.ts +12 -0
- package/src/session.ts +5 -0
- package/src/timeline/index.ts +1 -0
- package/src/timeline/screenshot-lightbox.tsx +93 -29
- package/styles/compiled.css +11 -1
- package/styles/tokens.css +3 -1
- package/dist/chunk-6H6S5V4Z.js.map +0 -1
- package/dist/chunk-ATQJEWCA.js.map +0 -1
- package/dist/chunk-FK6VG4LL.js +0 -90
- package/dist/chunk-FK6VG4LL.js.map +0 -1
- package/dist/chunk-KSDXER4X.js.map +0 -1
- package/dist/chunk-MXJGO7LE.js.map +0 -1
- package/dist/chunk-T7VAYG2I.js.map +0 -1
- /package/dist/{browser-viewer-GXUICIAG.js.map → browser-viewer-AEFHUYZK.js.map} +0 -0
- /package/dist/{chunk-QGBJ6GHG.js.map → chunk-ELIYWBZR.js.map} +0 -0
- /package/dist/{chunk-X4JAFD5R.js.map → chunk-OAM4WAAM.js.map} +0 -0
- /package/dist/{computer-viewer-ZOP4Q7A7.js.map → computer-viewer-FWEZISW4.js.map} +0 -0
package/src/accounts.tsx
ADDED
|
@@ -0,0 +1,970 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BrowserAccountsClientLike,
|
|
3
|
+
ManagedAuthDeepLinkResolution,
|
|
4
|
+
ManagedAuthLoginTransaction,
|
|
5
|
+
ManagedAuthSessionSetProjection,
|
|
6
|
+
} from "@opengeni/sdk/accounts";
|
|
7
|
+
import {
|
|
8
|
+
createContext,
|
|
9
|
+
useCallback,
|
|
10
|
+
useContext,
|
|
11
|
+
useEffect,
|
|
12
|
+
useMemo,
|
|
13
|
+
useRef,
|
|
14
|
+
useState,
|
|
15
|
+
type ReactNode,
|
|
16
|
+
} from "react";
|
|
17
|
+
|
|
18
|
+
export type BrowserAccountTransitionPhase =
|
|
19
|
+
| "preflight"
|
|
20
|
+
| "blocked"
|
|
21
|
+
| "committing"
|
|
22
|
+
| "loading"
|
|
23
|
+
| "ready"
|
|
24
|
+
| "recoverable_error";
|
|
25
|
+
|
|
26
|
+
export type BrowserAccountTransitionKind =
|
|
27
|
+
| "add"
|
|
28
|
+
| "select"
|
|
29
|
+
| "logout_one"
|
|
30
|
+
| "logout_all"
|
|
31
|
+
| "reauth"
|
|
32
|
+
| "cross_tab";
|
|
33
|
+
|
|
34
|
+
export type BrowserAccountTransitionBlocker = {
|
|
35
|
+
id: string;
|
|
36
|
+
label: string;
|
|
37
|
+
detail?: string | undefined;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type BrowserAccountTransition = {
|
|
41
|
+
kind: BrowserAccountTransitionKind;
|
|
42
|
+
from: ManagedAuthSessionSetProjection | null;
|
|
43
|
+
to: ManagedAuthSessionSetProjection | null;
|
|
44
|
+
signal: AbortSignal;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type BrowserAccountsProviderProps = {
|
|
48
|
+
client: BrowserAccountsClientLike;
|
|
49
|
+
/**
|
|
50
|
+
* Hide the authenticated tree, abort old actor work, clear tenant-bound
|
|
51
|
+
* state, recreate credentials/clients, and refetch principal access here.
|
|
52
|
+
* Cross-tab/server invalidation first calls this with `to: null` as a
|
|
53
|
+
* neutral fence before authority is reread, then calls it with the accepted
|
|
54
|
+
* projection after the new actor is known.
|
|
55
|
+
*/
|
|
56
|
+
onActorTransition: (transition: BrowserAccountTransition) => Promise<void>;
|
|
57
|
+
children?: ReactNode;
|
|
58
|
+
broadcastChannelName?: string | null | undefined;
|
|
59
|
+
/** Adopt the already-authenticated legacy provider session during dual-mode rollout. */
|
|
60
|
+
bootstrapLegacySession?: boolean | undefined;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
type BlockerInspector = () => BrowserAccountTransitionBlocker | null;
|
|
64
|
+
|
|
65
|
+
type PendingNonSecretTransition = {
|
|
66
|
+
kind: Exclude<BrowserAccountTransitionKind, "add" | "reauth" | "cross_tab">;
|
|
67
|
+
operationId: string;
|
|
68
|
+
expectedGeneration: string | null;
|
|
69
|
+
changesActor: (projection: ManagedAuthSessionSetProjection) => boolean;
|
|
70
|
+
execute: (expectedGeneration: string) => Promise<ManagedAuthSessionSetProjection>;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export type BrowserAccountsContextValue = {
|
|
74
|
+
projection: ManagedAuthSessionSetProjection | null;
|
|
75
|
+
phase: BrowserAccountTransitionPhase;
|
|
76
|
+
blockers: BrowserAccountTransitionBlocker[];
|
|
77
|
+
error: Error | null;
|
|
78
|
+
transaction: ManagedAuthLoginTransaction | null;
|
|
79
|
+
hasPendingTransition: boolean;
|
|
80
|
+
refresh: () => Promise<ManagedAuthSessionSetProjection | null>;
|
|
81
|
+
invalidateActor: () => Promise<ManagedAuthSessionSetProjection | null>;
|
|
82
|
+
registerTransitionBlocker: (id: string, inspect: BlockerInspector) => () => void;
|
|
83
|
+
continueTransition: () => Promise<boolean>;
|
|
84
|
+
cancelPendingTransition: () => void;
|
|
85
|
+
beginAdd: (returnIntent?: string | undefined) => Promise<ManagedAuthLoginTransaction>;
|
|
86
|
+
beginReauth: (
|
|
87
|
+
slotId: string,
|
|
88
|
+
returnIntent?: string | undefined,
|
|
89
|
+
) => Promise<ManagedAuthLoginTransaction>;
|
|
90
|
+
completeEmailPassword: (input: { email: string; password: string }) => Promise<boolean>;
|
|
91
|
+
settleExternalLoginTransaction: (transactionId: string) => Promise<boolean>;
|
|
92
|
+
cancelLoginTransaction: () => Promise<void>;
|
|
93
|
+
selectSlot: (slotId: string) => Promise<boolean>;
|
|
94
|
+
logoutSlot: (slotId: string, replacementSlotId: string | null) => Promise<boolean>;
|
|
95
|
+
logoutAll: () => Promise<boolean>;
|
|
96
|
+
resolveDeepLink: (path: string) => Promise<ManagedAuthDeepLinkResolution>;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const BrowserAccountsContext = createContext<BrowserAccountsContextValue | null>(null);
|
|
100
|
+
const DEFAULT_CHANNEL = "opengeni:browser-account-epoch:v1";
|
|
101
|
+
const CROSS_TAB_ACTOR_HOLD_MS = 30_000;
|
|
102
|
+
|
|
103
|
+
async function yieldToCrossTabActorHold(): Promise<void> {
|
|
104
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 0));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function operationId(): string {
|
|
108
|
+
return crypto.randomUUID();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function sameActor(
|
|
112
|
+
left: ManagedAuthSessionSetProjection | null,
|
|
113
|
+
right: ManagedAuthSessionSetProjection | null,
|
|
114
|
+
): boolean {
|
|
115
|
+
return (
|
|
116
|
+
left?.generation === right?.generation &&
|
|
117
|
+
left?.actorEpoch === right?.actorEpoch &&
|
|
118
|
+
left?.selectedSlotId === right?.selectedSlotId &&
|
|
119
|
+
left?.state === right?.state
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function sameSelectedActor(
|
|
124
|
+
left: ManagedAuthSessionSetProjection | null,
|
|
125
|
+
right: ManagedAuthSessionSetProjection | null,
|
|
126
|
+
): boolean {
|
|
127
|
+
return (
|
|
128
|
+
left !== null &&
|
|
129
|
+
right !== null &&
|
|
130
|
+
left.actorEpoch === right.actorEpoch &&
|
|
131
|
+
left.selectedSlotId === right.selectedSlotId &&
|
|
132
|
+
left.state === right.state
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function compareCounter(left: string, right: string): number {
|
|
137
|
+
const leftValue = BigInt(left);
|
|
138
|
+
const rightValue = BigInt(right);
|
|
139
|
+
return leftValue < rightValue ? -1 : leftValue > rightValue ? 1 : 0;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function isOlderProjection(
|
|
143
|
+
candidate: ManagedAuthSessionSetProjection,
|
|
144
|
+
current: ManagedAuthSessionSetProjection,
|
|
145
|
+
): boolean {
|
|
146
|
+
const epoch = compareCounter(candidate.actorEpoch, current.actorEpoch);
|
|
147
|
+
return epoch < 0 || (epoch === 0 && compareCounter(candidate.generation, current.generation) < 0);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function accountError(error: unknown): Error {
|
|
151
|
+
return error instanceof Error ? error : new Error("Browser account operation failed");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function supersededOperationError(): Error {
|
|
155
|
+
const error = new Error("Browser account operation was superseded by an actor change");
|
|
156
|
+
error.name = "AbortError";
|
|
157
|
+
return error;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function errorCode(error: unknown): string | null {
|
|
161
|
+
if (!error || typeof error !== "object") return null;
|
|
162
|
+
const code = (error as { code?: unknown }).code;
|
|
163
|
+
return typeof code === "string" ? code : null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function needsReconciliation(error: unknown): boolean {
|
|
167
|
+
return new Set(["actor_change_required", "generation_conflict", "operation_outcome_unknown"]).has(
|
|
168
|
+
errorCode(error) ?? "",
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function BrowserAccountsProvider({
|
|
173
|
+
client,
|
|
174
|
+
onActorTransition,
|
|
175
|
+
children,
|
|
176
|
+
broadcastChannelName = DEFAULT_CHANNEL,
|
|
177
|
+
bootstrapLegacySession = false,
|
|
178
|
+
}: BrowserAccountsProviderProps) {
|
|
179
|
+
const [projection, setProjection] = useState<ManagedAuthSessionSetProjection | null>(null);
|
|
180
|
+
const [phase, setPhase] = useState<BrowserAccountTransitionPhase>("loading");
|
|
181
|
+
const [blockers, setBlockers] = useState<BrowserAccountTransitionBlocker[]>([]);
|
|
182
|
+
const [error, setError] = useState<Error | null>(null);
|
|
183
|
+
const [transaction, setTransaction] = useState<ManagedAuthLoginTransaction | null>(null);
|
|
184
|
+
const projectionRef = useRef(projection);
|
|
185
|
+
const transactionRef = useRef(transaction);
|
|
186
|
+
const blockerInspectorsRef = useRef(new Map<string, BlockerInspector>());
|
|
187
|
+
const pendingRef = useRef<PendingNonSecretTransition | null>(null);
|
|
188
|
+
const transactionTargetSlotIdRef = useRef<string | null>(null);
|
|
189
|
+
const beginOperationRef = useRef<{ signature: string; operationId: string } | null>(null);
|
|
190
|
+
const bootstrapOperationRef = useRef<{ generation: string; operationId: string } | null>(null);
|
|
191
|
+
const completionOperationRef = useRef<{
|
|
192
|
+
operationId: string;
|
|
193
|
+
expectedGeneration: string;
|
|
194
|
+
} | null>(null);
|
|
195
|
+
const cancelOperationRef = useRef<{
|
|
196
|
+
operationId: string;
|
|
197
|
+
expectedGeneration: string;
|
|
198
|
+
} | null>(null);
|
|
199
|
+
const pendingCommitSequenceRef = useRef<number | null>(null);
|
|
200
|
+
const invalidatedDuringPendingCommitRef = useRef(false);
|
|
201
|
+
const sequenceRef = useRef(0);
|
|
202
|
+
const transitionAbortRef = useRef<AbortController | null>(null);
|
|
203
|
+
const channelRef = useRef<BroadcastChannel | null>(null);
|
|
204
|
+
const crossTabActorHoldsRef = useRef(new Map<string, ReturnType<typeof setTimeout>>());
|
|
205
|
+
projectionRef.current = projection;
|
|
206
|
+
transactionRef.current = transaction;
|
|
207
|
+
|
|
208
|
+
const inspectBlockers = useCallback((): BrowserAccountTransitionBlocker[] => {
|
|
209
|
+
const current = [...blockerInspectorsRef.current.entries()]
|
|
210
|
+
.map(([id, inspect]) => {
|
|
211
|
+
const blocker = inspect();
|
|
212
|
+
return blocker ? { ...blocker, id } : null;
|
|
213
|
+
})
|
|
214
|
+
.filter((value): value is BrowserAccountTransitionBlocker => value !== null)
|
|
215
|
+
.sort((left, right) => left.id.localeCompare(right.id));
|
|
216
|
+
setBlockers(current);
|
|
217
|
+
return current;
|
|
218
|
+
}, []);
|
|
219
|
+
|
|
220
|
+
const publishActorHint = useCallback((next: ManagedAuthSessionSetProjection | null) => {
|
|
221
|
+
if (!next) return;
|
|
222
|
+
channelRef.current?.postMessage({
|
|
223
|
+
type: "actor-epoch-changed",
|
|
224
|
+
generation: next.generation,
|
|
225
|
+
actorEpoch: next.actorEpoch,
|
|
226
|
+
});
|
|
227
|
+
}, []);
|
|
228
|
+
|
|
229
|
+
const publishActorHold = useCallback(
|
|
230
|
+
(transitionId: string, current: ManagedAuthSessionSetProjection) => {
|
|
231
|
+
channelRef.current?.postMessage({
|
|
232
|
+
type: "actor-transition-pending",
|
|
233
|
+
transitionId,
|
|
234
|
+
generation: current.generation,
|
|
235
|
+
actorEpoch: current.actorEpoch,
|
|
236
|
+
});
|
|
237
|
+
},
|
|
238
|
+
[],
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
const publishActorRelease = useCallback((transitionId: string) => {
|
|
242
|
+
channelRef.current?.postMessage({
|
|
243
|
+
type: "actor-transition-released",
|
|
244
|
+
transitionId,
|
|
245
|
+
});
|
|
246
|
+
}, []);
|
|
247
|
+
|
|
248
|
+
const clearLoginTransactionState = useCallback(() => {
|
|
249
|
+
transactionRef.current = null;
|
|
250
|
+
beginOperationRef.current = null;
|
|
251
|
+
completionOperationRef.current = null;
|
|
252
|
+
cancelOperationRef.current = null;
|
|
253
|
+
transactionTargetSlotIdRef.current = null;
|
|
254
|
+
setTransaction(null);
|
|
255
|
+
}, []);
|
|
256
|
+
|
|
257
|
+
const settleActorTransition = useCallback(
|
|
258
|
+
async (
|
|
259
|
+
kind: BrowserAccountTransitionKind,
|
|
260
|
+
from: ManagedAuthSessionSetProjection | null,
|
|
261
|
+
accepted: ManagedAuthSessionSetProjection | null,
|
|
262
|
+
sequence: number,
|
|
263
|
+
publish: boolean,
|
|
264
|
+
authorityResetConfirmed = false,
|
|
265
|
+
): Promise<ManagedAuthSessionSetProjection | null> => {
|
|
266
|
+
let source = from;
|
|
267
|
+
let target = accepted;
|
|
268
|
+
if (!authorityResetConfirmed && from && target && isOlderProjection(target, from)) {
|
|
269
|
+
target = await client.getSessionSet();
|
|
270
|
+
if (sequenceRef.current !== sequence) return null;
|
|
271
|
+
if (isOlderProjection(target, from)) {
|
|
272
|
+
throw new Error("Browser account response regressed the accepted actor epoch");
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (sameSelectedActor(source, target)) {
|
|
276
|
+
setProjection(target);
|
|
277
|
+
setPhase("ready");
|
|
278
|
+
setError(null);
|
|
279
|
+
return target;
|
|
280
|
+
}
|
|
281
|
+
while (true) {
|
|
282
|
+
transitionAbortRef.current?.abort();
|
|
283
|
+
const controller = new AbortController();
|
|
284
|
+
transitionAbortRef.current = controller;
|
|
285
|
+
// The accepted mutation is already server authority. Notify peers now,
|
|
286
|
+
// before this tab waits for its own auth/access reload, so they can hide
|
|
287
|
+
// and abort old-actor work throughout that entire window.
|
|
288
|
+
if (publish) publishActorHint(target);
|
|
289
|
+
setPhase("loading");
|
|
290
|
+
await onActorTransition({ kind, from: source, to: target, signal: controller.signal });
|
|
291
|
+
if (controller.signal.aborted || sequenceRef.current !== sequence) return null;
|
|
292
|
+
if (!target) {
|
|
293
|
+
setProjection(null);
|
|
294
|
+
setPhase("ready");
|
|
295
|
+
setError(null);
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
const confirmed = await client.reconcileSessionSetAuthority();
|
|
299
|
+
if (controller.signal.aborted || sequenceRef.current !== sequence) return null;
|
|
300
|
+
if (sameActor(target, confirmed) || sameSelectedActor(target, confirmed)) {
|
|
301
|
+
setProjection(confirmed);
|
|
302
|
+
setPhase("ready");
|
|
303
|
+
setError(null);
|
|
304
|
+
return confirmed;
|
|
305
|
+
}
|
|
306
|
+
// Tenant state was loaded for an authority that rotated again while
|
|
307
|
+
// the host callback was pending. The explicit two-probe result is the
|
|
308
|
+
// new authority even when its counters are lower, so keep the actor
|
|
309
|
+
// hidden and settle that authority before exposing ready state.
|
|
310
|
+
source = target;
|
|
311
|
+
target = confirmed;
|
|
312
|
+
}
|
|
313
|
+
},
|
|
314
|
+
[client, onActorTransition, publishActorHint],
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
const reconcile = useCallback(
|
|
318
|
+
async (kind: BrowserAccountTransitionKind, publish = false) => {
|
|
319
|
+
const sequence = ++sequenceRef.current;
|
|
320
|
+
const before = projectionRef.current;
|
|
321
|
+
setPhase("loading");
|
|
322
|
+
let first = await client.reconcileSessionSetAuthority();
|
|
323
|
+
if (sequenceRef.current !== sequence) return null;
|
|
324
|
+
if (
|
|
325
|
+
bootstrapLegacySession &&
|
|
326
|
+
first.mode === "dual" &&
|
|
327
|
+
first.selectedSlotId === null &&
|
|
328
|
+
first.slots.length === 0
|
|
329
|
+
) {
|
|
330
|
+
const bootstrapOperation =
|
|
331
|
+
bootstrapOperationRef.current?.generation === first.generation
|
|
332
|
+
? bootstrapOperationRef.current.operationId
|
|
333
|
+
: operationId();
|
|
334
|
+
bootstrapOperationRef.current = {
|
|
335
|
+
generation: first.generation,
|
|
336
|
+
operationId: bootstrapOperation,
|
|
337
|
+
};
|
|
338
|
+
try {
|
|
339
|
+
first = await client.bootstrapSessionSet({
|
|
340
|
+
operationId: bootstrapOperation,
|
|
341
|
+
expectedGeneration: first.generation,
|
|
342
|
+
});
|
|
343
|
+
bootstrapOperationRef.current = null;
|
|
344
|
+
} catch (caught) {
|
|
345
|
+
if (!needsReconciliation(caught)) throw caught;
|
|
346
|
+
const reconciled = await client.getSessionSet();
|
|
347
|
+
if (sequenceRef.current !== sequence) return null;
|
|
348
|
+
if (reconciled.selectedSlotId === null || reconciled.slots.length === 0) throw caught;
|
|
349
|
+
first = reconciled;
|
|
350
|
+
bootstrapOperationRef.current = null;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
if (sameActor(before, first) || sameSelectedActor(before, first)) {
|
|
354
|
+
setProjection(first);
|
|
355
|
+
setPhase("ready");
|
|
356
|
+
setError(null);
|
|
357
|
+
return first;
|
|
358
|
+
}
|
|
359
|
+
if (kind === "cross_tab") clearLoginTransactionState();
|
|
360
|
+
return await settleActorTransition(kind, before, first, sequence, publish, true);
|
|
361
|
+
},
|
|
362
|
+
[bootstrapLegacySession, clearLoginTransactionState, client, settleActorTransition],
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
const fail = useCallback(
|
|
366
|
+
async (caught: unknown, kind: BrowserAccountTransitionKind): Promise<never> => {
|
|
367
|
+
const nextError = accountError(caught);
|
|
368
|
+
if (needsReconciliation(caught)) {
|
|
369
|
+
try {
|
|
370
|
+
await reconcile(kind);
|
|
371
|
+
} catch {
|
|
372
|
+
// Preserve the original typed failure; a manual refresh remains available.
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
setError(nextError);
|
|
376
|
+
setPhase("recoverable_error");
|
|
377
|
+
throw nextError;
|
|
378
|
+
},
|
|
379
|
+
[reconcile],
|
|
380
|
+
);
|
|
381
|
+
|
|
382
|
+
const executePending = useCallback(
|
|
383
|
+
async (pending: PendingNonSecretTransition): Promise<boolean> => {
|
|
384
|
+
const blocked = inspectBlockers();
|
|
385
|
+
if (blocked.length > 0) {
|
|
386
|
+
pendingRef.current = pending;
|
|
387
|
+
setPhase("blocked");
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
const current = projectionRef.current;
|
|
391
|
+
if (!current) throw new Error("Browser session set is not loaded");
|
|
392
|
+
const expectedGeneration = pending.expectedGeneration ?? current.generation;
|
|
393
|
+
pending.expectedGeneration = expectedGeneration;
|
|
394
|
+
const sequence = ++sequenceRef.current;
|
|
395
|
+
pendingCommitSequenceRef.current = sequence;
|
|
396
|
+
invalidatedDuringPendingCommitRef.current = false;
|
|
397
|
+
pendingRef.current = pending;
|
|
398
|
+
setPhase("committing");
|
|
399
|
+
setError(null);
|
|
400
|
+
const changesActor = pending.changesActor(current);
|
|
401
|
+
if (changesActor) {
|
|
402
|
+
// Peers must hide and abort the old actor before this request can be
|
|
403
|
+
// accepted. Yielding one task after the secret-free hold lets queued
|
|
404
|
+
// BroadcastChannel delivery run before the network mutation starts;
|
|
405
|
+
// a peer that was already dispatching remains an ordinary pre-commit
|
|
406
|
+
// in-flight request and is still fenced by the server epoch.
|
|
407
|
+
publishActorHold(pending.operationId, current);
|
|
408
|
+
await yieldToCrossTabActorHold();
|
|
409
|
+
}
|
|
410
|
+
try {
|
|
411
|
+
let accepted: ManagedAuthSessionSetProjection;
|
|
412
|
+
try {
|
|
413
|
+
accepted = await pending.execute(expectedGeneration);
|
|
414
|
+
} catch (caught) {
|
|
415
|
+
if (errorCode(caught) !== "operation_outcome_unknown") throw caught;
|
|
416
|
+
// The command may already be durable. Replay exactly once before a
|
|
417
|
+
// generic authority read so a response-lost terminal mutation can
|
|
418
|
+
// recover its receipt with the frozen operation and admission data.
|
|
419
|
+
accepted = await pending.execute(expectedGeneration);
|
|
420
|
+
}
|
|
421
|
+
if (sequenceRef.current !== sequence) return false;
|
|
422
|
+
// A successful logout-all response is itself an authority-rotation
|
|
423
|
+
// receipt; every other lower-clock adoption must come from the SDK's
|
|
424
|
+
// explicit two-probe reconciliation path below.
|
|
425
|
+
let authorityResetConfirmed = pending.kind === "logout_all";
|
|
426
|
+
if (invalidatedDuringPendingCommitRef.current) {
|
|
427
|
+
accepted = await client.reconcileSessionSetAuthority();
|
|
428
|
+
authorityResetConfirmed = true;
|
|
429
|
+
if (sequenceRef.current !== sequence) return false;
|
|
430
|
+
}
|
|
431
|
+
pendingRef.current = null;
|
|
432
|
+
if (!authorityResetConfirmed && sameSelectedActor(current, accepted)) {
|
|
433
|
+
setProjection(accepted);
|
|
434
|
+
setPhase("ready");
|
|
435
|
+
setError(null);
|
|
436
|
+
} else {
|
|
437
|
+
await settleActorTransition(
|
|
438
|
+
pending.kind,
|
|
439
|
+
current,
|
|
440
|
+
accepted,
|
|
441
|
+
sequence,
|
|
442
|
+
true,
|
|
443
|
+
authorityResetConfirmed,
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
return true;
|
|
447
|
+
} catch (caught) {
|
|
448
|
+
return await fail(caught, pending.kind);
|
|
449
|
+
} finally {
|
|
450
|
+
if (changesActor) publishActorRelease(pending.operationId);
|
|
451
|
+
if (pendingCommitSequenceRef.current === sequence) {
|
|
452
|
+
pendingCommitSequenceRef.current = null;
|
|
453
|
+
invalidatedDuringPendingCommitRef.current = false;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
},
|
|
457
|
+
[client, fail, inspectBlockers, publishActorHold, publishActorRelease, settleActorTransition],
|
|
458
|
+
);
|
|
459
|
+
|
|
460
|
+
const refresh = useCallback(async () => {
|
|
461
|
+
try {
|
|
462
|
+
return await reconcile("cross_tab");
|
|
463
|
+
} catch (caught) {
|
|
464
|
+
const nextError = accountError(caught);
|
|
465
|
+
setError(nextError);
|
|
466
|
+
setPhase("recoverable_error");
|
|
467
|
+
throw nextError;
|
|
468
|
+
}
|
|
469
|
+
}, [reconcile]);
|
|
470
|
+
|
|
471
|
+
const beginNeutralActorInvalidation = useCallback(
|
|
472
|
+
async (before: ManagedAuthSessionSetProjection | null): Promise<AbortController> => {
|
|
473
|
+
transitionAbortRef.current?.abort();
|
|
474
|
+
const controller = new AbortController();
|
|
475
|
+
transitionAbortRef.current = controller;
|
|
476
|
+
projectionRef.current = null;
|
|
477
|
+
setProjection(null);
|
|
478
|
+
clearLoginTransactionState();
|
|
479
|
+
setPhase("loading");
|
|
480
|
+
setError(null);
|
|
481
|
+
// Do not wait for the authority reread before asking the host to remove
|
|
482
|
+
// and abort the prior actor. A response that was already in flight can
|
|
483
|
+
// otherwise commit old tenant state during the cross-tab hint/read race.
|
|
484
|
+
await onActorTransition({
|
|
485
|
+
kind: "cross_tab",
|
|
486
|
+
from: before,
|
|
487
|
+
to: null,
|
|
488
|
+
signal: controller.signal,
|
|
489
|
+
});
|
|
490
|
+
return controller;
|
|
491
|
+
},
|
|
492
|
+
[clearLoginTransactionState, onActorTransition],
|
|
493
|
+
);
|
|
494
|
+
|
|
495
|
+
const invalidateActor = useCallback(async () => {
|
|
496
|
+
if (pendingCommitSequenceRef.current !== null) {
|
|
497
|
+
invalidatedDuringPendingCommitRef.current = true;
|
|
498
|
+
await beginNeutralActorInvalidation(projectionRef.current);
|
|
499
|
+
return null;
|
|
500
|
+
}
|
|
501
|
+
const sequence = ++sequenceRef.current;
|
|
502
|
+
const before = projectionRef.current;
|
|
503
|
+
try {
|
|
504
|
+
const neutral = await beginNeutralActorInvalidation(before);
|
|
505
|
+
if (neutral.signal.aborted || sequenceRef.current !== sequence) return null;
|
|
506
|
+
const accepted = await client.reconcileSessionSetAuthority();
|
|
507
|
+
if (sequenceRef.current !== sequence) return null;
|
|
508
|
+
return await settleActorTransition("cross_tab", before, accepted, sequence, false, true);
|
|
509
|
+
} catch (caught) {
|
|
510
|
+
if (sequenceRef.current !== sequence) return null;
|
|
511
|
+
const nextError = accountError(caught);
|
|
512
|
+
setError(nextError);
|
|
513
|
+
setPhase("recoverable_error");
|
|
514
|
+
throw nextError;
|
|
515
|
+
}
|
|
516
|
+
}, [beginNeutralActorInvalidation, client, settleActorTransition]);
|
|
517
|
+
|
|
518
|
+
const registerTransitionBlocker = useCallback((id: string, inspect: BlockerInspector) => {
|
|
519
|
+
if (!id.trim()) throw new Error("Browser account transition blocker id is required");
|
|
520
|
+
blockerInspectorsRef.current.set(id, inspect);
|
|
521
|
+
return () => {
|
|
522
|
+
if (blockerInspectorsRef.current.get(id) === inspect) {
|
|
523
|
+
blockerInspectorsRef.current.delete(id);
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
}, []);
|
|
527
|
+
|
|
528
|
+
const continueTransition = useCallback(async (): Promise<boolean> => {
|
|
529
|
+
const pending = pendingRef.current;
|
|
530
|
+
return pending ? await executePending(pending) : false;
|
|
531
|
+
}, [executePending]);
|
|
532
|
+
|
|
533
|
+
const cancelPendingTransition = useCallback(() => {
|
|
534
|
+
pendingRef.current = null;
|
|
535
|
+
setBlockers([]);
|
|
536
|
+
setPhase(projectionRef.current ? "ready" : "loading");
|
|
537
|
+
}, []);
|
|
538
|
+
|
|
539
|
+
const beginLogin = useCallback(
|
|
540
|
+
async (kind: "add" | "reauth", slotId?: string, returnIntent?: string) => {
|
|
541
|
+
const current = projectionRef.current;
|
|
542
|
+
if (!current) throw new Error("Browser session set is not loaded");
|
|
543
|
+
if (kind === "reauth" && slotId === current.selectedSlotId && inspectBlockers().length > 0) {
|
|
544
|
+
setPhase("blocked");
|
|
545
|
+
throw new Error("Settle account transition blockers before re-authenticating");
|
|
546
|
+
}
|
|
547
|
+
const sequence = ++sequenceRef.current;
|
|
548
|
+
setPhase("committing");
|
|
549
|
+
setError(null);
|
|
550
|
+
const signature = JSON.stringify([
|
|
551
|
+
kind,
|
|
552
|
+
slotId ?? null,
|
|
553
|
+
returnIntent ?? null,
|
|
554
|
+
current.generation,
|
|
555
|
+
]);
|
|
556
|
+
const beginOperation =
|
|
557
|
+
beginOperationRef.current?.signature === signature
|
|
558
|
+
? beginOperationRef.current.operationId
|
|
559
|
+
: operationId();
|
|
560
|
+
beginOperationRef.current = { signature, operationId: beginOperation };
|
|
561
|
+
let next: ManagedAuthLoginTransaction;
|
|
562
|
+
try {
|
|
563
|
+
next = await client.beginLoginTransaction({
|
|
564
|
+
operationId: beginOperation,
|
|
565
|
+
expectedGeneration: current.generation,
|
|
566
|
+
kind,
|
|
567
|
+
...(slotId ? { slotId } : {}),
|
|
568
|
+
...(returnIntent ? { returnIntent } : {}),
|
|
569
|
+
});
|
|
570
|
+
} catch (caught) {
|
|
571
|
+
if (sequenceRef.current !== sequence) {
|
|
572
|
+
if (beginOperationRef.current?.operationId === beginOperation) {
|
|
573
|
+
beginOperationRef.current = null;
|
|
574
|
+
}
|
|
575
|
+
throw supersededOperationError();
|
|
576
|
+
}
|
|
577
|
+
return await fail(caught, kind);
|
|
578
|
+
}
|
|
579
|
+
if (sequenceRef.current !== sequence) {
|
|
580
|
+
if (beginOperationRef.current?.operationId === beginOperation) {
|
|
581
|
+
beginOperationRef.current = null;
|
|
582
|
+
}
|
|
583
|
+
throw supersededOperationError();
|
|
584
|
+
}
|
|
585
|
+
beginOperationRef.current = null;
|
|
586
|
+
transactionTargetSlotIdRef.current = slotId ?? null;
|
|
587
|
+
completionOperationRef.current = null;
|
|
588
|
+
cancelOperationRef.current = null;
|
|
589
|
+
setTransaction(next);
|
|
590
|
+
setPhase("ready");
|
|
591
|
+
return next;
|
|
592
|
+
},
|
|
593
|
+
[client, fail, inspectBlockers],
|
|
594
|
+
);
|
|
595
|
+
|
|
596
|
+
const beginAdd = useCallback(
|
|
597
|
+
async (returnIntent?: string) => await beginLogin("add", undefined, returnIntent),
|
|
598
|
+
[beginLogin],
|
|
599
|
+
);
|
|
600
|
+
const beginReauth = useCallback(
|
|
601
|
+
async (slotId: string, returnIntent?: string) =>
|
|
602
|
+
await beginLogin("reauth", slotId, returnIntent),
|
|
603
|
+
[beginLogin],
|
|
604
|
+
);
|
|
605
|
+
|
|
606
|
+
const completeEmailPassword = useCallback(
|
|
607
|
+
async (input: { email: string; password: string }): Promise<boolean> => {
|
|
608
|
+
const current = projectionRef.current;
|
|
609
|
+
const activeTransaction = transactionRef.current;
|
|
610
|
+
if (!current || !activeTransaction) throw new Error("No browser login transaction is active");
|
|
611
|
+
const selectedReauth =
|
|
612
|
+
activeTransaction.kind === "reauth" &&
|
|
613
|
+
transactionTargetSlotIdRef.current !== null &&
|
|
614
|
+
transactionTargetSlotIdRef.current === current.selectedSlotId;
|
|
615
|
+
if (selectedReauth && inspectBlockers().length > 0) {
|
|
616
|
+
setPhase("blocked");
|
|
617
|
+
return false;
|
|
618
|
+
}
|
|
619
|
+
const sequence = ++sequenceRef.current;
|
|
620
|
+
setPhase("committing");
|
|
621
|
+
setError(null);
|
|
622
|
+
const completionOperation = completionOperationRef.current ?? {
|
|
623
|
+
operationId: operationId(),
|
|
624
|
+
expectedGeneration: current.generation,
|
|
625
|
+
};
|
|
626
|
+
completionOperationRef.current = completionOperation;
|
|
627
|
+
try {
|
|
628
|
+
const completed = await client.completeEmailPasswordTransaction({
|
|
629
|
+
operationId: completionOperation.operationId,
|
|
630
|
+
expectedGeneration: completionOperation.expectedGeneration,
|
|
631
|
+
transactionId: activeTransaction.id,
|
|
632
|
+
email: input.email,
|
|
633
|
+
password: input.password,
|
|
634
|
+
});
|
|
635
|
+
if (sequenceRef.current !== sequence) return false;
|
|
636
|
+
completionOperationRef.current = null;
|
|
637
|
+
cancelOperationRef.current = null;
|
|
638
|
+
transactionTargetSlotIdRef.current = null;
|
|
639
|
+
setTransaction(null);
|
|
640
|
+
const actorChanged = completed.projection.actorEpoch !== current.actorEpoch;
|
|
641
|
+
if (actorChanged) {
|
|
642
|
+
await settleActorTransition(
|
|
643
|
+
activeTransaction.kind,
|
|
644
|
+
current,
|
|
645
|
+
completed.projection,
|
|
646
|
+
sequence,
|
|
647
|
+
true,
|
|
648
|
+
);
|
|
649
|
+
} else {
|
|
650
|
+
setProjection(completed.projection);
|
|
651
|
+
setPhase("ready");
|
|
652
|
+
}
|
|
653
|
+
return true;
|
|
654
|
+
} catch (caught) {
|
|
655
|
+
return await fail(caught, activeTransaction.kind);
|
|
656
|
+
}
|
|
657
|
+
},
|
|
658
|
+
[client, fail, inspectBlockers, settleActorTransition],
|
|
659
|
+
);
|
|
660
|
+
|
|
661
|
+
const settleExternalLoginTransaction = useCallback(
|
|
662
|
+
async (transactionId: string): Promise<boolean> => {
|
|
663
|
+
const activeTransaction = transactionRef.current;
|
|
664
|
+
if (!activeTransaction || activeTransaction.id !== transactionId) return false;
|
|
665
|
+
try {
|
|
666
|
+
await reconcile(activeTransaction.kind, true);
|
|
667
|
+
if (transactionRef.current?.id !== transactionId) return false;
|
|
668
|
+
completionOperationRef.current = null;
|
|
669
|
+
cancelOperationRef.current = null;
|
|
670
|
+
transactionTargetSlotIdRef.current = null;
|
|
671
|
+
setTransaction(null);
|
|
672
|
+
return true;
|
|
673
|
+
} catch (caught) {
|
|
674
|
+
const nextError = accountError(caught);
|
|
675
|
+
setError(nextError);
|
|
676
|
+
setPhase("recoverable_error");
|
|
677
|
+
throw nextError;
|
|
678
|
+
}
|
|
679
|
+
},
|
|
680
|
+
[reconcile],
|
|
681
|
+
);
|
|
682
|
+
|
|
683
|
+
const cancelLoginTransaction = useCallback(async () => {
|
|
684
|
+
const current = projectionRef.current;
|
|
685
|
+
const activeTransaction = transactionRef.current;
|
|
686
|
+
if (!current || !activeTransaction) return;
|
|
687
|
+
const sequence = ++sequenceRef.current;
|
|
688
|
+
setPhase("committing");
|
|
689
|
+
const cancelOperation = cancelOperationRef.current ?? {
|
|
690
|
+
operationId: operationId(),
|
|
691
|
+
expectedGeneration: current.generation,
|
|
692
|
+
};
|
|
693
|
+
cancelOperationRef.current = cancelOperation;
|
|
694
|
+
let next: ManagedAuthSessionSetProjection;
|
|
695
|
+
try {
|
|
696
|
+
next = await client.cancelLoginTransaction({
|
|
697
|
+
operationId: cancelOperation.operationId,
|
|
698
|
+
expectedGeneration: cancelOperation.expectedGeneration,
|
|
699
|
+
transactionId: activeTransaction.id,
|
|
700
|
+
});
|
|
701
|
+
} catch (caught) {
|
|
702
|
+
if (sequenceRef.current !== sequence) {
|
|
703
|
+
if (cancelOperationRef.current?.operationId === cancelOperation.operationId) {
|
|
704
|
+
cancelOperationRef.current = null;
|
|
705
|
+
}
|
|
706
|
+
throw supersededOperationError();
|
|
707
|
+
}
|
|
708
|
+
await fail(caught, activeTransaction.kind);
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
if (sequenceRef.current !== sequence) {
|
|
712
|
+
if (cancelOperationRef.current?.operationId === cancelOperation.operationId) {
|
|
713
|
+
cancelOperationRef.current = null;
|
|
714
|
+
}
|
|
715
|
+
throw supersededOperationError();
|
|
716
|
+
}
|
|
717
|
+
clearLoginTransactionState();
|
|
718
|
+
setProjection(next);
|
|
719
|
+
setPhase("ready");
|
|
720
|
+
setError(null);
|
|
721
|
+
}, [clearLoginTransactionState, client, fail]);
|
|
722
|
+
|
|
723
|
+
const selectSlot = useCallback(
|
|
724
|
+
async (slotId: string) => {
|
|
725
|
+
const id = operationId();
|
|
726
|
+
return await executePending({
|
|
727
|
+
kind: "select",
|
|
728
|
+
operationId: id,
|
|
729
|
+
expectedGeneration: null,
|
|
730
|
+
changesActor: (current) => current.selectedSlotId !== slotId,
|
|
731
|
+
execute: async (expectedGeneration) =>
|
|
732
|
+
await client.selectLoginSlot({
|
|
733
|
+
operationId: id,
|
|
734
|
+
expectedGeneration,
|
|
735
|
+
slotId,
|
|
736
|
+
}),
|
|
737
|
+
});
|
|
738
|
+
},
|
|
739
|
+
[client, executePending],
|
|
740
|
+
);
|
|
741
|
+
|
|
742
|
+
const logoutSlot = useCallback(
|
|
743
|
+
async (slotId: string, replacementSlotId: string | null) => {
|
|
744
|
+
const id = operationId();
|
|
745
|
+
return await executePending({
|
|
746
|
+
kind: "logout_one",
|
|
747
|
+
operationId: id,
|
|
748
|
+
expectedGeneration: null,
|
|
749
|
+
changesActor: (current) => current.selectedSlotId === slotId,
|
|
750
|
+
execute: async (expectedGeneration) =>
|
|
751
|
+
await client.logoutLoginSlot({
|
|
752
|
+
operationId: id,
|
|
753
|
+
expectedGeneration,
|
|
754
|
+
slotId,
|
|
755
|
+
replacementSlotId,
|
|
756
|
+
}),
|
|
757
|
+
});
|
|
758
|
+
},
|
|
759
|
+
[client, executePending],
|
|
760
|
+
);
|
|
761
|
+
|
|
762
|
+
const logoutAll = useCallback(async () => {
|
|
763
|
+
const id = operationId();
|
|
764
|
+
return await executePending({
|
|
765
|
+
kind: "logout_all",
|
|
766
|
+
operationId: id,
|
|
767
|
+
expectedGeneration: null,
|
|
768
|
+
// Logout-all always rotates the browser-set authority, even when the set
|
|
769
|
+
// is neutral but still contains slots. Peers must therefore enter the
|
|
770
|
+
// same precommit hold and reconciliation boundary unconditionally.
|
|
771
|
+
changesActor: () => true,
|
|
772
|
+
execute: async (expectedGeneration) => {
|
|
773
|
+
await client.logoutSessionSet({ operationId: id, expectedGeneration });
|
|
774
|
+
return await client.getSessionSet();
|
|
775
|
+
},
|
|
776
|
+
});
|
|
777
|
+
}, [client, executePending]);
|
|
778
|
+
|
|
779
|
+
const resolveDeepLink = useCallback(
|
|
780
|
+
async (path: string) => await client.resolveDeepLink({ path }),
|
|
781
|
+
[client],
|
|
782
|
+
);
|
|
783
|
+
|
|
784
|
+
useEffect(() => {
|
|
785
|
+
const sequence = sequenceRef;
|
|
786
|
+
const transitionAbort = transitionAbortRef;
|
|
787
|
+
void refresh().catch((caught) => {
|
|
788
|
+
setError(accountError(caught));
|
|
789
|
+
setPhase("recoverable_error");
|
|
790
|
+
});
|
|
791
|
+
return () => {
|
|
792
|
+
++sequence.current;
|
|
793
|
+
transitionAbort.current?.abort();
|
|
794
|
+
};
|
|
795
|
+
}, [client, refresh]);
|
|
796
|
+
|
|
797
|
+
useEffect(() => {
|
|
798
|
+
if (!broadcastChannelName || typeof BroadcastChannel === "undefined") return;
|
|
799
|
+
const channel = new BroadcastChannel(broadcastChannelName);
|
|
800
|
+
const actorHolds = crossTabActorHoldsRef.current;
|
|
801
|
+
channelRef.current = channel;
|
|
802
|
+
channel.onmessage = (event: MessageEvent<unknown>) => {
|
|
803
|
+
const value = event.data;
|
|
804
|
+
if (!value || typeof value !== "object") return;
|
|
805
|
+
const hint = value as {
|
|
806
|
+
type?: unknown;
|
|
807
|
+
transitionId?: unknown;
|
|
808
|
+
actorEpoch?: unknown;
|
|
809
|
+
generation?: unknown;
|
|
810
|
+
};
|
|
811
|
+
if (hint.type === "actor-transition-pending") {
|
|
812
|
+
if (
|
|
813
|
+
typeof hint.transitionId !== "string" ||
|
|
814
|
+
!/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu.test(
|
|
815
|
+
hint.transitionId,
|
|
816
|
+
) ||
|
|
817
|
+
typeof hint.actorEpoch !== "string" ||
|
|
818
|
+
typeof hint.generation !== "string" ||
|
|
819
|
+
crossTabActorHoldsRef.current.has(hint.transitionId)
|
|
820
|
+
) {
|
|
821
|
+
return;
|
|
822
|
+
}
|
|
823
|
+
const current = projectionRef.current;
|
|
824
|
+
if (
|
|
825
|
+
current &&
|
|
826
|
+
(current.actorEpoch !== hint.actorEpoch || current.generation !== hint.generation)
|
|
827
|
+
) {
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
if (!current && crossTabActorHoldsRef.current.size === 0) return;
|
|
831
|
+
const firstHold = crossTabActorHoldsRef.current.size === 0;
|
|
832
|
+
const timeout = setTimeout(() => {
|
|
833
|
+
if (!crossTabActorHoldsRef.current.delete(hint.transitionId as string)) return;
|
|
834
|
+
if (crossTabActorHoldsRef.current.size === 0) {
|
|
835
|
+
void invalidateActor().catch(() => undefined);
|
|
836
|
+
}
|
|
837
|
+
}, CROSS_TAB_ACTOR_HOLD_MS);
|
|
838
|
+
crossTabActorHoldsRef.current.set(hint.transitionId, timeout);
|
|
839
|
+
if (firstHold) {
|
|
840
|
+
if (pendingCommitSequenceRef.current !== null) {
|
|
841
|
+
invalidatedDuringPendingCommitRef.current = true;
|
|
842
|
+
} else {
|
|
843
|
+
++sequenceRef.current;
|
|
844
|
+
}
|
|
845
|
+
void beginNeutralActorInvalidation(current).catch(() => undefined);
|
|
846
|
+
}
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
if (hint.type === "actor-transition-released") {
|
|
850
|
+
if (typeof hint.transitionId !== "string") return;
|
|
851
|
+
const timeout = crossTabActorHoldsRef.current.get(hint.transitionId);
|
|
852
|
+
if (timeout === undefined) return;
|
|
853
|
+
clearTimeout(timeout);
|
|
854
|
+
crossTabActorHoldsRef.current.delete(hint.transitionId);
|
|
855
|
+
if (crossTabActorHoldsRef.current.size === 0) {
|
|
856
|
+
void invalidateActor().catch(() => undefined);
|
|
857
|
+
}
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
if (
|
|
861
|
+
hint.type !== "actor-epoch-changed" ||
|
|
862
|
+
typeof hint.actorEpoch !== "string" ||
|
|
863
|
+
typeof hint.generation !== "string" ||
|
|
864
|
+
(hint.actorEpoch === projectionRef.current?.actorEpoch &&
|
|
865
|
+
hint.generation === projectionRef.current?.generation)
|
|
866
|
+
) {
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
for (const timeout of crossTabActorHoldsRef.current.values()) clearTimeout(timeout);
|
|
870
|
+
crossTabActorHoldsRef.current.clear();
|
|
871
|
+
void invalidateActor().catch(() => undefined);
|
|
872
|
+
};
|
|
873
|
+
return () => {
|
|
874
|
+
for (const timeout of actorHolds.values()) clearTimeout(timeout);
|
|
875
|
+
actorHolds.clear();
|
|
876
|
+
channel.close();
|
|
877
|
+
if (channelRef.current === channel) channelRef.current = null;
|
|
878
|
+
};
|
|
879
|
+
}, [beginNeutralActorInvalidation, broadcastChannelName, invalidateActor]);
|
|
880
|
+
|
|
881
|
+
const value = useMemo<BrowserAccountsContextValue>(
|
|
882
|
+
() => ({
|
|
883
|
+
projection,
|
|
884
|
+
phase,
|
|
885
|
+
blockers,
|
|
886
|
+
error,
|
|
887
|
+
transaction,
|
|
888
|
+
hasPendingTransition: pendingRef.current !== null,
|
|
889
|
+
refresh,
|
|
890
|
+
invalidateActor,
|
|
891
|
+
registerTransitionBlocker,
|
|
892
|
+
continueTransition,
|
|
893
|
+
cancelPendingTransition,
|
|
894
|
+
beginAdd,
|
|
895
|
+
beginReauth,
|
|
896
|
+
completeEmailPassword,
|
|
897
|
+
settleExternalLoginTransaction,
|
|
898
|
+
cancelLoginTransaction,
|
|
899
|
+
selectSlot,
|
|
900
|
+
logoutSlot,
|
|
901
|
+
logoutAll,
|
|
902
|
+
resolveDeepLink,
|
|
903
|
+
}),
|
|
904
|
+
[
|
|
905
|
+
projection,
|
|
906
|
+
phase,
|
|
907
|
+
blockers,
|
|
908
|
+
error,
|
|
909
|
+
transaction,
|
|
910
|
+
refresh,
|
|
911
|
+
invalidateActor,
|
|
912
|
+
registerTransitionBlocker,
|
|
913
|
+
continueTransition,
|
|
914
|
+
cancelPendingTransition,
|
|
915
|
+
beginAdd,
|
|
916
|
+
beginReauth,
|
|
917
|
+
completeEmailPassword,
|
|
918
|
+
settleExternalLoginTransaction,
|
|
919
|
+
cancelLoginTransaction,
|
|
920
|
+
selectSlot,
|
|
921
|
+
logoutSlot,
|
|
922
|
+
logoutAll,
|
|
923
|
+
resolveDeepLink,
|
|
924
|
+
],
|
|
925
|
+
);
|
|
926
|
+
|
|
927
|
+
return (
|
|
928
|
+
<BrowserAccountsContext.Provider value={value}>{children}</BrowserAccountsContext.Provider>
|
|
929
|
+
);
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
export function useBrowserAccounts(): BrowserAccountsContextValue {
|
|
933
|
+
const context = useContext(BrowserAccountsContext);
|
|
934
|
+
if (!context) {
|
|
935
|
+
throw new Error(
|
|
936
|
+
"@opengeni/react/accounts: wrap the tree in <BrowserAccountsProvider> before using account hooks",
|
|
937
|
+
);
|
|
938
|
+
}
|
|
939
|
+
return context;
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/** Read the optional controller without requiring non-legacy hosts to mount it. */
|
|
943
|
+
export function useOptionalBrowserAccounts(): BrowserAccountsContextValue | null {
|
|
944
|
+
return useContext(BrowserAccountsContext);
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
export function useBrowserAccountTransitionBlocker(id: string, inspect: BlockerInspector): void {
|
|
948
|
+
const { registerTransitionBlocker } = useBrowserAccounts();
|
|
949
|
+
const inspectRef = useRef(inspect);
|
|
950
|
+
inspectRef.current = inspect;
|
|
951
|
+
useEffect(
|
|
952
|
+
() => registerTransitionBlocker(id, () => inspectRef.current()),
|
|
953
|
+
[id, registerTransitionBlocker],
|
|
954
|
+
);
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
/** No-op on legacy hosts that do not mount the optional accounts provider. */
|
|
958
|
+
export function useOptionalBrowserAccountTransitionBlocker(
|
|
959
|
+
id: string,
|
|
960
|
+
inspect: BlockerInspector,
|
|
961
|
+
): void {
|
|
962
|
+
const context = useContext(BrowserAccountsContext);
|
|
963
|
+
const registerTransitionBlocker = context?.registerTransitionBlocker;
|
|
964
|
+
const inspectRef = useRef(inspect);
|
|
965
|
+
inspectRef.current = inspect;
|
|
966
|
+
useEffect(() => {
|
|
967
|
+
if (!registerTransitionBlocker) return;
|
|
968
|
+
return registerTransitionBlocker(id, () => inspectRef.current());
|
|
969
|
+
}, [id, registerTransitionBlocker]);
|
|
970
|
+
}
|