@facto-to/notifications 0.0.0-stage → 0.1.0-beta.1
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/LICENSE +204 -0
- package/README.md +53 -2
- package/dist/core.d.ts +184 -0
- package/dist/core.js +588 -0
- package/dist/react.d.ts +285 -0
- package/dist/react.js +414 -0
- package/dist/styles.css +1 -0
- package/dist/ui.d.ts +335 -0
- package/dist/ui.js +1124 -0
- package/package.json +88 -4
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
interface EventScope {
|
|
5
|
+
environment?: string;
|
|
6
|
+
accountRealm?: string;
|
|
7
|
+
deploymentTarget?: string;
|
|
8
|
+
organizationId?: string;
|
|
9
|
+
userWallet?: string;
|
|
10
|
+
chainId?: number;
|
|
11
|
+
}
|
|
12
|
+
interface EventMeta {
|
|
13
|
+
eventId: string;
|
|
14
|
+
eventType: string;
|
|
15
|
+
schemaVersion: number;
|
|
16
|
+
occurredAt: string;
|
|
17
|
+
correlationId?: string;
|
|
18
|
+
producer: string;
|
|
19
|
+
scope: EventScope;
|
|
20
|
+
}
|
|
21
|
+
type InAppMessageMode = 'inbox' | 'mandatory_security' | 'live_signal';
|
|
22
|
+
type InAppSeverity = 'info' | 'success' | 'warning' | 'critical';
|
|
23
|
+
interface InAppMessageEnvelope<TPayload = Record<string, unknown>> {
|
|
24
|
+
meta: EventMeta;
|
|
25
|
+
cursor?: string;
|
|
26
|
+
mode: InAppMessageMode;
|
|
27
|
+
severity: InAppSeverity;
|
|
28
|
+
category: string;
|
|
29
|
+
aggregationKey?: string;
|
|
30
|
+
deepLink?: string;
|
|
31
|
+
invalidateKeys: string[];
|
|
32
|
+
payload: TPayload;
|
|
33
|
+
}
|
|
34
|
+
interface SseFrame {
|
|
35
|
+
id?: string;
|
|
36
|
+
/** W3C SSE event type; defaults to `'message'` when the wire frame omits `event:`. */
|
|
37
|
+
event: string;
|
|
38
|
+
/** Raw `event:` field value when explicitly supplied on the wire. */
|
|
39
|
+
explicitEvent?: string;
|
|
40
|
+
data: string;
|
|
41
|
+
retryMs?: number;
|
|
42
|
+
}
|
|
43
|
+
type RealtimeStreamStatus = 'idle' | 'connecting' | 'open' | 'reconnecting' | 'revoked' | 'closed';
|
|
44
|
+
type RealtimeStreamResyncReason = 'reconnect' | 'lagged' | 'visibility';
|
|
45
|
+
interface BroadcastChannelMessageEvent<T = unknown> {
|
|
46
|
+
data: T;
|
|
47
|
+
}
|
|
48
|
+
interface BroadcastChannelLike {
|
|
49
|
+
onmessage: ((event: BroadcastChannelMessageEvent) => void) | null;
|
|
50
|
+
postMessage(message: unknown): void;
|
|
51
|
+
close(): void;
|
|
52
|
+
}
|
|
53
|
+
interface DocumentVisibilityLike {
|
|
54
|
+
readonly visibilityState: string;
|
|
55
|
+
addEventListener(type: 'visibilitychange', listener: () => void): void;
|
|
56
|
+
removeEventListener(type: 'visibilitychange', listener: () => void): void;
|
|
57
|
+
}
|
|
58
|
+
interface RealtimeStreamOptions {
|
|
59
|
+
url: string;
|
|
60
|
+
headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
|
|
61
|
+
credentials?: RequestCredentials;
|
|
62
|
+
fetch?: typeof fetch;
|
|
63
|
+
/**
|
|
64
|
+
* When true, verifies that a non-empty `Authorization` header is present after
|
|
65
|
+
* resolving `headers`. If absent, emits `RealtimeStreamError(SSE_AUTH_HEADER_REQUIRED)`
|
|
66
|
+
* via `onTransportError` and does not send an unauthenticated request.
|
|
67
|
+
*/
|
|
68
|
+
requireAuthHeader?: boolean;
|
|
69
|
+
lastEventId?: string;
|
|
70
|
+
/** Maximum reconnect attempts after disconnect or HTTP/network error. Default `Infinity`; `0` disables reconnect. */
|
|
71
|
+
maxReconnectAttempts?: number;
|
|
72
|
+
/** Base reconnect backoff delay in ms. Defaults to `500` (minimum `10`). */
|
|
73
|
+
reconnectBaseDelayMs?: number;
|
|
74
|
+
/** Upper bound for reconnect backoff delay in ms. Defaults to `30_000`. */
|
|
75
|
+
reconnectMaxDelayMs?: number;
|
|
76
|
+
/** Optional expected server heartbeat interval hint in ms. */
|
|
77
|
+
keepAliveIntervalMs?: number;
|
|
78
|
+
/** Minimum hidden duration in ms before returning to visible triggers `onResync('visibility')`. Defaults to `15_000`. */
|
|
79
|
+
visibilityResyncThresholdMs?: number;
|
|
80
|
+
/** Isolation key for multi-tab BroadcastChannel coordination. */
|
|
81
|
+
scopeKey?: string;
|
|
82
|
+
/** Enables single-leader BroadcastChannel sharing across tabs for the same `scopeKey`. */
|
|
83
|
+
multiTab?: boolean;
|
|
84
|
+
/** Injectable BroadcastChannel factory for deterministic multi-tab tests. */
|
|
85
|
+
broadcastChannelFactory?: (name: string) => BroadcastChannelLike;
|
|
86
|
+
/** Optional election window in ms for multi-tab leader coordination. Defaults to `50`. */
|
|
87
|
+
multiTabElectionTimeoutMs?: number;
|
|
88
|
+
/** Optional heartbeat interval in ms for multi-tab leader liveness. Defaults to `5_000`. */
|
|
89
|
+
multiTabHeartbeatIntervalMs?: number;
|
|
90
|
+
/** Injectable document reference for visibility resync handling. */
|
|
91
|
+
documentRef?: DocumentVisibilityLike;
|
|
92
|
+
/** Optional clock provider for deterministic timing tests. */
|
|
93
|
+
now?: () => number;
|
|
94
|
+
/** Optional RNG `[0, 1)` for deterministic backoff jitter tests. */
|
|
95
|
+
random?: () => number;
|
|
96
|
+
}
|
|
97
|
+
interface RealtimeStreamHandlers {
|
|
98
|
+
onOpen?: () => void;
|
|
99
|
+
onFrame: (frame: SseFrame) => void;
|
|
100
|
+
onLagged?: (droppedCount: number | null, rawData: string) => void;
|
|
101
|
+
onSessionRevoked?: (rawData: string) => void;
|
|
102
|
+
onResync?: (reason: RealtimeStreamResyncReason) => void;
|
|
103
|
+
onStatusChange?: (status: RealtimeStreamStatus) => void;
|
|
104
|
+
onTransportError?: (error: unknown) => void;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
type NotificationAmountTone = 'neutral' | 'positive' | 'danger';
|
|
108
|
+
type NotificationStatusTone = 'neutral' | 'success' | 'warning' | 'danger';
|
|
109
|
+
type NotificationClaimStatus = 'open' | 'claimed' | 'resolved' | 'suppressed';
|
|
110
|
+
interface NotificationLogoSlot {
|
|
111
|
+
src?: string;
|
|
112
|
+
alt?: string;
|
|
113
|
+
fallback?: string;
|
|
114
|
+
}
|
|
115
|
+
interface NotificationStateTransition {
|
|
116
|
+
from: string;
|
|
117
|
+
to: string;
|
|
118
|
+
toTone?: NotificationStatusTone;
|
|
119
|
+
meta?: string;
|
|
120
|
+
}
|
|
121
|
+
interface NotificationAttachment {
|
|
122
|
+
name: string;
|
|
123
|
+
meta?: string;
|
|
124
|
+
href?: string;
|
|
125
|
+
}
|
|
126
|
+
interface NotificationItem {
|
|
127
|
+
id: string;
|
|
128
|
+
eventId?: string;
|
|
129
|
+
aggregationKey?: string;
|
|
130
|
+
mode: InAppMessageMode;
|
|
131
|
+
severity: InAppSeverity;
|
|
132
|
+
category: string;
|
|
133
|
+
title: string;
|
|
134
|
+
subtitle?: string;
|
|
135
|
+
description?: string;
|
|
136
|
+
actorName?: string;
|
|
137
|
+
actorAvatarUrl?: string;
|
|
138
|
+
contextLabel?: string;
|
|
139
|
+
timestamp: string;
|
|
140
|
+
dayGroup?: string;
|
|
141
|
+
unread: boolean;
|
|
142
|
+
mandatory?: boolean;
|
|
143
|
+
deepLink?: string;
|
|
144
|
+
amountDisplay?: string;
|
|
145
|
+
amountTone?: NotificationAmountTone;
|
|
146
|
+
counterparty?: string;
|
|
147
|
+
counterpartyLabel?: string;
|
|
148
|
+
statusLabel?: string;
|
|
149
|
+
statusTone?: NotificationStatusTone;
|
|
150
|
+
logos?: readonly NotificationLogoSlot[];
|
|
151
|
+
stateTransition?: NotificationStateTransition;
|
|
152
|
+
quoteBody?: string;
|
|
153
|
+
quoteMention?: string;
|
|
154
|
+
attachment?: NotificationAttachment;
|
|
155
|
+
claimStatus?: NotificationClaimStatus;
|
|
156
|
+
claimedBy?: string;
|
|
157
|
+
occurrenceCount?: number;
|
|
158
|
+
meta?: Record<string, unknown>;
|
|
159
|
+
}
|
|
160
|
+
interface NotificationListResult {
|
|
161
|
+
items: NotificationItem[];
|
|
162
|
+
unreadCount?: number;
|
|
163
|
+
nextCursor?: string | null;
|
|
164
|
+
}
|
|
165
|
+
interface NotificationSubscribeCallbacks {
|
|
166
|
+
onEnvelope: (envelope: InAppMessageEnvelope) => void;
|
|
167
|
+
onItem: (item: NotificationItem) => void;
|
|
168
|
+
onLagged: (dropped: number | null) => void;
|
|
169
|
+
onSessionRevoked: (reason: string) => void;
|
|
170
|
+
onResync: () => void;
|
|
171
|
+
onStatusChange: (status: RealtimeStreamStatus) => void;
|
|
172
|
+
}
|
|
173
|
+
interface NotificationAdapter {
|
|
174
|
+
scopeKey: string;
|
|
175
|
+
list: (cursor?: string) => Promise<NotificationListResult>;
|
|
176
|
+
markRead?: (id: string) => Promise<void>;
|
|
177
|
+
markAllRead?: () => Promise<void>;
|
|
178
|
+
claim?: (id: string) => Promise<NotificationItem | void>;
|
|
179
|
+
resolve?: (id: string) => Promise<NotificationItem | void>;
|
|
180
|
+
subscribe?: (callbacks: NotificationSubscribeCallbacks) => () => void;
|
|
181
|
+
mapEnvelopeToItem?: (envelope: InAppMessageEnvelope) => NotificationItem | null;
|
|
182
|
+
onInvalidateKeys?: (keys: readonly string[]) => void;
|
|
183
|
+
onSessionRevoked?: (reason: string) => void;
|
|
184
|
+
}
|
|
185
|
+
interface NotificationLiveEvent {
|
|
186
|
+
envelope?: InAppMessageEnvelope;
|
|
187
|
+
item?: NotificationItem | null;
|
|
188
|
+
}
|
|
189
|
+
type NotificationLiveEventListener = (event: NotificationLiveEvent) => void;
|
|
190
|
+
interface NotificationCenterState {
|
|
191
|
+
scopeKey: string;
|
|
192
|
+
items: NotificationItem[];
|
|
193
|
+
unreadCount: number;
|
|
194
|
+
hasExplicitUnreadCount: boolean;
|
|
195
|
+
nextCursor: string | null;
|
|
196
|
+
loading: boolean;
|
|
197
|
+
loadingMore: boolean;
|
|
198
|
+
error: unknown | null;
|
|
199
|
+
streamStatus: RealtimeStreamStatus;
|
|
200
|
+
sessionRevoked: boolean;
|
|
201
|
+
revokedReason: string | null;
|
|
202
|
+
lastEnvelope: InAppMessageEnvelope | null;
|
|
203
|
+
}
|
|
204
|
+
interface NotificationCenterContextValue {
|
|
205
|
+
scopeKey: string;
|
|
206
|
+
items: readonly NotificationItem[];
|
|
207
|
+
unreadCount: number;
|
|
208
|
+
nextCursor: string | null;
|
|
209
|
+
loading: boolean;
|
|
210
|
+
loadingMore: boolean;
|
|
211
|
+
error: unknown | null;
|
|
212
|
+
streamStatus: RealtimeStreamStatus;
|
|
213
|
+
sessionRevoked: boolean;
|
|
214
|
+
revokedReason: string | null;
|
|
215
|
+
lastEnvelope: InAppMessageEnvelope | null;
|
|
216
|
+
refresh: () => Promise<void>;
|
|
217
|
+
loadMore: () => Promise<void>;
|
|
218
|
+
markRead: (id: string) => Promise<void>;
|
|
219
|
+
markAllRead: () => Promise<void>;
|
|
220
|
+
claim: (id: string) => Promise<void>;
|
|
221
|
+
resolve: (id: string) => Promise<void>;
|
|
222
|
+
upsertItem: (item: NotificationItem) => void;
|
|
223
|
+
subscribeLiveEvents: (listener: NotificationLiveEventListener) => () => void;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
declare function countUnreadItems(items: readonly NotificationItem[]): number;
|
|
227
|
+
declare function createEmptyNotificationState(scopeKey: string, loading?: boolean): NotificationCenterState;
|
|
228
|
+
/**
|
|
229
|
+
* Folds an incoming notification into `items`:
|
|
230
|
+
* - If `incoming.aggregationKey` matches an existing item, replaces it and moves it to index 0.
|
|
231
|
+
* - Else if `incoming.id` matches an existing item, replaces it and moves it to index 0.
|
|
232
|
+
* - Else prepends `incoming` at index 0.
|
|
233
|
+
*/
|
|
234
|
+
declare function upsertNotificationItem(items: readonly NotificationItem[], incoming: NotificationItem): {
|
|
235
|
+
items: NotificationItem[];
|
|
236
|
+
unreadDelta: number;
|
|
237
|
+
replaced: boolean;
|
|
238
|
+
};
|
|
239
|
+
declare function applyUpsertToState(state: NotificationCenterState, incoming: NotificationItem): NotificationCenterState;
|
|
240
|
+
declare function applyListResultToState(state: NotificationCenterState, result: NotificationListResult, append: boolean): NotificationCenterState;
|
|
241
|
+
declare function applyMarkReadToState(state: NotificationCenterState, id: string): NotificationCenterState;
|
|
242
|
+
declare function applyMarkAllReadToState(state: NotificationCenterState): NotificationCenterState;
|
|
243
|
+
declare function applyItemPatchToState(state: NotificationCenterState, id: string, patch: Partial<NotificationItem>): NotificationCenterState;
|
|
244
|
+
|
|
245
|
+
interface NotificationCenterProviderProps {
|
|
246
|
+
adapter: NotificationAdapter;
|
|
247
|
+
children: ReactNode;
|
|
248
|
+
/** Optional current operator identifier used for optimistic `claim()` updates. */
|
|
249
|
+
currentOperator?: string;
|
|
250
|
+
/** Optional error hook invoked when an optimistic action (`markRead`, `claim`, `resolve`) fails. */
|
|
251
|
+
onActionError?: (error: unknown) => void;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Scope-isolated notification center state provider.
|
|
255
|
+
* Immediately hard-resets items, unread counts, and errors whenever
|
|
256
|
+
* `adapter.scopeKey` changes to eliminate cross-tenant or cross-environment flicker.
|
|
257
|
+
*/
|
|
258
|
+
declare function NotificationCenterProvider({ adapter, children, currentOperator, onActionError, }: NotificationCenterProviderProps): react.JSX.Element;
|
|
259
|
+
/**
|
|
260
|
+
* Returns the nearest `NotificationCenterProvider` value, throwing if called outside a provider.
|
|
261
|
+
*/
|
|
262
|
+
declare function useNotificationCenter(): NotificationCenterContextValue;
|
|
263
|
+
/**
|
|
264
|
+
* Returns the nearest `NotificationCenterProvider` value, or `null` when used standalone.
|
|
265
|
+
*/
|
|
266
|
+
declare function useOptionalNotificationCenter(): NotificationCenterContextValue | null;
|
|
267
|
+
|
|
268
|
+
interface UseRealtimeStreamOptions extends RealtimeStreamOptions {
|
|
269
|
+
/** When false, closes any active stream and stays idle. Defaults to true. */
|
|
270
|
+
enabled?: boolean;
|
|
271
|
+
}
|
|
272
|
+
interface UseRealtimeStreamResult {
|
|
273
|
+
status: RealtimeStreamStatus;
|
|
274
|
+
lastEventId: string | undefined;
|
|
275
|
+
close: () => void;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* React hook wrapping `createRealtimeStream`. Handler updates do not tear down
|
|
279
|
+
* the underlying SSE connection; changing connection-defining options restarts
|
|
280
|
+
* the stream cleanly.
|
|
281
|
+
*/
|
|
282
|
+
declare function useRealtimeStream(options: UseRealtimeStreamOptions | null | undefined, handlers: RealtimeStreamHandlers): UseRealtimeStreamResult;
|
|
283
|
+
|
|
284
|
+
export { NotificationCenterProvider, applyItemPatchToState, applyListResultToState, applyMarkAllReadToState, applyMarkReadToState, applyUpsertToState, countUnreadItems, createEmptyNotificationState, upsertNotificationItem, useNotificationCenter, useOptionalNotificationCenter, useRealtimeStream };
|
|
285
|
+
export type { NotificationAdapter, NotificationAmountTone, NotificationAttachment, NotificationCenterContextValue, NotificationCenterProviderProps, NotificationCenterState, NotificationClaimStatus, NotificationItem, NotificationListResult, NotificationLiveEvent, NotificationLiveEventListener, NotificationLogoSlot, NotificationStateTransition, NotificationStatusTone, NotificationSubscribeCallbacks, UseRealtimeStreamOptions, UseRealtimeStreamResult };
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as H } from "react/jsx-runtime";
|
|
3
|
+
import { useState as A, useRef as v, useCallback as C, useEffect as q, useMemo as j, useContext as F, createContext as z } from "react";
|
|
4
|
+
import { createRealtimeStream as J } from "./core.js";
|
|
5
|
+
function U(e) {
|
|
6
|
+
let a = 0;
|
|
7
|
+
for (const i of e)
|
|
8
|
+
i.unread && (a += 1);
|
|
9
|
+
return a;
|
|
10
|
+
}
|
|
11
|
+
function h(e, a = !0) {
|
|
12
|
+
return {
|
|
13
|
+
scopeKey: e,
|
|
14
|
+
items: [],
|
|
15
|
+
unreadCount: 0,
|
|
16
|
+
hasExplicitUnreadCount: !1,
|
|
17
|
+
nextCursor: null,
|
|
18
|
+
loading: a,
|
|
19
|
+
loadingMore: !1,
|
|
20
|
+
error: null,
|
|
21
|
+
streamStatus: "idle",
|
|
22
|
+
sessionRevoked: !1,
|
|
23
|
+
revokedReason: null,
|
|
24
|
+
lastEnvelope: null
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function Q(e, a) {
|
|
28
|
+
const i = e.findIndex((t) => a.aggregationKey && t.aggregationKey === a.aggregationKey ? !0 : t.id === a.id);
|
|
29
|
+
if (i === -1)
|
|
30
|
+
return {
|
|
31
|
+
items: [a, ...e],
|
|
32
|
+
unreadDelta: a.unread ? 1 : 0,
|
|
33
|
+
replaced: !1
|
|
34
|
+
};
|
|
35
|
+
const f = e[i], y = [
|
|
36
|
+
a,
|
|
37
|
+
...e.slice(0, i),
|
|
38
|
+
...e.slice(i + 1)
|
|
39
|
+
], o = (a.unread ? 1 : 0) - (f.unread ? 1 : 0);
|
|
40
|
+
return {
|
|
41
|
+
items: y,
|
|
42
|
+
unreadDelta: o,
|
|
43
|
+
replaced: !0
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function k(e, a) {
|
|
47
|
+
const { items: i, unreadDelta: f } = Q(e.items, a), y = e.hasExplicitUnreadCount ? Math.max(0, e.unreadCount + f) : U(i);
|
|
48
|
+
return {
|
|
49
|
+
...e,
|
|
50
|
+
items: i,
|
|
51
|
+
unreadCount: y
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function O(e, a, i) {
|
|
55
|
+
let f;
|
|
56
|
+
if (!i)
|
|
57
|
+
f = [...a.items];
|
|
58
|
+
else {
|
|
59
|
+
const t = new Set(e.items.map((p) => p.id)), K = new Set(
|
|
60
|
+
e.items.map((p) => p.aggregationKey).filter((p) => !!p)
|
|
61
|
+
), m = a.items.filter((p) => !(t.has(p.id) || p.aggregationKey && K.has(p.aggregationKey)));
|
|
62
|
+
f = [...e.items, ...m];
|
|
63
|
+
}
|
|
64
|
+
const y = typeof a.unreadCount == "number", o = y ? Math.max(0, a.unreadCount) : U(f);
|
|
65
|
+
return {
|
|
66
|
+
...e,
|
|
67
|
+
items: f,
|
|
68
|
+
unreadCount: o,
|
|
69
|
+
hasExplicitUnreadCount: y,
|
|
70
|
+
nextCursor: a.nextCursor ?? null,
|
|
71
|
+
loading: !1,
|
|
72
|
+
loadingMore: !1,
|
|
73
|
+
error: null
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function V(e, a) {
|
|
77
|
+
let i = !1, f = !1;
|
|
78
|
+
const y = e.items.map((t) => t.id !== a || !t.unread ? t : (i = !0, f = !0, { ...t, unread: !1 }));
|
|
79
|
+
if (!i) return e;
|
|
80
|
+
const o = e.hasExplicitUnreadCount ? Math.max(0, e.unreadCount - (f ? 1 : 0)) : U(y);
|
|
81
|
+
return {
|
|
82
|
+
...e,
|
|
83
|
+
items: y,
|
|
84
|
+
unreadCount: o
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function W(e) {
|
|
88
|
+
const a = e.items.map(
|
|
89
|
+
(i) => i.unread ? { ...i, unread: !1 } : i
|
|
90
|
+
);
|
|
91
|
+
return {
|
|
92
|
+
...e,
|
|
93
|
+
items: a,
|
|
94
|
+
unreadCount: 0
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function P(e, a, i) {
|
|
98
|
+
let f = !1, y = 0;
|
|
99
|
+
const o = e.items.map((K) => {
|
|
100
|
+
if (K.id !== a) return K;
|
|
101
|
+
f = !0;
|
|
102
|
+
const m = { ...K, ...i };
|
|
103
|
+
return y = (m.unread ? 1 : 0) - (K.unread ? 1 : 0), m;
|
|
104
|
+
});
|
|
105
|
+
if (!f) return e;
|
|
106
|
+
const t = e.hasExplicitUnreadCount ? Math.max(0, e.unreadCount + y) : U(o);
|
|
107
|
+
return {
|
|
108
|
+
...e,
|
|
109
|
+
items: o,
|
|
110
|
+
unreadCount: t
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const G = z(null);
|
|
114
|
+
function $({
|
|
115
|
+
adapter: e,
|
|
116
|
+
children: a,
|
|
117
|
+
currentOperator: i,
|
|
118
|
+
onActionError: f
|
|
119
|
+
}) {
|
|
120
|
+
const [y, o] = A(
|
|
121
|
+
() => h(e.scopeKey, !0)
|
|
122
|
+
), t = v(e);
|
|
123
|
+
t.current = e;
|
|
124
|
+
const K = v(f);
|
|
125
|
+
K.current = f;
|
|
126
|
+
const m = v(i);
|
|
127
|
+
m.current = i;
|
|
128
|
+
const p = e.scopeKey, u = v(0), E = v(/* @__PURE__ */ new Set()), g = y.scopeKey === p ? y : h(p, !0), x = v(g);
|
|
129
|
+
x.current = g;
|
|
130
|
+
const M = C((c) => {
|
|
131
|
+
for (const r of E.current)
|
|
132
|
+
r(c);
|
|
133
|
+
}, []), b = C(async () => {
|
|
134
|
+
const c = t.current.scopeKey, r = u.current;
|
|
135
|
+
o((l) => {
|
|
136
|
+
const s = l.scopeKey === c ? l : h(c, !0);
|
|
137
|
+
return {
|
|
138
|
+
...s,
|
|
139
|
+
loading: s.items.length === 0,
|
|
140
|
+
error: null
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
try {
|
|
144
|
+
const l = await t.current.list();
|
|
145
|
+
if (u.current !== r || t.current.scopeKey !== c)
|
|
146
|
+
return;
|
|
147
|
+
o((s) => {
|
|
148
|
+
const n = s.scopeKey === c ? s : h(c, !1);
|
|
149
|
+
return O(n, l, !1);
|
|
150
|
+
});
|
|
151
|
+
} catch (l) {
|
|
152
|
+
if (u.current !== r || t.current.scopeKey !== c)
|
|
153
|
+
return;
|
|
154
|
+
o((s) => ({
|
|
155
|
+
...s.scopeKey === c ? s : h(c, !1),
|
|
156
|
+
loading: !1,
|
|
157
|
+
loadingMore: !1,
|
|
158
|
+
error: l
|
|
159
|
+
}));
|
|
160
|
+
}
|
|
161
|
+
}, []), I = C(async () => {
|
|
162
|
+
const c = x.current;
|
|
163
|
+
if (!c.nextCursor || c.loading || c.loadingMore) return;
|
|
164
|
+
const r = t.current.scopeKey, l = u.current, s = c.nextCursor;
|
|
165
|
+
o(
|
|
166
|
+
(n) => n.scopeKey === r ? { ...n, loadingMore: !0 } : n
|
|
167
|
+
);
|
|
168
|
+
try {
|
|
169
|
+
const n = await t.current.list(s);
|
|
170
|
+
if (u.current !== l || t.current.scopeKey !== r)
|
|
171
|
+
return;
|
|
172
|
+
o(
|
|
173
|
+
(d) => d.scopeKey === r ? O(d, n, !0) : d
|
|
174
|
+
);
|
|
175
|
+
} catch (n) {
|
|
176
|
+
if (u.current !== l || t.current.scopeKey !== r)
|
|
177
|
+
return;
|
|
178
|
+
o(
|
|
179
|
+
(d) => d.scopeKey === r ? { ...d, loadingMore: !1, error: n } : d
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
}, []), w = C((c) => {
|
|
183
|
+
const r = t.current.scopeKey;
|
|
184
|
+
o((l) => {
|
|
185
|
+
const s = l.scopeKey === r ? l : h(r, !1);
|
|
186
|
+
return k(s, c);
|
|
187
|
+
});
|
|
188
|
+
}, []), T = C(async (c) => {
|
|
189
|
+
const r = t.current.scopeKey, l = u.current, s = x.current;
|
|
190
|
+
o(
|
|
191
|
+
(n) => n.scopeKey === r ? V(n, c) : n
|
|
192
|
+
);
|
|
193
|
+
try {
|
|
194
|
+
await t.current.markRead?.(c);
|
|
195
|
+
} catch (n) {
|
|
196
|
+
u.current === l && t.current.scopeKey === r && (o({ ...s, error: n }), K.current?.(n));
|
|
197
|
+
}
|
|
198
|
+
}, []), L = C(async () => {
|
|
199
|
+
const c = t.current.scopeKey, r = u.current, l = x.current;
|
|
200
|
+
o(
|
|
201
|
+
(s) => s.scopeKey === c ? W(s) : s
|
|
202
|
+
);
|
|
203
|
+
try {
|
|
204
|
+
await t.current.markAllRead?.();
|
|
205
|
+
} catch (s) {
|
|
206
|
+
u.current === r && t.current.scopeKey === c && (o({ ...l, error: s }), K.current?.(s));
|
|
207
|
+
}
|
|
208
|
+
}, []), D = C(async (c) => {
|
|
209
|
+
const r = t.current.scopeKey, l = u.current, s = x.current;
|
|
210
|
+
o(
|
|
211
|
+
(n) => n.scopeKey === r ? P(n, c, {
|
|
212
|
+
claimStatus: "claimed",
|
|
213
|
+
claimedBy: m.current,
|
|
214
|
+
unread: !1
|
|
215
|
+
}) : n
|
|
216
|
+
);
|
|
217
|
+
try {
|
|
218
|
+
const n = await t.current.claim?.(c);
|
|
219
|
+
n && u.current === l && t.current.scopeKey === r && o(
|
|
220
|
+
(d) => d.scopeKey === r ? k(d, n) : d
|
|
221
|
+
);
|
|
222
|
+
} catch (n) {
|
|
223
|
+
u.current === l && t.current.scopeKey === r && (o({ ...s, error: n }), K.current?.(n));
|
|
224
|
+
}
|
|
225
|
+
}, []), N = C(async (c) => {
|
|
226
|
+
const r = t.current.scopeKey, l = u.current, s = x.current;
|
|
227
|
+
o(
|
|
228
|
+
(n) => n.scopeKey === r ? P(n, c, {
|
|
229
|
+
claimStatus: "resolved",
|
|
230
|
+
unread: !1
|
|
231
|
+
}) : n
|
|
232
|
+
);
|
|
233
|
+
try {
|
|
234
|
+
const n = await t.current.resolve?.(c);
|
|
235
|
+
n && u.current === l && t.current.scopeKey === r && o(
|
|
236
|
+
(d) => d.scopeKey === r ? k(d, n) : d
|
|
237
|
+
);
|
|
238
|
+
} catch (n) {
|
|
239
|
+
u.current === l && t.current.scopeKey === r && (o({ ...s, error: n }), K.current?.(n));
|
|
240
|
+
}
|
|
241
|
+
}, []), S = C(
|
|
242
|
+
(c) => (E.current.add(c), () => {
|
|
243
|
+
E.current.delete(c);
|
|
244
|
+
}),
|
|
245
|
+
[]
|
|
246
|
+
);
|
|
247
|
+
q(() => {
|
|
248
|
+
u.current += 1;
|
|
249
|
+
const c = u.current, r = p;
|
|
250
|
+
o(h(r, !0)), b();
|
|
251
|
+
const l = t.current.subscribe?.({
|
|
252
|
+
onEnvelope: (s) => {
|
|
253
|
+
if (u.current !== c || t.current.scopeKey !== r)
|
|
254
|
+
return;
|
|
255
|
+
s.invalidateKeys && s.invalidateKeys.length > 0 && t.current.onInvalidateKeys?.(s.invalidateKeys);
|
|
256
|
+
const n = t.current.mapEnvelopeToItem?.(s) ?? null;
|
|
257
|
+
o((d) => {
|
|
258
|
+
const B = d.scopeKey === r ? d : h(r, !1);
|
|
259
|
+
return { ...n ? k(B, n) : B, lastEnvelope: s };
|
|
260
|
+
}), M({ envelope: s, item: n });
|
|
261
|
+
},
|
|
262
|
+
onItem: (s) => {
|
|
263
|
+
u.current !== c || t.current.scopeKey !== r || (o((n) => {
|
|
264
|
+
const d = n.scopeKey === r ? n : h(r, !1);
|
|
265
|
+
return k(d, s);
|
|
266
|
+
}), M({ item: s }));
|
|
267
|
+
},
|
|
268
|
+
onLagged: () => {
|
|
269
|
+
u.current !== c || t.current.scopeKey !== r || b();
|
|
270
|
+
},
|
|
271
|
+
onResync: () => {
|
|
272
|
+
u.current !== c || t.current.scopeKey !== r || b();
|
|
273
|
+
},
|
|
274
|
+
onSessionRevoked: (s) => {
|
|
275
|
+
u.current !== c || t.current.scopeKey !== r || (o((n) => ({
|
|
276
|
+
...n.scopeKey === r ? n : h(r, !1),
|
|
277
|
+
streamStatus: "revoked",
|
|
278
|
+
sessionRevoked: !0,
|
|
279
|
+
revokedReason: s
|
|
280
|
+
})), t.current.onSessionRevoked?.(s));
|
|
281
|
+
},
|
|
282
|
+
onStatusChange: (s) => {
|
|
283
|
+
u.current !== c || t.current.scopeKey !== r || o((n) => {
|
|
284
|
+
const d = n.scopeKey === r ? n : h(r, !1);
|
|
285
|
+
return {
|
|
286
|
+
...d,
|
|
287
|
+
streamStatus: s,
|
|
288
|
+
sessionRevoked: d.sessionRevoked || s === "revoked"
|
|
289
|
+
};
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
return () => {
|
|
294
|
+
l?.();
|
|
295
|
+
};
|
|
296
|
+
}, [p, b, M]);
|
|
297
|
+
const R = j(
|
|
298
|
+
() => ({
|
|
299
|
+
scopeKey: g.scopeKey,
|
|
300
|
+
items: g.items,
|
|
301
|
+
unreadCount: g.unreadCount,
|
|
302
|
+
nextCursor: g.nextCursor,
|
|
303
|
+
loading: g.loading,
|
|
304
|
+
loadingMore: g.loadingMore,
|
|
305
|
+
error: g.error,
|
|
306
|
+
streamStatus: g.streamStatus,
|
|
307
|
+
sessionRevoked: g.sessionRevoked,
|
|
308
|
+
revokedReason: g.revokedReason,
|
|
309
|
+
lastEnvelope: g.lastEnvelope,
|
|
310
|
+
refresh: b,
|
|
311
|
+
loadMore: I,
|
|
312
|
+
markRead: T,
|
|
313
|
+
markAllRead: L,
|
|
314
|
+
claim: D,
|
|
315
|
+
resolve: N,
|
|
316
|
+
upsertItem: w,
|
|
317
|
+
subscribeLiveEvents: S
|
|
318
|
+
}),
|
|
319
|
+
[
|
|
320
|
+
g,
|
|
321
|
+
b,
|
|
322
|
+
I,
|
|
323
|
+
T,
|
|
324
|
+
L,
|
|
325
|
+
D,
|
|
326
|
+
N,
|
|
327
|
+
w,
|
|
328
|
+
S
|
|
329
|
+
]
|
|
330
|
+
);
|
|
331
|
+
return /* @__PURE__ */ H(G.Provider, { value: R, children: a });
|
|
332
|
+
}
|
|
333
|
+
function ee() {
|
|
334
|
+
const e = F(G);
|
|
335
|
+
if (!e)
|
|
336
|
+
throw new Error(
|
|
337
|
+
"useNotificationCenter must be used within a NotificationCenterProvider"
|
|
338
|
+
);
|
|
339
|
+
return e;
|
|
340
|
+
}
|
|
341
|
+
function te() {
|
|
342
|
+
return F(G);
|
|
343
|
+
}
|
|
344
|
+
function ne(e, a) {
|
|
345
|
+
const [i, f] = A("idle"), [y, o] = A(
|
|
346
|
+
e?.lastEventId
|
|
347
|
+
), t = v(null), K = v(e);
|
|
348
|
+
K.current = e;
|
|
349
|
+
const m = v(a);
|
|
350
|
+
m.current = a;
|
|
351
|
+
const p = !!(e && e.enabled !== !1), u = e?.url, E = e?.scopeKey, g = e?.multiTab, x = e?.credentials, M = e?.requireAuthHeader, b = e?.maxReconnectAttempts, I = e?.reconnectBaseDelayMs, w = e?.reconnectMaxDelayMs, T = e?.visibilityResyncThresholdMs, L = e?.lastEventId;
|
|
352
|
+
q(() => {
|
|
353
|
+
if (!p || !u || !K.current) {
|
|
354
|
+
f("idle");
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
const N = K.current, S = J(N, {
|
|
358
|
+
onOpen: () => {
|
|
359
|
+
o(S.getLastEventId()), m.current.onOpen?.();
|
|
360
|
+
},
|
|
361
|
+
onFrame: (R) => {
|
|
362
|
+
R.id !== void 0 && o(R.id), m.current.onFrame(R);
|
|
363
|
+
},
|
|
364
|
+
onLagged: (R, c) => {
|
|
365
|
+
m.current.onLagged?.(R, c);
|
|
366
|
+
},
|
|
367
|
+
onSessionRevoked: (R) => {
|
|
368
|
+
m.current.onSessionRevoked?.(R);
|
|
369
|
+
},
|
|
370
|
+
onResync: (R) => {
|
|
371
|
+
m.current.onResync?.(R);
|
|
372
|
+
},
|
|
373
|
+
onStatusChange: (R) => {
|
|
374
|
+
f(R), m.current.onStatusChange?.(R);
|
|
375
|
+
},
|
|
376
|
+
onTransportError: (R) => {
|
|
377
|
+
m.current.onTransportError?.(R);
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
return t.current = S, f(S.getStatus()), o(S.getLastEventId()), () => {
|
|
381
|
+
t.current === S && (t.current = null), S.close();
|
|
382
|
+
};
|
|
383
|
+
}, [
|
|
384
|
+
p,
|
|
385
|
+
u,
|
|
386
|
+
E,
|
|
387
|
+
g,
|
|
388
|
+
x,
|
|
389
|
+
M,
|
|
390
|
+
b,
|
|
391
|
+
I,
|
|
392
|
+
w,
|
|
393
|
+
T,
|
|
394
|
+
L
|
|
395
|
+
]);
|
|
396
|
+
const D = C(() => {
|
|
397
|
+
t.current?.close();
|
|
398
|
+
}, []);
|
|
399
|
+
return { status: i, lastEventId: y, close: D };
|
|
400
|
+
}
|
|
401
|
+
export {
|
|
402
|
+
$ as NotificationCenterProvider,
|
|
403
|
+
P as applyItemPatchToState,
|
|
404
|
+
O as applyListResultToState,
|
|
405
|
+
W as applyMarkAllReadToState,
|
|
406
|
+
V as applyMarkReadToState,
|
|
407
|
+
k as applyUpsertToState,
|
|
408
|
+
U as countUnreadItems,
|
|
409
|
+
h as createEmptyNotificationState,
|
|
410
|
+
Q as upsertNotificationItem,
|
|
411
|
+
ee as useNotificationCenter,
|
|
412
|
+
te as useOptionalNotificationCenter,
|
|
413
|
+
ne as useRealtimeStream
|
|
414
|
+
};
|