beeper-inbox-react 0.3.1 → 0.5.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 +22 -5
- package/dist/index.d.ts +77 -18
- package/dist/index.js +2045 -1388
- package/dist/index.js.map +1 -1
- package/package.json +7 -4
- package/dist/index.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,1467 +1,2124 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { createContext, useState, useRef, useCallback, useMemo, useEffect, useContext, useLayoutEffect } from 'react';
|
|
2
|
+
import { createPortal } from 'react-dom';
|
|
3
|
+
import { ConvexClient } from 'convex/browser';
|
|
4
|
+
import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
|
|
5
|
+
|
|
6
|
+
// src/index.tsx
|
|
7
|
+
|
|
8
|
+
// ../inbox-core/dist/websocket.js
|
|
9
|
+
var SESSION_EXPIRED_CLOSE = 4001;
|
|
10
|
+
var SOCKET_PING_MS = 25e3;
|
|
11
|
+
function createWebSocketRealtimeClient(url, options) {
|
|
12
|
+
const open = options.createSocket ?? ((socketUrl) => new WebSocket(socketUrl));
|
|
13
|
+
return {
|
|
14
|
+
onUpdate(_query, args, onResult, onError) {
|
|
15
|
+
const params = new URLSearchParams({
|
|
16
|
+
subscriberId: String(args.subscriberId ?? ""),
|
|
17
|
+
token: String(args.sessionToken ?? ""),
|
|
18
|
+
publishableKey: options.publishableKey
|
|
19
|
+
});
|
|
20
|
+
const socket = open(`${url}${url.includes("?") ? "&" : "?"}${params}`);
|
|
21
|
+
let stopped = false;
|
|
22
|
+
let page = null;
|
|
23
|
+
let loading = false;
|
|
24
|
+
let reloadQueued = false;
|
|
25
|
+
let pingTimer = null;
|
|
26
|
+
const stop = () => {
|
|
27
|
+
stopped = true;
|
|
28
|
+
if (pingTimer)
|
|
29
|
+
clearInterval(pingTimer);
|
|
30
|
+
pingTimer = null;
|
|
31
|
+
};
|
|
32
|
+
const reload = async () => {
|
|
33
|
+
if (loading) {
|
|
34
|
+
reloadQueued = true;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
loading = true;
|
|
38
|
+
try {
|
|
39
|
+
page = await options.loadFirstPage();
|
|
40
|
+
if (!stopped)
|
|
41
|
+
onResult(page);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (!stopped) {
|
|
44
|
+
stop();
|
|
45
|
+
try {
|
|
46
|
+
socket.close(1e3);
|
|
47
|
+
} catch {
|
|
48
|
+
}
|
|
49
|
+
onError(error instanceof Error ? error : new Error("Inbox request failed"));
|
|
50
|
+
}
|
|
51
|
+
} finally {
|
|
52
|
+
loading = false;
|
|
53
|
+
if (reloadQueued && !stopped) {
|
|
54
|
+
reloadQueued = false;
|
|
55
|
+
void reload();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
socket.addEventListener("open", () => {
|
|
60
|
+
if (stopped)
|
|
61
|
+
return;
|
|
62
|
+
pingTimer = setInterval(() => {
|
|
63
|
+
try {
|
|
64
|
+
socket.send("ping");
|
|
65
|
+
} catch {
|
|
66
|
+
}
|
|
67
|
+
}, SOCKET_PING_MS);
|
|
68
|
+
void reload();
|
|
69
|
+
});
|
|
70
|
+
socket.addEventListener("message", (event) => {
|
|
71
|
+
if (stopped || event.data === "pong")
|
|
72
|
+
return;
|
|
73
|
+
let message;
|
|
74
|
+
try {
|
|
75
|
+
message = JSON.parse(String(event.data));
|
|
76
|
+
} catch {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (message.type === "sync") {
|
|
80
|
+
void reload();
|
|
81
|
+
} else if (message.type === "item" && message.item) {
|
|
82
|
+
if (!page || loading || options.reloadOnItem) {
|
|
83
|
+
reloadQueued = loading;
|
|
84
|
+
if (!loading)
|
|
85
|
+
void reload();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
page = applyInboxItem(page, message.item, args);
|
|
89
|
+
onResult(page);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
socket.addEventListener("close", (event) => {
|
|
93
|
+
if (stopped)
|
|
94
|
+
return;
|
|
95
|
+
stop();
|
|
96
|
+
onError(new Error(event.code === SESSION_EXPIRED_CLOSE ? "Widget session expired" : `Realtime connection closed (${event.code})`));
|
|
97
|
+
});
|
|
98
|
+
return () => {
|
|
99
|
+
stop();
|
|
100
|
+
try {
|
|
101
|
+
socket.close(1e3);
|
|
102
|
+
} catch {
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
},
|
|
106
|
+
close() {
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function applyInboxItem(page, item, args) {
|
|
111
|
+
if (page.items.some((existing) => existing._id === item._id))
|
|
112
|
+
return page;
|
|
113
|
+
const kind = item.kind;
|
|
114
|
+
const kinds = Array.isArray(args.kinds) ? args.kinds : [];
|
|
115
|
+
const visible = args.archived !== true && (kinds.length === 0 || kind !== void 0 && kinds.includes(kind));
|
|
116
|
+
const unread = !item.readAt;
|
|
117
|
+
const unreadByKind = page.unreadByKind ? { ...page.unreadByKind } : void 0;
|
|
118
|
+
if (unread && kind && unreadByKind && kind in unreadByKind)
|
|
119
|
+
unreadByKind[kind] = Math.min(100, (unreadByKind[kind] ?? 0) + 1);
|
|
120
|
+
return {
|
|
121
|
+
...page,
|
|
122
|
+
items: visible ? [item, ...page.items] : page.items,
|
|
123
|
+
unreadCount: unread ? Math.min(100, page.unreadCount + 1) : page.unreadCount,
|
|
124
|
+
...unreadByKind ? { unreadByKind } : {}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
var DEFAULT_CONFIG = {
|
|
128
|
+
modeDefault: "system",
|
|
129
|
+
accentColor: "#f97316",
|
|
130
|
+
radius: 14,
|
|
131
|
+
fontScale: 1,
|
|
132
|
+
compact: false,
|
|
133
|
+
showTopicTabs: true,
|
|
134
|
+
showMarkAll: true,
|
|
135
|
+
headerTitle: "Notifications",
|
|
136
|
+
showDismiss: false
|
|
15
137
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
138
|
+
function pickRealtimeSource(config) {
|
|
139
|
+
if (config.realtime?.kind === "ws" && config.realtime.url && typeof WebSocket !== "undefined") {
|
|
140
|
+
return { kind: "ws", url: config.realtime.url };
|
|
141
|
+
}
|
|
142
|
+
return config.convexUrl ? { kind: "convex", url: config.convexUrl } : null;
|
|
143
|
+
}
|
|
144
|
+
function createConvexClient(url) {
|
|
145
|
+
const client = new ConvexClient(url);
|
|
146
|
+
return {
|
|
147
|
+
onUpdate(query, args, onResult, onError) {
|
|
148
|
+
const unsubscribe = client.onUpdate(query, args, onResult, onError);
|
|
149
|
+
return () => unsubscribe();
|
|
150
|
+
},
|
|
151
|
+
close: () => client.close()
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function createWidgetSocketClient(url, publishableKey, loaders) {
|
|
155
|
+
return {
|
|
156
|
+
onUpdate(query, args, onResult, onError) {
|
|
157
|
+
const list = query === "widgetInbox:list";
|
|
158
|
+
return createWebSocketRealtimeClient(url, {
|
|
159
|
+
publishableKey,
|
|
160
|
+
loadFirstPage: () => list ? loaders.current.head() : loaders.current.unread(),
|
|
161
|
+
reloadOnItem: !list
|
|
162
|
+
}).onUpdate(query, args, onResult, onError);
|
|
163
|
+
},
|
|
164
|
+
close() {
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
var DEFAULT_BEEPER_WIDGET_API_BASE_URL = "https://api.beeper.iconeht.com";
|
|
169
|
+
var Ctx = createContext(null);
|
|
170
|
+
var ItemsDemandCtx = createContext(null);
|
|
171
|
+
var INBOX_PAGE_SIZE = 20;
|
|
172
|
+
function mergeOlderItems(head, previous) {
|
|
173
|
+
const last = head[head.length - 1];
|
|
174
|
+
if (!last) {
|
|
175
|
+
return previous;
|
|
176
|
+
}
|
|
177
|
+
const headIds = new Set(head.map((item) => item._id));
|
|
178
|
+
return [
|
|
179
|
+
...head,
|
|
180
|
+
...previous.filter((item) => !headIds.has(item._id) && item.createdAt <= last.createdAt)
|
|
181
|
+
];
|
|
182
|
+
}
|
|
183
|
+
var REALTIME_RETRY_MS = 6e4;
|
|
184
|
+
var MAX_POLL_BACKOFF_MS = 5 * 6e4;
|
|
35
185
|
function isPageHidden() {
|
|
36
|
-
|
|
186
|
+
return typeof document !== "undefined" && document.hidden === true;
|
|
37
187
|
}
|
|
38
188
|
function parseTokenExpiryMs(token) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
? globalThis.atob(padded)
|
|
51
|
-
: null;
|
|
52
|
-
if (!decoded) {
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
const json = JSON.parse(decoded);
|
|
56
|
-
if (typeof json.exp !== "number" || !Number.isFinite(json.exp)) {
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
return json.exp;
|
|
189
|
+
const segments = token.split(".");
|
|
190
|
+
const payload = segments.length >= 3 ? segments[1] : segments[0];
|
|
191
|
+
if (!payload) {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
try {
|
|
195
|
+
const normalized = payload.replace(/-/g, "+").replace(/_/g, "/");
|
|
196
|
+
const padded = normalized.padEnd(Math.ceil(normalized.length / 4) * 4, "=");
|
|
197
|
+
const decoded = typeof globalThis.Buffer !== "undefined" ? globalThis.Buffer.from(padded, "base64").toString("utf8") : typeof globalThis.atob === "function" ? globalThis.atob(padded) : null;
|
|
198
|
+
if (!decoded) {
|
|
199
|
+
return null;
|
|
60
200
|
}
|
|
61
|
-
|
|
62
|
-
|
|
201
|
+
const json = JSON.parse(decoded);
|
|
202
|
+
if (typeof json.exp !== "number" || !Number.isFinite(json.exp)) {
|
|
203
|
+
return null;
|
|
63
204
|
}
|
|
205
|
+
return json.exp;
|
|
206
|
+
} catch {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
64
209
|
}
|
|
65
210
|
function isSessionExpiringSoon(expiresAt, now = Date.now()) {
|
|
66
|
-
|
|
211
|
+
return Boolean(expiresAt && expiresAt - now <= 3e4);
|
|
212
|
+
}
|
|
213
|
+
var CONFIG_CACHE_MS = 10 * 6e4;
|
|
214
|
+
function storedConfigKey(baseUrl, projectId) {
|
|
215
|
+
return `beeper:widget-config:${baseUrl}:${projectId}`;
|
|
216
|
+
}
|
|
217
|
+
function readStoredConfig(baseUrl, projectId) {
|
|
218
|
+
try {
|
|
219
|
+
if (typeof window === "undefined" || !window.sessionStorage) {
|
|
220
|
+
return void 0;
|
|
221
|
+
}
|
|
222
|
+
const raw = window.sessionStorage.getItem(storedConfigKey(baseUrl, projectId));
|
|
223
|
+
const stored = raw ? JSON.parse(raw) : null;
|
|
224
|
+
if (!stored?.config || !stored.savedAt || Date.now() - stored.savedAt > CONFIG_CACHE_MS) {
|
|
225
|
+
return void 0;
|
|
226
|
+
}
|
|
227
|
+
return stored.config;
|
|
228
|
+
} catch {
|
|
229
|
+
return void 0;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function writeStoredConfig(baseUrl, projectId, config) {
|
|
233
|
+
try {
|
|
234
|
+
if (typeof window !== "undefined" && window.sessionStorage) {
|
|
235
|
+
window.sessionStorage.setItem(
|
|
236
|
+
storedConfigKey(baseUrl, projectId),
|
|
237
|
+
JSON.stringify({ savedAt: Date.now(), config })
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
} catch {
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function storedSessionKey(projectId, subscriberId) {
|
|
244
|
+
return `beeper:widget-session:${projectId}:${subscriberId}`;
|
|
245
|
+
}
|
|
246
|
+
function readStoredSession(projectId, subscriberId) {
|
|
247
|
+
try {
|
|
248
|
+
if (typeof window === "undefined" || !window.sessionStorage) {
|
|
249
|
+
return "";
|
|
250
|
+
}
|
|
251
|
+
const key = storedSessionKey(projectId, subscriberId);
|
|
252
|
+
const token = window.sessionStorage.getItem(key) ?? "";
|
|
253
|
+
const expiresAt = parseTokenExpiryMs(token);
|
|
254
|
+
if (!token || !expiresAt || isSessionExpiringSoon(expiresAt)) {
|
|
255
|
+
window.sessionStorage.removeItem(key);
|
|
256
|
+
return "";
|
|
257
|
+
}
|
|
258
|
+
return token;
|
|
259
|
+
} catch {
|
|
260
|
+
return "";
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
function writeStoredSession(projectId, subscriberId, token) {
|
|
264
|
+
try {
|
|
265
|
+
if (typeof window !== "undefined" && window.sessionStorage) {
|
|
266
|
+
window.sessionStorage.setItem(storedSessionKey(projectId, subscriberId), token);
|
|
267
|
+
}
|
|
268
|
+
} catch {
|
|
269
|
+
}
|
|
67
270
|
}
|
|
68
271
|
function resolveTheme(theme) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
return window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
74
|
-
? "dark"
|
|
75
|
-
: "light";
|
|
272
|
+
if (theme === "system") {
|
|
273
|
+
if (typeof window === "undefined") {
|
|
274
|
+
return "light";
|
|
76
275
|
}
|
|
77
|
-
return
|
|
276
|
+
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
277
|
+
}
|
|
278
|
+
return theme;
|
|
78
279
|
}
|
|
79
280
|
function timeAgo(ts) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (diffSec < 86400)
|
|
86
|
-
return `${Math.floor(diffSec / 3600)}h ago`;
|
|
87
|
-
return `${Math.floor(diffSec / 86400)}d ago`;
|
|
281
|
+
const diffSec = Math.max(1, Math.floor((Date.now() - ts) / 1e3));
|
|
282
|
+
if (diffSec < 60) return `${diffSec}s ago`;
|
|
283
|
+
if (diffSec < 3600) return `${Math.floor(diffSec / 60)}m ago`;
|
|
284
|
+
if (diffSec < 86400) return `${Math.floor(diffSec / 3600)}h ago`;
|
|
285
|
+
return `${Math.floor(diffSec / 86400)}d ago`;
|
|
88
286
|
}
|
|
89
287
|
function withAlpha(color, alpha) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
return `color-mix(in srgb, ${normalized} ${Math.round(clamped * 100)}%, transparent)`;
|
|
288
|
+
const normalized = color.trim();
|
|
289
|
+
const clamped = Math.max(0, Math.min(1, alpha));
|
|
290
|
+
const shortHexMatch = /^#([\da-f]{3})$/i.exec(normalized);
|
|
291
|
+
const shortHex = shortHexMatch?.[1];
|
|
292
|
+
if (shortHex) {
|
|
293
|
+
const channels = shortHex.split("").map((channel) => parseInt(channel + channel, 16));
|
|
294
|
+
const [r, g, b] = channels;
|
|
295
|
+
return `rgba(${r}, ${g}, ${b}, ${clamped})`;
|
|
296
|
+
}
|
|
297
|
+
const longHexMatch = /^#([\da-f]{6})$/i.exec(normalized);
|
|
298
|
+
const value = longHexMatch?.[1];
|
|
299
|
+
if (value) {
|
|
300
|
+
const r = parseInt(value.slice(0, 2), 16);
|
|
301
|
+
const g = parseInt(value.slice(2, 4), 16);
|
|
302
|
+
const b = parseInt(value.slice(4, 6), 16);
|
|
303
|
+
return `rgba(${r}, ${g}, ${b}, ${clamped})`;
|
|
304
|
+
}
|
|
305
|
+
return `color-mix(in srgb, ${normalized} ${Math.round(clamped * 100)}%, transparent)`;
|
|
110
306
|
}
|
|
111
307
|
function readStringRecordValue(record, keys) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
308
|
+
if (!record) {
|
|
309
|
+
return void 0;
|
|
310
|
+
}
|
|
311
|
+
for (const key of keys) {
|
|
312
|
+
const value = record[key];
|
|
313
|
+
if (typeof value === "string" && value.trim()) {
|
|
314
|
+
return value.trim();
|
|
120
315
|
}
|
|
121
|
-
|
|
316
|
+
}
|
|
317
|
+
return void 0;
|
|
122
318
|
}
|
|
123
319
|
function normalizeInboxItem(item) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
actionUrl,
|
|
144
|
-
};
|
|
320
|
+
const actionLabel = item.actionLabel ?? readStringRecordValue(item.metadata, [
|
|
321
|
+
"_beeperActionLabel",
|
|
322
|
+
"actionLabel",
|
|
323
|
+
"action_label",
|
|
324
|
+
"ctaLabel",
|
|
325
|
+
"cta_label"
|
|
326
|
+
]);
|
|
327
|
+
const actionUrl = item.actionUrl ?? readStringRecordValue(item.metadata, [
|
|
328
|
+
"_beeperActionUrl",
|
|
329
|
+
"actionUrl",
|
|
330
|
+
"action_url",
|
|
331
|
+
"ctaUrl",
|
|
332
|
+
"cta_url"
|
|
333
|
+
]);
|
|
334
|
+
return {
|
|
335
|
+
...item,
|
|
336
|
+
actionLabel,
|
|
337
|
+
actionUrl
|
|
338
|
+
};
|
|
145
339
|
}
|
|
146
340
|
function resolveActionNavigationTarget(actionUrl, configured) {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
341
|
+
if (configured !== "auto") {
|
|
342
|
+
return configured;
|
|
343
|
+
}
|
|
344
|
+
if (typeof window === "undefined") {
|
|
345
|
+
return "_self";
|
|
346
|
+
}
|
|
347
|
+
try {
|
|
348
|
+
const resolved = new URL(actionUrl, window.location.origin);
|
|
349
|
+
return resolved.origin === window.location.origin ? "_self" : "_blank";
|
|
350
|
+
} catch {
|
|
351
|
+
return "_self";
|
|
352
|
+
}
|
|
160
353
|
}
|
|
161
354
|
function tabKinds(tab) {
|
|
162
|
-
|
|
355
|
+
return tab?.kind === void 0 ? [] : [tab.kind].flat();
|
|
163
356
|
}
|
|
164
|
-
/** A tab's badge: its kinds' unread, or the whole inbox's for an "all" tab. */
|
|
165
357
|
function countTabUnread(tab, unreadCount, unreadByKind) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
? unreadCount
|
|
169
|
-
: kinds.reduce((sum, kind) => sum + (unreadByKind[kind] ?? 0), 0);
|
|
358
|
+
const kinds = tabKinds(tab);
|
|
359
|
+
return kinds.length === 0 ? unreadCount : kinds.reduce((sum, kind) => sum + (unreadByKind[kind] ?? 0), 0);
|
|
170
360
|
}
|
|
171
|
-
/** `&kind=` for the active tab and `&unreadKind=` for every tab's badge. */
|
|
172
361
|
function buildKindsQuery(activeKinds, unreadKinds) {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
362
|
+
return [
|
|
363
|
+
...activeKinds.map((kind) => `&kind=${encodeURIComponent(kind)}`),
|
|
364
|
+
...unreadKinds.map((kind) => `&unreadKind=${encodeURIComponent(kind)}`)
|
|
365
|
+
].join("");
|
|
177
366
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
367
|
+
function BeeperInboxProvider({
|
|
368
|
+
baseUrl = DEFAULT_BEEPER_WIDGET_API_BASE_URL,
|
|
369
|
+
projectId,
|
|
370
|
+
publishableKey,
|
|
371
|
+
sessionToken,
|
|
372
|
+
getSessionToken,
|
|
373
|
+
sessionEndpoint = "/api/beeper/widget-session",
|
|
374
|
+
sessionRequestHeaders,
|
|
375
|
+
persistSession = true,
|
|
376
|
+
subscriber,
|
|
377
|
+
theme = "system",
|
|
378
|
+
customization,
|
|
379
|
+
push,
|
|
380
|
+
actionLinkTarget = "auto",
|
|
381
|
+
onActionClick,
|
|
382
|
+
pollIntervalMs = 15e3,
|
|
383
|
+
onEvent,
|
|
384
|
+
tabs,
|
|
385
|
+
defaultTab,
|
|
386
|
+
children
|
|
387
|
+
}) {
|
|
388
|
+
const [open, setOpen] = useState(false);
|
|
389
|
+
const [items, setItems] = useState([]);
|
|
390
|
+
const [hasMore, setHasMore] = useState(false);
|
|
391
|
+
const [loadingMore, setLoadingMore] = useState(false);
|
|
392
|
+
const cursorRef = useRef(null);
|
|
393
|
+
const hasOlderPagesRef = useRef(false);
|
|
394
|
+
const loadMoreRef = useRef(null);
|
|
395
|
+
const [itemsDemand, setItemsDemand] = useState(0);
|
|
396
|
+
const registerItemsDemand = useCallback(() => {
|
|
397
|
+
setItemsDemand((count) => count + 1);
|
|
398
|
+
return () => setItemsDemand((count) => count - 1);
|
|
399
|
+
}, []);
|
|
400
|
+
const wantsItems = open || itemsDemand > 0;
|
|
401
|
+
const [unreadCount, setUnreadCount] = useState(0);
|
|
402
|
+
const [unreadByKind, setUnreadByKind] = useState({});
|
|
403
|
+
const tabsKey = JSON.stringify(tabs ?? []);
|
|
404
|
+
const stableTabs = useMemo(() => JSON.parse(tabsKey), [tabsKey]);
|
|
405
|
+
const [activeTab, setActiveTabState] = useState(
|
|
406
|
+
defaultTab ?? tabs?.[0]?.key ?? null
|
|
407
|
+
);
|
|
408
|
+
const activeKinds = useMemo(
|
|
409
|
+
() => tabKinds(stableTabs.find((candidate) => candidate.key === activeTab)),
|
|
410
|
+
[activeTab, stableTabs]
|
|
411
|
+
);
|
|
412
|
+
const unreadKinds = useMemo(
|
|
413
|
+
() => Array.from(new Set(stableTabs.flatMap(tabKinds))),
|
|
414
|
+
[stableTabs]
|
|
415
|
+
);
|
|
416
|
+
const kindsQuery = useMemo(
|
|
417
|
+
() => buildKindsQuery(activeKinds, unreadKinds),
|
|
418
|
+
[activeKinds, unreadKinds]
|
|
419
|
+
);
|
|
420
|
+
const [loading, setLoading] = useState(false);
|
|
421
|
+
const [refreshing, setRefreshing] = useState(false);
|
|
422
|
+
const [connectionState, setConnectionState] = useState("connecting");
|
|
423
|
+
const [errorMessage, setErrorMessage] = useState(null);
|
|
424
|
+
const [serverConfig, setServerConfig] = useState(DEFAULT_CONFIG);
|
|
425
|
+
const [realtimeSource, setRealtimeSource] = useState(null);
|
|
426
|
+
const [initialSessionToken] = useState(
|
|
427
|
+
() => sessionToken ?? (persistSession ? readStoredSession(projectId, subscriber.subscriberId) : "")
|
|
428
|
+
);
|
|
429
|
+
const [runtimeSessionToken, setRuntimeSessionToken] = useState(initialSessionToken);
|
|
430
|
+
const [runtimeSessionExpiresAt, setRuntimeSessionExpiresAt] = useState(
|
|
431
|
+
parseTokenExpiryMs(initialSessionToken)
|
|
432
|
+
);
|
|
433
|
+
const runtimeSessionTokenRef = useRef(initialSessionToken);
|
|
434
|
+
const runtimeSessionExpiresAtRef = useRef(
|
|
435
|
+
parseTokenExpiryMs(initialSessionToken)
|
|
436
|
+
);
|
|
437
|
+
const [realtimeClient, setRealtimeClient] = useState(null);
|
|
438
|
+
const realtimeClientRef = useRef(null);
|
|
439
|
+
const realtimeLoadersRef = useRef({
|
|
440
|
+
head: async () => ({}),
|
|
441
|
+
unread: async () => ({})
|
|
442
|
+
});
|
|
443
|
+
const [reconnectNonce, setReconnectNonce] = useState(0);
|
|
444
|
+
const sessionRefreshRef = useRef(null);
|
|
445
|
+
const eventRef = useRef(onEvent);
|
|
446
|
+
const triggerRef = useRef(null);
|
|
447
|
+
const hasLoadedOnceRef = useRef(false);
|
|
448
|
+
const [configResolved, setConfigResolved] = useState(false);
|
|
449
|
+
const [realtimeFailed, setRealtimeFailed] = useState(false);
|
|
450
|
+
const realtimeLiveRef = useRef(false);
|
|
451
|
+
useEffect(() => {
|
|
452
|
+
eventRef.current = onEvent;
|
|
453
|
+
}, [onEvent]);
|
|
454
|
+
const subscriberRef = useRef(subscriber);
|
|
455
|
+
const getSessionTokenRef = useRef(getSessionToken);
|
|
456
|
+
const sessionRequestHeadersRef = useRef(sessionRequestHeaders);
|
|
457
|
+
useEffect(() => {
|
|
458
|
+
subscriberRef.current = subscriber;
|
|
459
|
+
getSessionTokenRef.current = getSessionToken;
|
|
460
|
+
sessionRequestHeadersRef.current = sessionRequestHeaders;
|
|
461
|
+
}, [getSessionToken, sessionRequestHeaders, subscriber]);
|
|
462
|
+
const hasGetSessionToken = Boolean(getSessionToken);
|
|
463
|
+
const canRefreshSession = hasGetSessionToken || Boolean(sessionEndpoint);
|
|
464
|
+
const emitEvent = useCallback(
|
|
465
|
+
(event) => {
|
|
466
|
+
eventRef.current?.(event);
|
|
467
|
+
},
|
|
468
|
+
[]
|
|
469
|
+
);
|
|
470
|
+
useEffect(() => {
|
|
471
|
+
const next = sessionToken ?? (persistSession ? readStoredSession(projectId, subscriber.subscriberId) : "");
|
|
472
|
+
setRuntimeSessionToken(next);
|
|
473
|
+
setRuntimeSessionExpiresAt(parseTokenExpiryMs(next));
|
|
474
|
+
runtimeSessionTokenRef.current = next;
|
|
475
|
+
runtimeSessionExpiresAtRef.current = parseTokenExpiryMs(next);
|
|
476
|
+
}, [persistSession, projectId, sessionToken, subscriber.subscriberId]);
|
|
477
|
+
const resolveSessionToken = useCallback(async () => {
|
|
478
|
+
const subscriber2 = subscriberRef.current;
|
|
479
|
+
const getSessionToken2 = getSessionTokenRef.current;
|
|
480
|
+
if (getSessionToken2) {
|
|
481
|
+
return getSessionToken2(subscriber2);
|
|
482
|
+
}
|
|
483
|
+
const sessionUrl = /^https?:\/\//i.test(sessionEndpoint) ? sessionEndpoint : typeof window !== "undefined" ? new URL(sessionEndpoint, window.location.origin).toString() : sessionEndpoint;
|
|
484
|
+
const response = await fetch(sessionUrl, {
|
|
485
|
+
method: "POST",
|
|
486
|
+
headers: {
|
|
487
|
+
"content-type": "application/json",
|
|
488
|
+
...sessionRequestHeadersRef.current ?? {}
|
|
489
|
+
},
|
|
490
|
+
body: JSON.stringify({
|
|
491
|
+
projectId,
|
|
492
|
+
subscriberId: subscriber2.subscriberId,
|
|
493
|
+
profile: {
|
|
494
|
+
displayName: subscriber2.displayName,
|
|
495
|
+
email: subscriber2.email,
|
|
496
|
+
avatarUrl: subscriber2.avatarUrl,
|
|
497
|
+
metadata: subscriber2.metadata
|
|
276
498
|
}
|
|
499
|
+
})
|
|
500
|
+
});
|
|
501
|
+
const payload = await response.json();
|
|
502
|
+
const token = payload.widgetSessionToken ?? payload.data?.widgetSessionToken;
|
|
503
|
+
if (!response.ok || !token) {
|
|
504
|
+
throw new Error(
|
|
505
|
+
payload?.error ?? "Failed to mint Beeper widget session token"
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
return token;
|
|
509
|
+
}, [projectId, sessionEndpoint]);
|
|
510
|
+
const refreshSession = useCallback(async () => {
|
|
511
|
+
if (sessionRefreshRef.current) {
|
|
512
|
+
return sessionRefreshRef.current;
|
|
513
|
+
}
|
|
514
|
+
emitEvent({
|
|
515
|
+
type: "session.refresh.start",
|
|
516
|
+
projectId,
|
|
517
|
+
subscriberId: subscriber.subscriberId
|
|
518
|
+
});
|
|
519
|
+
const request = resolveSessionToken().then((next) => {
|
|
520
|
+
setRuntimeSessionToken(next);
|
|
521
|
+
runtimeSessionTokenRef.current = next;
|
|
522
|
+
if (persistSession && !sessionToken) {
|
|
523
|
+
writeStoredSession(projectId, subscriber.subscriberId, next);
|
|
524
|
+
}
|
|
525
|
+
const nextExpiresAt = parseTokenExpiryMs(next);
|
|
526
|
+
setRuntimeSessionExpiresAt(nextExpiresAt);
|
|
527
|
+
runtimeSessionExpiresAtRef.current = nextExpiresAt;
|
|
528
|
+
emitEvent({
|
|
529
|
+
type: "session.refresh.success",
|
|
530
|
+
projectId,
|
|
531
|
+
subscriberId: subscriber.subscriberId
|
|
532
|
+
});
|
|
533
|
+
return next;
|
|
534
|
+
}).catch((error) => {
|
|
535
|
+
const message = error instanceof Error ? error.message : "Session refresh failed";
|
|
536
|
+
emitEvent({
|
|
537
|
+
type: "session.refresh.error",
|
|
538
|
+
projectId,
|
|
539
|
+
subscriberId: subscriber.subscriberId,
|
|
540
|
+
message
|
|
541
|
+
});
|
|
542
|
+
throw error;
|
|
543
|
+
}).finally(() => {
|
|
544
|
+
sessionRefreshRef.current = null;
|
|
545
|
+
});
|
|
546
|
+
sessionRefreshRef.current = request;
|
|
547
|
+
return request;
|
|
548
|
+
}, [
|
|
549
|
+
emitEvent,
|
|
550
|
+
persistSession,
|
|
551
|
+
projectId,
|
|
552
|
+
resolveSessionToken,
|
|
553
|
+
sessionToken,
|
|
554
|
+
subscriber.subscriberId
|
|
555
|
+
]);
|
|
556
|
+
const ensureSession = useCallback(async () => {
|
|
557
|
+
const now = Date.now();
|
|
558
|
+
const currentToken = runtimeSessionTokenRef.current;
|
|
559
|
+
const currentExpiresAt = runtimeSessionExpiresAtRef.current;
|
|
560
|
+
if (currentToken) {
|
|
561
|
+
if (isSessionExpiringSoon(currentExpiresAt, now) && canRefreshSession) {
|
|
562
|
+
return refreshSession();
|
|
563
|
+
}
|
|
564
|
+
return currentToken;
|
|
565
|
+
}
|
|
566
|
+
return refreshSession();
|
|
567
|
+
}, [
|
|
568
|
+
canRefreshSession,
|
|
569
|
+
refreshSession
|
|
570
|
+
]);
|
|
571
|
+
const fetchJson = useCallback(
|
|
572
|
+
async (method, path, body, allowSessionRefresh = true, sessionTokenOverride) => {
|
|
573
|
+
let sessionHeaderToken = sessionTokenOverride ?? runtimeSessionTokenRef.current;
|
|
574
|
+
if (!sessionHeaderToken && allowSessionRefresh) {
|
|
575
|
+
sessionHeaderToken = await ensureSession();
|
|
576
|
+
}
|
|
577
|
+
const url = `${baseUrl.replace(/\/$/, "")}/api/v1/projects/${projectId}${path}`;
|
|
578
|
+
const response = await fetch(url, {
|
|
579
|
+
method,
|
|
580
|
+
headers: {
|
|
581
|
+
"content-type": "application/json",
|
|
582
|
+
"x-beeper-publishable-key": publishableKey,
|
|
583
|
+
...sessionHeaderToken ? {
|
|
584
|
+
"x-beeper-session-token": sessionHeaderToken
|
|
585
|
+
} : {}
|
|
586
|
+
},
|
|
587
|
+
body: method === "POST" ? JSON.stringify(body ?? {}) : void 0
|
|
588
|
+
});
|
|
589
|
+
const rawText = await response.text();
|
|
590
|
+
let payload = {};
|
|
591
|
+
try {
|
|
592
|
+
payload = rawText ? JSON.parse(rawText) : {};
|
|
593
|
+
} catch {
|
|
277
594
|
emitEvent({
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
595
|
+
type: "api.error",
|
|
596
|
+
projectId,
|
|
597
|
+
subscriberId: subscriber.subscriberId,
|
|
598
|
+
path,
|
|
599
|
+
status: response.status,
|
|
600
|
+
message: `Non-JSON response from Beeper API. Check baseUrl if you are overriding it. ${rawText.slice(0, 120)}`
|
|
281
601
|
});
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
return next;
|
|
295
|
-
})
|
|
296
|
-
.catch((error) => {
|
|
297
|
-
const message = error instanceof Error ? error.message : "Session refresh failed";
|
|
298
|
-
emitEvent({
|
|
299
|
-
type: "session.refresh.error",
|
|
300
|
-
projectId,
|
|
301
|
-
subscriberId: subscriber.subscriberId,
|
|
302
|
-
message,
|
|
303
|
-
});
|
|
304
|
-
throw error;
|
|
305
|
-
})
|
|
306
|
-
.finally(() => {
|
|
307
|
-
sessionRefreshRef.current = null;
|
|
308
|
-
});
|
|
309
|
-
sessionRefreshRef.current = request;
|
|
310
|
-
return request;
|
|
311
|
-
}, [emitEvent, projectId, resolveSessionToken, subscriber.subscriberId]);
|
|
312
|
-
const ensureSession = useCallback(async () => {
|
|
313
|
-
const now = Date.now();
|
|
314
|
-
const currentToken = runtimeSessionTokenRef.current;
|
|
315
|
-
const currentExpiresAt = runtimeSessionExpiresAtRef.current;
|
|
316
|
-
if (currentToken) {
|
|
317
|
-
if (isSessionExpiringSoon(currentExpiresAt, now) && canRefreshSession) {
|
|
318
|
-
return refreshSession();
|
|
319
|
-
}
|
|
320
|
-
return currentToken;
|
|
321
|
-
}
|
|
322
|
-
return refreshSession();
|
|
323
|
-
}, [
|
|
324
|
-
canRefreshSession,
|
|
325
|
-
refreshSession,
|
|
326
|
-
]);
|
|
327
|
-
const fetchJson = useCallback(async (method, path, body, allowSessionRefresh = true, sessionTokenOverride) => {
|
|
328
|
-
let sessionHeaderToken = sessionTokenOverride ?? runtimeSessionTokenRef.current;
|
|
329
|
-
if (!sessionHeaderToken && allowSessionRefresh) {
|
|
330
|
-
sessionHeaderToken = await ensureSession();
|
|
331
|
-
}
|
|
332
|
-
const url = `${baseUrl.replace(/\/$/, "")}/api/v1/projects/${projectId}${path}`;
|
|
333
|
-
const response = await fetch(url, {
|
|
334
|
-
method,
|
|
335
|
-
headers: {
|
|
336
|
-
"content-type": "application/json",
|
|
337
|
-
"x-beeper-publishable-key": publishableKey,
|
|
338
|
-
...(sessionHeaderToken
|
|
339
|
-
? {
|
|
340
|
-
"x-beeper-session-token": sessionHeaderToken,
|
|
341
|
-
}
|
|
342
|
-
: {}),
|
|
343
|
-
},
|
|
344
|
-
body: method === "POST" ? JSON.stringify(body ?? {}) : undefined,
|
|
602
|
+
throw new Error(
|
|
603
|
+
`Beeper API returned non-JSON (${response.status}). Check the configured baseUrl override.`
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
if (!response.ok) {
|
|
607
|
+
emitEvent({
|
|
608
|
+
type: "api.error",
|
|
609
|
+
projectId,
|
|
610
|
+
subscriberId: subscriber.subscriberId,
|
|
611
|
+
path,
|
|
612
|
+
status: response.status,
|
|
613
|
+
message: payload.error ?? "Beeper widget request failed"
|
|
345
614
|
});
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
payload = rawText ? JSON.parse(rawText) : {};
|
|
350
|
-
}
|
|
351
|
-
catch {
|
|
352
|
-
emitEvent({
|
|
353
|
-
type: "api.error",
|
|
354
|
-
projectId,
|
|
355
|
-
subscriberId: subscriber.subscriberId,
|
|
356
|
-
path,
|
|
357
|
-
status: response.status,
|
|
358
|
-
message: `Non-JSON response from Beeper API. Check baseUrl if you are overriding it. ${rawText.slice(0, 120)}`,
|
|
359
|
-
});
|
|
360
|
-
throw new Error(`Beeper API returned non-JSON (${response.status}). Check the configured baseUrl override.`);
|
|
361
|
-
}
|
|
362
|
-
if (!response.ok) {
|
|
363
|
-
emitEvent({
|
|
364
|
-
type: "api.error",
|
|
365
|
-
projectId,
|
|
366
|
-
subscriberId: subscriber.subscriberId,
|
|
367
|
-
path,
|
|
368
|
-
status: response.status,
|
|
369
|
-
message: payload.error ?? "Beeper widget request failed",
|
|
370
|
-
});
|
|
371
|
-
if (allowSessionRefresh &&
|
|
372
|
-
response.status === 401 &&
|
|
373
|
-
canRefreshSession) {
|
|
374
|
-
const nextToken = await refreshSession();
|
|
375
|
-
return fetchJson(method, path, body, false, nextToken);
|
|
376
|
-
}
|
|
377
|
-
throw new Error(payload.error ?? "Beeper widget request failed");
|
|
615
|
+
if (allowSessionRefresh && response.status === 401 && canRefreshSession) {
|
|
616
|
+
const nextToken = await refreshSession();
|
|
617
|
+
return fetchJson(method, path, body, false, nextToken);
|
|
378
618
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
619
|
+
throw new Error(payload.error ?? "Beeper widget request failed");
|
|
620
|
+
}
|
|
621
|
+
return payload.data;
|
|
622
|
+
},
|
|
623
|
+
[
|
|
624
|
+
baseUrl,
|
|
625
|
+
canRefreshSession,
|
|
626
|
+
emitEvent,
|
|
627
|
+
projectId,
|
|
628
|
+
publishableKey,
|
|
629
|
+
refreshSession,
|
|
630
|
+
ensureSession,
|
|
631
|
+
subscriber.subscriberId
|
|
632
|
+
]
|
|
633
|
+
);
|
|
634
|
+
const fetchWidgetConfigDocument = useCallback(async () => {
|
|
635
|
+
const stored = readStoredConfig(baseUrl, projectId);
|
|
636
|
+
if (stored) {
|
|
637
|
+
return stored;
|
|
638
|
+
}
|
|
639
|
+
const url = `${baseUrl.replace(/\/$/, "")}/api/v1/projects/${projectId}/widget/config`;
|
|
640
|
+
const response = await fetch(url, {
|
|
641
|
+
method: "GET",
|
|
642
|
+
headers: {
|
|
643
|
+
"content-type": "application/json",
|
|
644
|
+
"x-beeper-publishable-key": publishableKey
|
|
645
|
+
}
|
|
646
|
+
});
|
|
647
|
+
const rawText = await response.text();
|
|
648
|
+
let payload = {};
|
|
649
|
+
try {
|
|
650
|
+
payload = rawText ? JSON.parse(rawText) : {};
|
|
651
|
+
} catch {
|
|
652
|
+
return void 0;
|
|
653
|
+
}
|
|
654
|
+
if (!response.ok) {
|
|
655
|
+
return void 0;
|
|
656
|
+
}
|
|
657
|
+
if (payload.data) {
|
|
658
|
+
writeStoredConfig(baseUrl, projectId, payload.data);
|
|
659
|
+
}
|
|
660
|
+
return payload.data;
|
|
661
|
+
}, [baseUrl, projectId, publishableKey]);
|
|
662
|
+
const applyHead = useCallback(
|
|
663
|
+
(inbox) => {
|
|
664
|
+
const head = (inbox.items ?? []).map(normalizeInboxItem);
|
|
665
|
+
if (hasOlderPagesRef.current) {
|
|
666
|
+
setItems((current) => mergeOlderItems(head, current));
|
|
667
|
+
} else {
|
|
668
|
+
cursorRef.current = inbox.nextCursor ?? null;
|
|
669
|
+
setItems(head);
|
|
670
|
+
}
|
|
671
|
+
setHasMore(cursorRef.current !== null);
|
|
672
|
+
setUnreadCount(Number(inbox.unreadCount ?? 0));
|
|
673
|
+
setUnreadByKind(inbox.unreadByKind ?? {});
|
|
674
|
+
hasLoadedOnceRef.current = true;
|
|
675
|
+
},
|
|
676
|
+
[]
|
|
677
|
+
);
|
|
678
|
+
const applyUnread = useCallback(
|
|
679
|
+
(result) => {
|
|
680
|
+
setUnreadCount(Number(result.unreadCount ?? 0));
|
|
681
|
+
setUnreadByKind(result.byKind ?? {});
|
|
682
|
+
},
|
|
683
|
+
[]
|
|
684
|
+
);
|
|
685
|
+
const unreadQuery = useMemo(
|
|
686
|
+
() => unreadKinds.map((kind) => `&kind=${encodeURIComponent(kind)}`).join(""),
|
|
687
|
+
[unreadKinds]
|
|
688
|
+
);
|
|
689
|
+
const headPath = useMemo(
|
|
690
|
+
() => `/widget/inbox?subscriberId=${encodeURIComponent(subscriber.subscriberId)}&limit=${INBOX_PAGE_SIZE}${kindsQuery}`,
|
|
691
|
+
[kindsQuery, subscriber.subscriberId]
|
|
692
|
+
);
|
|
693
|
+
const unreadPath = useMemo(
|
|
694
|
+
() => `/widget/unread?subscriberId=${encodeURIComponent(subscriber.subscriberId)}${unreadQuery}`,
|
|
695
|
+
[subscriber.subscriberId, unreadQuery]
|
|
696
|
+
);
|
|
697
|
+
realtimeLoadersRef.current = {
|
|
698
|
+
head: () => fetchJson("GET", headPath),
|
|
699
|
+
unread: () => fetchJson("GET", unreadPath)
|
|
700
|
+
};
|
|
701
|
+
const refresh = useCallback(async (options) => {
|
|
702
|
+
const silent = options?.silent ?? false;
|
|
703
|
+
if (silent) {
|
|
704
|
+
setRefreshing(true);
|
|
705
|
+
} else {
|
|
706
|
+
setLoading(true);
|
|
707
|
+
}
|
|
708
|
+
try {
|
|
709
|
+
await ensureSession();
|
|
710
|
+
if (!wantsItems) {
|
|
711
|
+
applyUnread(
|
|
712
|
+
await fetchJson("GET", unreadPath)
|
|
713
|
+
);
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
applyHead(
|
|
717
|
+
await fetchJson("GET", headPath)
|
|
718
|
+
);
|
|
719
|
+
} finally {
|
|
720
|
+
if (silent) {
|
|
721
|
+
setRefreshing(false);
|
|
722
|
+
} else {
|
|
723
|
+
setLoading(false);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}, [
|
|
727
|
+
applyHead,
|
|
728
|
+
applyUnread,
|
|
729
|
+
ensureSession,
|
|
730
|
+
fetchJson,
|
|
731
|
+
headPath,
|
|
732
|
+
unreadPath,
|
|
733
|
+
wantsItems
|
|
734
|
+
]);
|
|
735
|
+
const loadMore = useCallback(async () => {
|
|
736
|
+
const cursor = cursorRef.current;
|
|
737
|
+
if (loadMoreRef.current || !cursor) {
|
|
738
|
+
return loadMoreRef.current ?? void 0;
|
|
739
|
+
}
|
|
740
|
+
setLoadingMore(true);
|
|
741
|
+
const request = (async () => {
|
|
742
|
+
const page = await fetchJson(
|
|
743
|
+
"GET",
|
|
744
|
+
`/widget/inbox?subscriberId=${encodeURIComponent(subscriber.subscriberId)}&limit=${INBOX_PAGE_SIZE}&cursor=${encodeURIComponent(cursor)}${kindsQuery}`
|
|
745
|
+
);
|
|
746
|
+
hasOlderPagesRef.current = true;
|
|
747
|
+
cursorRef.current = page.nextCursor ?? null;
|
|
748
|
+
const older = (page.items ?? []).map(normalizeInboxItem);
|
|
749
|
+
setItems((current) => {
|
|
750
|
+
const seen = new Set(current.map((item) => item._id));
|
|
751
|
+
return [...current, ...older.filter((item) => !seen.has(item._id))];
|
|
752
|
+
});
|
|
753
|
+
setHasMore(cursorRef.current !== null);
|
|
754
|
+
})().finally(() => {
|
|
755
|
+
loadMoreRef.current = null;
|
|
756
|
+
setLoadingMore(false);
|
|
757
|
+
});
|
|
758
|
+
loadMoreRef.current = request;
|
|
759
|
+
return request;
|
|
760
|
+
}, [fetchJson, kindsQuery, subscriber.subscriberId]);
|
|
761
|
+
useEffect(() => {
|
|
762
|
+
hasOlderPagesRef.current = false;
|
|
763
|
+
cursorRef.current = null;
|
|
764
|
+
setHasMore(false);
|
|
765
|
+
}, [wantsItems, kindsQuery]);
|
|
766
|
+
const decrementUnread = useCallback((item) => {
|
|
767
|
+
if (!item || item.readAt) {
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
setUnreadCount((current) => Math.max(0, current - 1));
|
|
771
|
+
if (item.kind) {
|
|
772
|
+
const kind = item.kind;
|
|
773
|
+
setUnreadByKind(
|
|
774
|
+
(current) => kind in current ? { ...current, [kind]: Math.max(0, (current[kind] ?? 0) - 1) } : current
|
|
775
|
+
);
|
|
776
|
+
}
|
|
777
|
+
}, []);
|
|
778
|
+
const markRead = useCallback(
|
|
779
|
+
async (inboxItemId) => {
|
|
780
|
+
const previousItems = items;
|
|
781
|
+
const previousUnreadCount = unreadCount;
|
|
782
|
+
const previousUnreadByKind = unreadByKind;
|
|
783
|
+
const now = Date.now();
|
|
784
|
+
setItems(
|
|
785
|
+
(current) => current.map((item) => item._id === inboxItemId ? { ...item, readAt: now } : item)
|
|
786
|
+
);
|
|
787
|
+
decrementUnread(previousItems.find((item) => item._id === inboxItemId));
|
|
788
|
+
await ensureSession();
|
|
789
|
+
try {
|
|
790
|
+
await fetchJson("POST", "/widget/read", {
|
|
791
|
+
subscriberId: subscriber.subscriberId,
|
|
792
|
+
inboxItemIds: [inboxItemId]
|
|
399
793
|
});
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
try {
|
|
403
|
-
payload = rawText ? JSON.parse(rawText) : {};
|
|
404
|
-
}
|
|
405
|
-
catch {
|
|
406
|
-
return undefined;
|
|
407
|
-
}
|
|
408
|
-
if (!response.ok) {
|
|
409
|
-
return undefined;
|
|
410
|
-
}
|
|
411
|
-
return payload.data;
|
|
412
|
-
}, [baseUrl, projectId, publishableKey]);
|
|
413
|
-
const refresh = useCallback(async (options) => {
|
|
414
|
-
const silent = options?.silent ?? false;
|
|
415
|
-
if (silent) {
|
|
416
|
-
setRefreshing(true);
|
|
417
|
-
}
|
|
418
|
-
else {
|
|
419
|
-
setLoading(true);
|
|
420
|
-
}
|
|
421
|
-
try {
|
|
422
|
-
await ensureSession();
|
|
423
|
-
const inbox = await fetchJson("GET", `/widget/inbox?subscriberId=${encodeURIComponent(subscriber.subscriberId)}&limit=50${kindsQuery}`);
|
|
424
|
-
setItems((inbox.items ?? []).map(normalizeInboxItem));
|
|
425
|
-
setUnreadCount(inbox.unreadCount ?? 0);
|
|
426
|
-
setUnreadByKind(inbox.unreadByKind ?? {});
|
|
427
|
-
hasLoadedOnceRef.current = true;
|
|
428
|
-
}
|
|
429
|
-
finally {
|
|
430
|
-
if (silent) {
|
|
431
|
-
setRefreshing(false);
|
|
432
|
-
}
|
|
433
|
-
else {
|
|
434
|
-
setLoading(false);
|
|
435
|
-
}
|
|
794
|
+
if (!realtimeLiveRef.current) {
|
|
795
|
+
await refresh({ silent: true });
|
|
436
796
|
}
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
797
|
+
} catch (error) {
|
|
798
|
+
setItems(previousItems);
|
|
799
|
+
setUnreadCount(previousUnreadCount);
|
|
800
|
+
setUnreadByKind(previousUnreadByKind);
|
|
801
|
+
throw error;
|
|
802
|
+
}
|
|
803
|
+
},
|
|
804
|
+
[
|
|
805
|
+
decrementUnread,
|
|
806
|
+
ensureSession,
|
|
807
|
+
fetchJson,
|
|
808
|
+
items,
|
|
809
|
+
refresh,
|
|
810
|
+
subscriber.subscriberId,
|
|
811
|
+
unreadByKind,
|
|
812
|
+
unreadCount
|
|
813
|
+
]
|
|
814
|
+
);
|
|
815
|
+
const setArchived = useCallback(
|
|
816
|
+
async (inboxItemId, archived) => {
|
|
817
|
+
const previousItems = items;
|
|
818
|
+
const previousUnreadCount = unreadCount;
|
|
819
|
+
const previousUnreadByKind = unreadByKind;
|
|
820
|
+
if (archived) {
|
|
821
|
+
setItems((current) => current.filter((item) => item._id !== inboxItemId));
|
|
455
822
|
decrementUnread(previousItems.find((item) => item._id === inboxItemId));
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
// A live subscription pushes the write's result, so only a poller re-fetches.
|
|
463
|
-
if (!realtimeLiveRef.current) {
|
|
464
|
-
await refresh({ silent: true });
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
catch (error) {
|
|
468
|
-
setItems(previousItems);
|
|
469
|
-
setUnreadCount(previousUnreadCount);
|
|
470
|
-
setUnreadByKind(previousUnreadByKind);
|
|
471
|
-
throw error;
|
|
472
|
-
}
|
|
473
|
-
}, [
|
|
474
|
-
decrementUnread,
|
|
475
|
-
ensureSession,
|
|
476
|
-
fetchJson,
|
|
477
|
-
items,
|
|
478
|
-
refresh,
|
|
479
|
-
subscriber.subscriberId,
|
|
480
|
-
unreadByKind,
|
|
481
|
-
unreadCount,
|
|
482
|
-
]);
|
|
483
|
-
const setArchived = useCallback(async (inboxItemId, archived) => {
|
|
484
|
-
const previousItems = items;
|
|
485
|
-
const previousUnreadCount = unreadCount;
|
|
486
|
-
const previousUnreadByKind = unreadByKind;
|
|
487
|
-
// The panel lists active items, so only archiving changes what it shows.
|
|
488
|
-
if (archived) {
|
|
489
|
-
setItems((current) => current.filter((item) => item._id !== inboxItemId));
|
|
490
|
-
decrementUnread(previousItems.find((item) => item._id === inboxItemId));
|
|
491
|
-
}
|
|
492
|
-
await ensureSession();
|
|
493
|
-
try {
|
|
494
|
-
await fetchJson("POST", archived ? "/widget/archive" : "/widget/unarchive", {
|
|
495
|
-
subscriberId: subscriber.subscriberId,
|
|
496
|
-
inboxItemIds: [inboxItemId],
|
|
497
|
-
});
|
|
498
|
-
if (!realtimeLiveRef.current) {
|
|
499
|
-
await refresh({ silent: true });
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
catch (error) {
|
|
503
|
-
setItems(previousItems);
|
|
504
|
-
setUnreadCount(previousUnreadCount);
|
|
505
|
-
setUnreadByKind(previousUnreadByKind);
|
|
506
|
-
throw error;
|
|
507
|
-
}
|
|
508
|
-
}, [
|
|
509
|
-
decrementUnread,
|
|
510
|
-
ensureSession,
|
|
511
|
-
fetchJson,
|
|
512
|
-
items,
|
|
513
|
-
refresh,
|
|
514
|
-
subscriber.subscriberId,
|
|
515
|
-
unreadByKind,
|
|
516
|
-
unreadCount,
|
|
517
|
-
]);
|
|
518
|
-
const archive = useCallback((id) => setArchived(id, true), [setArchived]);
|
|
519
|
-
const unarchive = useCallback((id) => setArchived(id, false), [setArchived]);
|
|
520
|
-
const setActiveTab = useCallback((key) => {
|
|
521
|
-
if (key === activeTab) {
|
|
522
|
-
return;
|
|
523
|
-
}
|
|
524
|
-
// Clear the old tab's rows rather than showing them under the new one.
|
|
525
|
-
setItems([]);
|
|
526
|
-
setLoading(true);
|
|
527
|
-
setActiveTabState(key);
|
|
528
|
-
}, [activeTab]);
|
|
529
|
-
const tabUnreadCount = useCallback((tab) => countTabUnread(tab, unreadCount, unreadByKind), [unreadByKind, unreadCount]);
|
|
530
|
-
const markAllRead = useCallback(async () => {
|
|
531
|
-
const previousItems = items;
|
|
532
|
-
const previousUnreadCount = unreadCount;
|
|
533
|
-
const now = Date.now();
|
|
534
|
-
const previousUnreadByKind = unreadByKind;
|
|
535
|
-
setItems((current) => current.map((item) => (item.readAt ? item : { ...item, readAt: now })));
|
|
536
|
-
setUnreadCount(0);
|
|
537
|
-
setUnreadByKind((current) => Object.fromEntries(Object.keys(current).map((kind) => [kind, 0])));
|
|
538
|
-
await ensureSession();
|
|
539
|
-
try {
|
|
540
|
-
await fetchJson("POST", "/widget/read-all", {
|
|
541
|
-
subscriberId: subscriber.subscriberId,
|
|
542
|
-
});
|
|
543
|
-
if (!realtimeLiveRef.current) {
|
|
544
|
-
await refresh({ silent: true });
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
catch (error) {
|
|
548
|
-
setItems(previousItems);
|
|
549
|
-
setUnreadCount(previousUnreadCount);
|
|
550
|
-
setUnreadByKind(previousUnreadByKind);
|
|
551
|
-
throw error;
|
|
552
|
-
}
|
|
553
|
-
}, [ensureSession, fetchJson, items, refresh, subscriber.subscriberId, unreadByKind, unreadCount]);
|
|
554
|
-
const registerPushToken = useCallback(async (token, options) => {
|
|
555
|
-
const normalizedToken = token.trim();
|
|
556
|
-
if (!normalizedToken) {
|
|
557
|
-
return;
|
|
558
|
-
}
|
|
559
|
-
await ensureSession();
|
|
560
|
-
await fetchJson("POST", "/widget/push/register", {
|
|
561
|
-
subscriberId: subscriber.subscriberId,
|
|
562
|
-
pushToken: normalizedToken,
|
|
563
|
-
platform: options?.platform ?? "web",
|
|
564
|
-
deviceLabel: options?.deviceLabel,
|
|
565
|
-
metadata: options?.metadata,
|
|
823
|
+
}
|
|
824
|
+
await ensureSession();
|
|
825
|
+
try {
|
|
826
|
+
await fetchJson("POST", archived ? "/widget/archive" : "/widget/unarchive", {
|
|
827
|
+
subscriberId: subscriber.subscriberId,
|
|
828
|
+
inboxItemIds: [inboxItemId]
|
|
566
829
|
});
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
const normalizedToken = token.trim();
|
|
570
|
-
if (!normalizedToken) {
|
|
571
|
-
return;
|
|
830
|
+
if (!realtimeLiveRef.current) {
|
|
831
|
+
await refresh({ silent: true });
|
|
572
832
|
}
|
|
833
|
+
} catch (error) {
|
|
834
|
+
setItems(previousItems);
|
|
835
|
+
setUnreadCount(previousUnreadCount);
|
|
836
|
+
setUnreadByKind(previousUnreadByKind);
|
|
837
|
+
throw error;
|
|
838
|
+
}
|
|
839
|
+
},
|
|
840
|
+
[
|
|
841
|
+
decrementUnread,
|
|
842
|
+
ensureSession,
|
|
843
|
+
fetchJson,
|
|
844
|
+
items,
|
|
845
|
+
refresh,
|
|
846
|
+
subscriber.subscriberId,
|
|
847
|
+
unreadByKind,
|
|
848
|
+
unreadCount
|
|
849
|
+
]
|
|
850
|
+
);
|
|
851
|
+
const archive = useCallback((id) => setArchived(id, true), [setArchived]);
|
|
852
|
+
const unarchive = useCallback((id) => setArchived(id, false), [setArchived]);
|
|
853
|
+
const setActiveTab = useCallback(
|
|
854
|
+
(key) => {
|
|
855
|
+
if (key === activeTab) {
|
|
856
|
+
return;
|
|
857
|
+
}
|
|
858
|
+
setItems([]);
|
|
859
|
+
setLoading(true);
|
|
860
|
+
setActiveTabState(key);
|
|
861
|
+
},
|
|
862
|
+
[activeTab]
|
|
863
|
+
);
|
|
864
|
+
const tabUnreadCount = useCallback(
|
|
865
|
+
(tab) => countTabUnread(tab, unreadCount, unreadByKind),
|
|
866
|
+
[unreadByKind, unreadCount]
|
|
867
|
+
);
|
|
868
|
+
const markAllRead = useCallback(async () => {
|
|
869
|
+
const previousItems = items;
|
|
870
|
+
const previousUnreadCount = unreadCount;
|
|
871
|
+
const now = Date.now();
|
|
872
|
+
const previousUnreadByKind = unreadByKind;
|
|
873
|
+
setItems((current) => current.map((item) => item.readAt ? item : { ...item, readAt: now }));
|
|
874
|
+
setUnreadCount(0);
|
|
875
|
+
setUnreadByKind(
|
|
876
|
+
(current) => Object.fromEntries(Object.keys(current).map((kind) => [kind, 0]))
|
|
877
|
+
);
|
|
878
|
+
await ensureSession();
|
|
879
|
+
try {
|
|
880
|
+
await fetchJson("POST", "/widget/read-all", {
|
|
881
|
+
subscriberId: subscriber.subscriberId
|
|
882
|
+
});
|
|
883
|
+
if (!realtimeLiveRef.current) {
|
|
884
|
+
await refresh({ silent: true });
|
|
885
|
+
}
|
|
886
|
+
} catch (error) {
|
|
887
|
+
setItems(previousItems);
|
|
888
|
+
setUnreadCount(previousUnreadCount);
|
|
889
|
+
setUnreadByKind(previousUnreadByKind);
|
|
890
|
+
throw error;
|
|
891
|
+
}
|
|
892
|
+
}, [ensureSession, fetchJson, items, refresh, subscriber.subscriberId, unreadByKind, unreadCount]);
|
|
893
|
+
const registerPushToken = useCallback(
|
|
894
|
+
async (token, options) => {
|
|
895
|
+
const normalizedToken = token.trim();
|
|
896
|
+
if (!normalizedToken) {
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
await ensureSession();
|
|
900
|
+
await fetchJson("POST", "/widget/push/register", {
|
|
901
|
+
subscriberId: subscriber.subscriberId,
|
|
902
|
+
pushToken: normalizedToken,
|
|
903
|
+
platform: options?.platform ?? "web",
|
|
904
|
+
deviceLabel: options?.deviceLabel,
|
|
905
|
+
metadata: options?.metadata
|
|
906
|
+
});
|
|
907
|
+
},
|
|
908
|
+
[ensureSession, fetchJson, subscriber.subscriberId]
|
|
909
|
+
);
|
|
910
|
+
const unregisterPushToken = useCallback(
|
|
911
|
+
async (token) => {
|
|
912
|
+
const normalizedToken = token.trim();
|
|
913
|
+
if (!normalizedToken) {
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
916
|
+
await ensureSession();
|
|
917
|
+
await fetchJson("POST", "/widget/push/unregister", {
|
|
918
|
+
subscriberId: subscriber.subscriberId,
|
|
919
|
+
pushToken: normalizedToken
|
|
920
|
+
});
|
|
921
|
+
},
|
|
922
|
+
[ensureSession, fetchJson, subscriber.subscriberId]
|
|
923
|
+
);
|
|
924
|
+
const handleActionClick = useCallback(
|
|
925
|
+
async (item) => {
|
|
926
|
+
const actionUrl = item.actionUrl?.trim();
|
|
927
|
+
if (!actionUrl) {
|
|
928
|
+
return;
|
|
929
|
+
}
|
|
930
|
+
const shouldNavigate = await onActionClick?.({
|
|
931
|
+
item,
|
|
932
|
+
actionLabel: item.actionLabel,
|
|
933
|
+
actionUrl
|
|
934
|
+
}) !== false;
|
|
935
|
+
try {
|
|
573
936
|
await ensureSession();
|
|
574
|
-
await fetchJson("POST", "/widget/
|
|
575
|
-
|
|
576
|
-
|
|
937
|
+
await fetchJson("POST", "/widget/action-click", {
|
|
938
|
+
subscriberId: subscriber.subscriberId,
|
|
939
|
+
inboxItemId: item._id,
|
|
940
|
+
actionLabel: item.actionLabel,
|
|
941
|
+
actionUrl
|
|
577
942
|
});
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
943
|
+
} catch {
|
|
944
|
+
}
|
|
945
|
+
if (!shouldNavigate || typeof window === "undefined") {
|
|
946
|
+
return;
|
|
947
|
+
}
|
|
948
|
+
const target = resolveActionNavigationTarget(actionUrl, actionLinkTarget);
|
|
949
|
+
if (target === "_blank") {
|
|
950
|
+
window.open(actionUrl, "_blank", "noopener,noreferrer");
|
|
951
|
+
return;
|
|
952
|
+
}
|
|
953
|
+
window.location.assign(actionUrl);
|
|
954
|
+
},
|
|
955
|
+
[
|
|
956
|
+
actionLinkTarget,
|
|
957
|
+
ensureSession,
|
|
958
|
+
fetchJson,
|
|
959
|
+
onActionClick,
|
|
960
|
+
subscriber.subscriberId
|
|
961
|
+
]
|
|
962
|
+
);
|
|
963
|
+
const reconnect = useCallback(async () => {
|
|
964
|
+
emitEvent({
|
|
965
|
+
type: "realtime.reconnect.manual",
|
|
966
|
+
projectId,
|
|
967
|
+
subscriberId: subscriber.subscriberId
|
|
968
|
+
});
|
|
969
|
+
setConnectionState("connecting");
|
|
970
|
+
setErrorMessage(null);
|
|
971
|
+
setReconnectNonce((n) => n + 1);
|
|
972
|
+
if (canRefreshSession) {
|
|
973
|
+
await refreshSession();
|
|
974
|
+
} else {
|
|
975
|
+
await ensureSession();
|
|
976
|
+
}
|
|
977
|
+
}, [
|
|
978
|
+
canRefreshSession,
|
|
979
|
+
emitEvent,
|
|
980
|
+
ensureSession,
|
|
981
|
+
projectId,
|
|
982
|
+
refreshSession,
|
|
983
|
+
subscriber.subscriberId
|
|
984
|
+
]);
|
|
985
|
+
useEffect(() => {
|
|
986
|
+
let cancelled = false;
|
|
987
|
+
async function loadConfig() {
|
|
988
|
+
try {
|
|
989
|
+
const config = await fetchWidgetConfigDocument();
|
|
990
|
+
if (!cancelled && config) {
|
|
991
|
+
const next = pickRealtimeSource(config);
|
|
992
|
+
setRealtimeSource(
|
|
993
|
+
(current) => current?.kind === next?.kind && current?.url === next?.url ? current : next
|
|
994
|
+
);
|
|
995
|
+
setServerConfig({ ...DEFAULT_CONFIG, ...config });
|
|
597
996
|
}
|
|
598
|
-
|
|
599
|
-
|
|
997
|
+
} catch {
|
|
998
|
+
if (!cancelled) {
|
|
999
|
+
setRealtimeSource(null);
|
|
1000
|
+
setServerConfig(DEFAULT_CONFIG);
|
|
600
1001
|
}
|
|
601
|
-
|
|
602
|
-
|
|
1002
|
+
} finally {
|
|
1003
|
+
if (!cancelled) {
|
|
1004
|
+
setConfigResolved(true);
|
|
603
1005
|
}
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
void loadConfig();
|
|
1009
|
+
return () => {
|
|
1010
|
+
cancelled = true;
|
|
1011
|
+
};
|
|
1012
|
+
}, [fetchWidgetConfigDocument]);
|
|
1013
|
+
useEffect(() => {
|
|
1014
|
+
if (!runtimeSessionToken || !runtimeSessionExpiresAt || !hasGetSessionToken) {
|
|
1015
|
+
return;
|
|
1016
|
+
}
|
|
1017
|
+
const refreshInMs = Math.max(runtimeSessionExpiresAt - Date.now() - 6e4, 1e3);
|
|
1018
|
+
const timer = setTimeout(() => {
|
|
1019
|
+
refreshSession().catch(() => void 0);
|
|
1020
|
+
}, refreshInMs);
|
|
1021
|
+
return () => clearTimeout(timer);
|
|
1022
|
+
}, [hasGetSessionToken, refreshSession, runtimeSessionExpiresAt, runtimeSessionToken]);
|
|
1023
|
+
const pushToken = push?.token;
|
|
1024
|
+
const pushPlatform = push?.platform;
|
|
1025
|
+
const pushDeviceLabel = push?.deviceLabel;
|
|
1026
|
+
const pushMetadataKey = push?.metadata ? JSON.stringify(push.metadata) : "";
|
|
1027
|
+
const pushAutoRegister = push?.autoRegister !== false;
|
|
1028
|
+
const pushAutoUnregister = Boolean(push?.autoUnregister);
|
|
1029
|
+
useEffect(() => {
|
|
1030
|
+
if (!pushToken || !pushAutoRegister) {
|
|
1031
|
+
return;
|
|
1032
|
+
}
|
|
1033
|
+
registerPushToken(pushToken, {
|
|
1034
|
+
platform: pushPlatform,
|
|
1035
|
+
deviceLabel: pushDeviceLabel,
|
|
1036
|
+
metadata: pushMetadataKey ? JSON.parse(pushMetadataKey) : void 0
|
|
1037
|
+
}).catch(() => void 0);
|
|
1038
|
+
if (!pushAutoUnregister) {
|
|
1039
|
+
return;
|
|
1040
|
+
}
|
|
1041
|
+
return () => {
|
|
1042
|
+
unregisterPushToken(pushToken).catch(() => void 0);
|
|
1043
|
+
};
|
|
1044
|
+
}, [
|
|
1045
|
+
pushAutoRegister,
|
|
1046
|
+
pushAutoUnregister,
|
|
1047
|
+
pushDeviceLabel,
|
|
1048
|
+
pushMetadataKey,
|
|
1049
|
+
pushPlatform,
|
|
1050
|
+
pushToken,
|
|
1051
|
+
registerPushToken,
|
|
1052
|
+
unregisterPushToken
|
|
1053
|
+
]);
|
|
1054
|
+
useEffect(() => {
|
|
1055
|
+
if (!realtimeSource) {
|
|
1056
|
+
return;
|
|
1057
|
+
}
|
|
1058
|
+
const client = realtimeSource.kind === "ws" ? createWidgetSocketClient(realtimeSource.url, publishableKey, realtimeLoadersRef) : createConvexClient(realtimeSource.url);
|
|
1059
|
+
realtimeClientRef.current = client;
|
|
1060
|
+
setRealtimeClient(client);
|
|
1061
|
+
return () => {
|
|
1062
|
+
realtimeClientRef.current = null;
|
|
1063
|
+
setRealtimeClient(null);
|
|
1064
|
+
void client.close();
|
|
1065
|
+
};
|
|
1066
|
+
}, [publishableKey, realtimeSource]);
|
|
1067
|
+
useEffect(() => {
|
|
1068
|
+
if (!realtimeClient) {
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
let disposed = false;
|
|
1072
|
+
let retryTimer = null;
|
|
1073
|
+
let firstResultTimer = null;
|
|
1074
|
+
let retryAttempt = 0;
|
|
1075
|
+
let unsubscribe = null;
|
|
1076
|
+
const clearSubscription = () => {
|
|
1077
|
+
if (typeof unsubscribe === "function") {
|
|
1078
|
+
unsubscribe();
|
|
1079
|
+
} else {
|
|
1080
|
+
unsubscribe?.unsubscribe?.();
|
|
1081
|
+
}
|
|
1082
|
+
unsubscribe = null;
|
|
1083
|
+
};
|
|
1084
|
+
function handleSubscriptionError(error, afterSessionRefresh) {
|
|
1085
|
+
clearSubscription();
|
|
1086
|
+
realtimeLiveRef.current = false;
|
|
1087
|
+
if (!afterSessionRefresh && canRefreshSession && /session|token|expired|invalid/i.test(error.message)) {
|
|
1088
|
+
subscribe(true).catch(() => void 0);
|
|
1089
|
+
return;
|
|
1090
|
+
}
|
|
1091
|
+
setLoading(false);
|
|
1092
|
+
setRefreshing(false);
|
|
1093
|
+
setConnectionState("polling");
|
|
1094
|
+
setRealtimeFailed(true);
|
|
1095
|
+
emitEvent({
|
|
1096
|
+
type: "realtime.error",
|
|
1097
|
+
projectId,
|
|
1098
|
+
subscriberId: subscriber.subscriberId,
|
|
1099
|
+
message: error.message
|
|
1100
|
+
});
|
|
1101
|
+
retryAttempt += 1;
|
|
1102
|
+
emitEvent({
|
|
1103
|
+
type: "realtime.retry.scheduled",
|
|
1104
|
+
projectId,
|
|
1105
|
+
subscriberId: subscriber.subscriberId,
|
|
1106
|
+
attempt: retryAttempt,
|
|
1107
|
+
retryInMs: REALTIME_RETRY_MS
|
|
1108
|
+
});
|
|
1109
|
+
retryTimer = setTimeout(() => {
|
|
1110
|
+
subscribe().catch(() => void 0);
|
|
1111
|
+
}, REALTIME_RETRY_MS);
|
|
1112
|
+
}
|
|
1113
|
+
async function subscribe(forceRefreshSession = false) {
|
|
1114
|
+
if (wantsItems && !hasLoadedOnceRef.current) {
|
|
1115
|
+
setLoading(true);
|
|
1116
|
+
}
|
|
1117
|
+
setConnectionState("connecting");
|
|
1118
|
+
emitEvent({
|
|
1119
|
+
type: "realtime.connecting",
|
|
1120
|
+
projectId,
|
|
1121
|
+
subscriberId: subscriber.subscriberId
|
|
1122
|
+
});
|
|
1123
|
+
try {
|
|
1124
|
+
const token = forceRefreshSession ? await refreshSession() : await ensureSession();
|
|
1125
|
+
if (disposed || !realtimeClientRef.current) {
|
|
1126
|
+
return;
|
|
608
1127
|
}
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
fetchJson,
|
|
614
|
-
onActionClick,
|
|
615
|
-
subscriber.subscriberId,
|
|
616
|
-
]);
|
|
617
|
-
const reconnect = useCallback(async () => {
|
|
618
|
-
emitEvent({
|
|
619
|
-
type: "realtime.reconnect.manual",
|
|
1128
|
+
clearSubscription();
|
|
1129
|
+
const subscription = realtimeClientRef.current.onUpdate(
|
|
1130
|
+
wantsItems ? "widgetInbox:list" : "widgetInbox:unreadCount",
|
|
1131
|
+
wantsItems ? {
|
|
620
1132
|
projectId,
|
|
1133
|
+
sessionToken: token,
|
|
621
1134
|
subscriberId: subscriber.subscriberId,
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
}, [
|
|
635
|
-
canRefreshSession,
|
|
636
|
-
emitEvent,
|
|
637
|
-
ensureSession,
|
|
638
|
-
projectId,
|
|
639
|
-
refreshSession,
|
|
640
|
-
subscriber.subscriberId,
|
|
641
|
-
]);
|
|
642
|
-
useEffect(() => {
|
|
643
|
-
let cancelled = false;
|
|
644
|
-
async function loadConfig() {
|
|
645
|
-
try {
|
|
646
|
-
const config = await fetchWidgetConfigDocument();
|
|
647
|
-
if (!cancelled && config) {
|
|
648
|
-
setRealtimeConvexUrl(config.convexUrl ?? null);
|
|
649
|
-
setServerConfig({ ...DEFAULT_CONFIG, ...config });
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
catch {
|
|
653
|
-
if (!cancelled) {
|
|
654
|
-
setRealtimeConvexUrl(null);
|
|
655
|
-
setServerConfig(DEFAULT_CONFIG);
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
finally {
|
|
659
|
-
if (!cancelled) {
|
|
660
|
-
setConfigResolved(true);
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
void loadConfig();
|
|
665
|
-
return () => {
|
|
666
|
-
cancelled = true;
|
|
667
|
-
};
|
|
668
|
-
}, [fetchWidgetConfigDocument]);
|
|
669
|
-
useEffect(() => {
|
|
670
|
-
if (!runtimeSessionToken || !runtimeSessionExpiresAt || !hasGetSessionToken) {
|
|
671
|
-
return;
|
|
672
|
-
}
|
|
673
|
-
const refreshInMs = Math.max(runtimeSessionExpiresAt - Date.now() - 60_000, 1_000);
|
|
674
|
-
const timer = setTimeout(() => {
|
|
675
|
-
refreshSession().catch(() => undefined);
|
|
676
|
-
}, refreshInMs);
|
|
677
|
-
return () => clearTimeout(timer);
|
|
678
|
-
}, [hasGetSessionToken, refreshSession, runtimeSessionExpiresAt, runtimeSessionToken]);
|
|
679
|
-
// Keyed by value so an inline `push={{ ... }}` doesn't re-register the device
|
|
680
|
-
// on every render.
|
|
681
|
-
const pushToken = push?.token;
|
|
682
|
-
const pushPlatform = push?.platform;
|
|
683
|
-
const pushDeviceLabel = push?.deviceLabel;
|
|
684
|
-
const pushMetadataKey = push?.metadata ? JSON.stringify(push.metadata) : "";
|
|
685
|
-
const pushAutoRegister = push?.autoRegister !== false;
|
|
686
|
-
const pushAutoUnregister = Boolean(push?.autoUnregister);
|
|
687
|
-
useEffect(() => {
|
|
688
|
-
if (!pushToken || !pushAutoRegister) {
|
|
689
|
-
return;
|
|
690
|
-
}
|
|
691
|
-
registerPushToken(pushToken, {
|
|
692
|
-
platform: pushPlatform,
|
|
693
|
-
deviceLabel: pushDeviceLabel,
|
|
694
|
-
metadata: pushMetadataKey ? JSON.parse(pushMetadataKey) : undefined,
|
|
695
|
-
}).catch(() => undefined);
|
|
696
|
-
if (!pushAutoUnregister) {
|
|
697
|
-
return;
|
|
698
|
-
}
|
|
699
|
-
return () => {
|
|
700
|
-
unregisterPushToken(pushToken).catch(() => undefined);
|
|
701
|
-
};
|
|
702
|
-
}, [
|
|
703
|
-
pushAutoRegister,
|
|
704
|
-
pushAutoUnregister,
|
|
705
|
-
pushDeviceLabel,
|
|
706
|
-
pushMetadataKey,
|
|
707
|
-
pushPlatform,
|
|
708
|
-
pushToken,
|
|
709
|
-
registerPushToken,
|
|
710
|
-
unregisterPushToken,
|
|
711
|
-
]);
|
|
712
|
-
useEffect(() => {
|
|
713
|
-
if (!realtimeConvexUrl) {
|
|
714
|
-
return;
|
|
715
|
-
}
|
|
716
|
-
const client = new ConvexClient(realtimeConvexUrl);
|
|
717
|
-
convexClientRef.current = client;
|
|
718
|
-
setConvexClient(client);
|
|
719
|
-
return () => {
|
|
720
|
-
convexClientRef.current = null;
|
|
721
|
-
setConvexClient(null);
|
|
722
|
-
if (typeof client.close === "function") {
|
|
723
|
-
client.close();
|
|
724
|
-
}
|
|
725
|
-
};
|
|
726
|
-
}, [realtimeConvexUrl]);
|
|
727
|
-
useEffect(() => {
|
|
728
|
-
if (!convexClient) {
|
|
729
|
-
return;
|
|
730
|
-
}
|
|
731
|
-
let disposed = false;
|
|
732
|
-
let retryTimer = null;
|
|
733
|
-
let firstResultTimer = null;
|
|
734
|
-
let retryAttempt = 0;
|
|
735
|
-
let unsubscribe = null;
|
|
736
|
-
const clearSubscription = () => {
|
|
737
|
-
if (typeof unsubscribe === "function") {
|
|
738
|
-
unsubscribe();
|
|
1135
|
+
limit: INBOX_PAGE_SIZE,
|
|
1136
|
+
...activeKinds.length ? { kinds: activeKinds } : {},
|
|
1137
|
+
...unreadKinds.length ? { unreadKinds } : {}
|
|
1138
|
+
} : {
|
|
1139
|
+
projectId,
|
|
1140
|
+
sessionToken: token,
|
|
1141
|
+
subscriberId: subscriber.subscriberId,
|
|
1142
|
+
...unreadKinds.length ? { kinds: unreadKinds } : {}
|
|
1143
|
+
},
|
|
1144
|
+
(result) => {
|
|
1145
|
+
if (disposed) {
|
|
1146
|
+
return;
|
|
739
1147
|
}
|
|
740
|
-
|
|
741
|
-
|
|
1148
|
+
if (firstResultTimer) {
|
|
1149
|
+
clearTimeout(firstResultTimer);
|
|
1150
|
+
firstResultTimer = null;
|
|
742
1151
|
}
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
realtimeLiveRef.current = false;
|
|
751
|
-
if (!afterSessionRefresh &&
|
|
752
|
-
canRefreshSession &&
|
|
753
|
-
/session|token|expired|invalid/i.test(error.message)) {
|
|
754
|
-
subscribe(true).catch(() => undefined);
|
|
755
|
-
return;
|
|
1152
|
+
realtimeLiveRef.current = true;
|
|
1153
|
+
setRealtimeFailed(false);
|
|
1154
|
+
if (wantsItems) {
|
|
1155
|
+
applyHead(result ?? {});
|
|
1156
|
+
setLoading(false);
|
|
1157
|
+
} else {
|
|
1158
|
+
applyUnread(result ?? {});
|
|
756
1159
|
}
|
|
757
|
-
setLoading(false);
|
|
758
1160
|
setRefreshing(false);
|
|
759
|
-
setConnectionState("
|
|
760
|
-
|
|
761
|
-
emitEvent({
|
|
762
|
-
type: "realtime.error",
|
|
763
|
-
projectId,
|
|
764
|
-
subscriberId: subscriber.subscriberId,
|
|
765
|
-
message: error.message,
|
|
766
|
-
});
|
|
767
|
-
retryAttempt += 1;
|
|
768
|
-
emitEvent({
|
|
769
|
-
type: "realtime.retry.scheduled",
|
|
770
|
-
projectId,
|
|
771
|
-
subscriberId: subscriber.subscriberId,
|
|
772
|
-
attempt: retryAttempt,
|
|
773
|
-
retryInMs: REALTIME_RETRY_MS,
|
|
774
|
-
});
|
|
775
|
-
retryTimer = setTimeout(() => {
|
|
776
|
-
subscribe().catch(() => undefined);
|
|
777
|
-
}, REALTIME_RETRY_MS);
|
|
778
|
-
}
|
|
779
|
-
async function subscribe(forceRefreshSession = false) {
|
|
780
|
-
if (!hasLoadedOnceRef.current) {
|
|
781
|
-
setLoading(true);
|
|
782
|
-
}
|
|
783
|
-
setConnectionState("connecting");
|
|
1161
|
+
setConnectionState("live");
|
|
1162
|
+
setErrorMessage(null);
|
|
784
1163
|
emitEvent({
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
1164
|
+
type: "realtime.live",
|
|
1165
|
+
projectId,
|
|
1166
|
+
subscriberId: subscriber.subscriberId
|
|
788
1167
|
});
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
if (disposed || !convexClientRef.current) {
|
|
792
|
-
return;
|
|
793
|
-
}
|
|
794
|
-
clearSubscription();
|
|
795
|
-
const subscription = convexClientRef.current.onUpdate("widgetInbox:list", {
|
|
796
|
-
projectId,
|
|
797
|
-
sessionToken: token,
|
|
798
|
-
subscriberId: subscriber.subscriberId,
|
|
799
|
-
limit: 50,
|
|
800
|
-
...(activeKinds.length ? { kinds: activeKinds } : {}),
|
|
801
|
-
...(unreadKinds.length ? { unreadKinds } : {}),
|
|
802
|
-
}, (inbox) => {
|
|
803
|
-
if (disposed) {
|
|
804
|
-
return;
|
|
805
|
-
}
|
|
806
|
-
if (firstResultTimer) {
|
|
807
|
-
clearTimeout(firstResultTimer);
|
|
808
|
-
firstResultTimer = null;
|
|
809
|
-
}
|
|
810
|
-
realtimeLiveRef.current = true;
|
|
811
|
-
setRealtimeFailed(false);
|
|
812
|
-
setItems((inbox?.items ?? []).map(normalizeInboxItem));
|
|
813
|
-
setUnreadCount(Number(inbox?.unreadCount ?? 0));
|
|
814
|
-
setUnreadByKind(inbox?.unreadByKind ?? {});
|
|
815
|
-
setLoading(false);
|
|
816
|
-
setRefreshing(false);
|
|
817
|
-
hasLoadedOnceRef.current = true;
|
|
818
|
-
setConnectionState("live");
|
|
819
|
-
setErrorMessage(null);
|
|
820
|
-
emitEvent({
|
|
821
|
-
type: "realtime.live",
|
|
822
|
-
projectId,
|
|
823
|
-
subscriberId: subscriber.subscriberId,
|
|
824
|
-
});
|
|
825
|
-
}, (error) => {
|
|
826
|
-
if (!disposed) {
|
|
827
|
-
handleSubscriptionError(error, forceRefreshSession);
|
|
828
|
-
}
|
|
829
|
-
});
|
|
830
|
-
unsubscribe = subscription;
|
|
831
|
-
retryAttempt = 0;
|
|
832
|
-
// Nothing fetches over HTTP while a subscription is expected to deliver,
|
|
833
|
-
// so if it stays silent (e.g. WebSockets blocked) let polling take over.
|
|
834
|
-
if (!realtimeLiveRef.current && !firstResultTimer) {
|
|
835
|
-
firstResultTimer = setTimeout(() => {
|
|
836
|
-
firstResultTimer = null;
|
|
837
|
-
if (!disposed && !realtimeLiveRef.current) {
|
|
838
|
-
setRealtimeFailed(true);
|
|
839
|
-
}
|
|
840
|
-
}, Math.max(pollIntervalMs, 5_000));
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
|
-
catch (error) {
|
|
844
|
-
if (disposed) {
|
|
845
|
-
return;
|
|
846
|
-
}
|
|
847
|
-
const message = error instanceof Error ? error.message : "Realtime connection failed";
|
|
848
|
-
if (!forceRefreshSession &&
|
|
849
|
-
canRefreshSession &&
|
|
850
|
-
/session|token|expired|invalid/i.test(message)) {
|
|
851
|
-
await subscribe(true);
|
|
852
|
-
return;
|
|
853
|
-
}
|
|
854
|
-
setLoading(false);
|
|
855
|
-
setRefreshing(false);
|
|
856
|
-
setConnectionState("error");
|
|
857
|
-
setErrorMessage(message);
|
|
858
|
-
emitEvent({
|
|
859
|
-
type: "realtime.error",
|
|
860
|
-
projectId,
|
|
861
|
-
subscriberId: subscriber.subscriberId,
|
|
862
|
-
message,
|
|
863
|
-
});
|
|
864
|
-
const backoffMs = Math.min(30_000, 1_000 * 2 ** retryAttempt);
|
|
865
|
-
retryAttempt += 1;
|
|
866
|
-
emitEvent({
|
|
867
|
-
type: "realtime.retry.scheduled",
|
|
868
|
-
projectId,
|
|
869
|
-
subscriberId: subscriber.subscriberId,
|
|
870
|
-
attempt: retryAttempt,
|
|
871
|
-
retryInMs: backoffMs,
|
|
872
|
-
});
|
|
873
|
-
retryTimer = setTimeout(() => {
|
|
874
|
-
subscribe().catch(() => undefined);
|
|
875
|
-
}, backoffMs);
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
subscribe().catch(() => {
|
|
1168
|
+
},
|
|
1169
|
+
(error) => {
|
|
879
1170
|
if (!disposed) {
|
|
880
|
-
|
|
881
|
-
setRefreshing(false);
|
|
882
|
-
setConnectionState("error");
|
|
883
|
-
setErrorMessage("Failed to start realtime updates");
|
|
884
|
-
emitEvent({
|
|
885
|
-
type: "realtime.error",
|
|
886
|
-
projectId,
|
|
887
|
-
subscriberId: subscriber.subscriberId,
|
|
888
|
-
message: "Failed to start realtime updates",
|
|
889
|
-
});
|
|
890
|
-
}
|
|
891
|
-
});
|
|
892
|
-
return () => {
|
|
893
|
-
disposed = true;
|
|
894
|
-
realtimeLiveRef.current = false;
|
|
895
|
-
if (retryTimer) {
|
|
896
|
-
clearTimeout(retryTimer);
|
|
897
|
-
}
|
|
898
|
-
if (firstResultTimer) {
|
|
899
|
-
clearTimeout(firstResultTimer);
|
|
1171
|
+
handleSubscriptionError(error, forceRefreshSession);
|
|
900
1172
|
}
|
|
901
|
-
|
|
902
|
-
|
|
1173
|
+
}
|
|
1174
|
+
);
|
|
1175
|
+
unsubscribe = subscription;
|
|
1176
|
+
retryAttempt = 0;
|
|
1177
|
+
if (!realtimeLiveRef.current && !firstResultTimer) {
|
|
1178
|
+
firstResultTimer = setTimeout(() => {
|
|
1179
|
+
firstResultTimer = null;
|
|
1180
|
+
if (!disposed && !realtimeLiveRef.current) {
|
|
1181
|
+
setRealtimeFailed(true);
|
|
903
1182
|
}
|
|
904
|
-
|
|
905
|
-
unsubscribe?.unsubscribe?.();
|
|
906
|
-
}
|
|
907
|
-
};
|
|
908
|
-
}, [
|
|
909
|
-
activeKinds,
|
|
910
|
-
canRefreshSession,
|
|
911
|
-
convexClient,
|
|
912
|
-
emitEvent,
|
|
913
|
-
ensureSession,
|
|
914
|
-
pollIntervalMs,
|
|
915
|
-
projectId,
|
|
916
|
-
reconnectNonce,
|
|
917
|
-
unreadKinds,
|
|
918
|
-
refreshSession,
|
|
919
|
-
subscriber.subscriberId,
|
|
920
|
-
]);
|
|
921
|
-
useEffect(() => {
|
|
922
|
-
// Until config settles we don't know whether a subscription will deliver,
|
|
923
|
-
// and while one does, polling would only re-fetch what it pushes.
|
|
924
|
-
if (!configResolved || (realtimeConvexUrl && !realtimeFailed)) {
|
|
925
|
-
return;
|
|
1183
|
+
}, Math.max(pollIntervalMs, 5e3));
|
|
926
1184
|
}
|
|
927
|
-
|
|
1185
|
+
} catch (error) {
|
|
1186
|
+
if (disposed) {
|
|
1187
|
+
return;
|
|
1188
|
+
}
|
|
1189
|
+
const message = error instanceof Error ? error.message : "Realtime connection failed";
|
|
1190
|
+
if (!forceRefreshSession && canRefreshSession && /session|token|expired|invalid/i.test(message)) {
|
|
1191
|
+
await subscribe(true);
|
|
1192
|
+
return;
|
|
1193
|
+
}
|
|
1194
|
+
setLoading(false);
|
|
1195
|
+
setRefreshing(false);
|
|
1196
|
+
setConnectionState("error");
|
|
1197
|
+
setErrorMessage(message);
|
|
928
1198
|
emitEvent({
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
1199
|
+
type: "realtime.error",
|
|
1200
|
+
projectId,
|
|
1201
|
+
subscriberId: subscriber.subscriberId,
|
|
1202
|
+
message
|
|
932
1203
|
});
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
1204
|
+
const backoffMs = Math.min(3e4, 1e3 * 2 ** retryAttempt);
|
|
1205
|
+
retryAttempt += 1;
|
|
1206
|
+
emitEvent({
|
|
1207
|
+
type: "realtime.retry.scheduled",
|
|
1208
|
+
projectId,
|
|
1209
|
+
subscriberId: subscriber.subscriberId,
|
|
1210
|
+
attempt: retryAttempt,
|
|
1211
|
+
retryInMs: backoffMs
|
|
1212
|
+
});
|
|
1213
|
+
retryTimer = setTimeout(() => {
|
|
1214
|
+
subscribe().catch(() => void 0);
|
|
1215
|
+
}, backoffMs);
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
subscribe().catch(() => {
|
|
1219
|
+
if (!disposed) {
|
|
1220
|
+
setLoading(false);
|
|
1221
|
+
setRefreshing(false);
|
|
1222
|
+
setConnectionState("error");
|
|
1223
|
+
setErrorMessage("Failed to start realtime updates");
|
|
1224
|
+
emitEvent({
|
|
1225
|
+
type: "realtime.error",
|
|
1226
|
+
projectId,
|
|
1227
|
+
subscriberId: subscriber.subscriberId,
|
|
1228
|
+
message: "Failed to start realtime updates"
|
|
1229
|
+
});
|
|
1230
|
+
}
|
|
1231
|
+
});
|
|
1232
|
+
return () => {
|
|
1233
|
+
disposed = true;
|
|
1234
|
+
realtimeLiveRef.current = false;
|
|
1235
|
+
if (retryTimer) {
|
|
1236
|
+
clearTimeout(retryTimer);
|
|
1237
|
+
}
|
|
1238
|
+
if (firstResultTimer) {
|
|
1239
|
+
clearTimeout(firstResultTimer);
|
|
1240
|
+
}
|
|
1241
|
+
if (typeof unsubscribe === "function") {
|
|
1242
|
+
unsubscribe();
|
|
1243
|
+
} else {
|
|
1244
|
+
unsubscribe?.unsubscribe?.();
|
|
1245
|
+
}
|
|
1246
|
+
};
|
|
1247
|
+
}, [
|
|
1248
|
+
activeKinds,
|
|
1249
|
+
applyHead,
|
|
1250
|
+
applyUnread,
|
|
1251
|
+
canRefreshSession,
|
|
1252
|
+
realtimeClient,
|
|
1253
|
+
emitEvent,
|
|
1254
|
+
ensureSession,
|
|
1255
|
+
pollIntervalMs,
|
|
1256
|
+
projectId,
|
|
1257
|
+
reconnectNonce,
|
|
1258
|
+
unreadKinds,
|
|
1259
|
+
refreshSession,
|
|
1260
|
+
subscriber.subscriberId,
|
|
1261
|
+
wantsItems
|
|
1262
|
+
]);
|
|
1263
|
+
useEffect(() => {
|
|
1264
|
+
if (!configResolved || realtimeSource && !realtimeFailed) {
|
|
1265
|
+
return;
|
|
1266
|
+
}
|
|
1267
|
+
setConnectionState("polling");
|
|
1268
|
+
emitEvent({
|
|
1269
|
+
type: "transport.polling",
|
|
1270
|
+
projectId,
|
|
1271
|
+
subscriberId: subscriber.subscriberId
|
|
1272
|
+
});
|
|
1273
|
+
let disposed = false;
|
|
1274
|
+
let timer = null;
|
|
1275
|
+
let failures = 0;
|
|
1276
|
+
const schedule = () => {
|
|
1277
|
+
if (timer) {
|
|
1278
|
+
clearTimeout(timer);
|
|
1279
|
+
timer = null;
|
|
1280
|
+
}
|
|
1281
|
+
if (disposed || isPageHidden()) {
|
|
1282
|
+
return;
|
|
1283
|
+
}
|
|
1284
|
+
const delayMs = Math.max(
|
|
985
1285
|
pollIntervalMs,
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
};
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
items,
|
|
1014
|
-
loading,
|
|
1015
|
-
refreshing,
|
|
1016
|
-
connectionState,
|
|
1017
|
-
errorMessage,
|
|
1018
|
-
refresh,
|
|
1019
|
-
reconnect,
|
|
1020
|
-
markRead,
|
|
1021
|
-
markAllRead,
|
|
1022
|
-
archive,
|
|
1023
|
-
unarchive,
|
|
1024
|
-
unreadByKind,
|
|
1025
|
-
tabs: stableTabs,
|
|
1026
|
-
activeTab,
|
|
1027
|
-
setActiveTab,
|
|
1028
|
-
tabUnreadCount,
|
|
1029
|
-
registerPushToken,
|
|
1030
|
-
unregisterPushToken,
|
|
1031
|
-
handleActionClick,
|
|
1032
|
-
effectiveConfig,
|
|
1033
|
-
triggerRef,
|
|
1034
|
-
}), [
|
|
1035
|
-
archive,
|
|
1036
|
-
unarchive,
|
|
1037
|
-
unreadByKind,
|
|
1038
|
-
stableTabs,
|
|
1039
|
-
activeTab,
|
|
1040
|
-
setActiveTab,
|
|
1041
|
-
tabUnreadCount,
|
|
1042
|
-
open,
|
|
1043
|
-
unreadCount,
|
|
1044
|
-
items,
|
|
1045
|
-
loading,
|
|
1046
|
-
refreshing,
|
|
1047
|
-
connectionState,
|
|
1048
|
-
errorMessage,
|
|
1049
|
-
refresh,
|
|
1050
|
-
reconnect,
|
|
1051
|
-
markRead,
|
|
1052
|
-
markAllRead,
|
|
1053
|
-
registerPushToken,
|
|
1054
|
-
unregisterPushToken,
|
|
1055
|
-
handleActionClick,
|
|
1056
|
-
effectiveConfig,
|
|
1057
|
-
]);
|
|
1058
|
-
return _jsx(Ctx.Provider, { value: value, children: children });
|
|
1059
|
-
}
|
|
1060
|
-
export function useBeeperInbox() {
|
|
1061
|
-
const ctx = useContext(Ctx);
|
|
1062
|
-
if (!ctx) {
|
|
1063
|
-
throw new Error("useBeeperInbox must be used inside BeeperInboxProvider");
|
|
1286
|
+
Math.min(pollIntervalMs * 2 ** failures, MAX_POLL_BACKOFF_MS)
|
|
1287
|
+
);
|
|
1288
|
+
timer = setTimeout(() => {
|
|
1289
|
+
timer = null;
|
|
1290
|
+
poll({ silent: true });
|
|
1291
|
+
}, delayMs);
|
|
1292
|
+
};
|
|
1293
|
+
const poll = (options) => {
|
|
1294
|
+
refresh(options).then(
|
|
1295
|
+
() => {
|
|
1296
|
+
failures = 0;
|
|
1297
|
+
},
|
|
1298
|
+
() => {
|
|
1299
|
+
failures += 1;
|
|
1300
|
+
}
|
|
1301
|
+
).finally(schedule);
|
|
1302
|
+
};
|
|
1303
|
+
const onVisibilityChange = () => {
|
|
1304
|
+
if (isPageHidden()) {
|
|
1305
|
+
schedule();
|
|
1306
|
+
} else {
|
|
1307
|
+
poll({ silent: true });
|
|
1308
|
+
}
|
|
1309
|
+
};
|
|
1310
|
+
poll();
|
|
1311
|
+
if (typeof document !== "undefined") {
|
|
1312
|
+
document.addEventListener("visibilitychange", onVisibilityChange);
|
|
1064
1313
|
}
|
|
1065
|
-
return
|
|
1314
|
+
return () => {
|
|
1315
|
+
disposed = true;
|
|
1316
|
+
if (timer) {
|
|
1317
|
+
clearTimeout(timer);
|
|
1318
|
+
}
|
|
1319
|
+
if (typeof document !== "undefined") {
|
|
1320
|
+
document.removeEventListener("visibilitychange", onVisibilityChange);
|
|
1321
|
+
}
|
|
1322
|
+
};
|
|
1323
|
+
}, [
|
|
1324
|
+
configResolved,
|
|
1325
|
+
emitEvent,
|
|
1326
|
+
pollIntervalMs,
|
|
1327
|
+
projectId,
|
|
1328
|
+
realtimeSource,
|
|
1329
|
+
realtimeFailed,
|
|
1330
|
+
refresh,
|
|
1331
|
+
subscriber.subscriberId
|
|
1332
|
+
]);
|
|
1333
|
+
const effectiveConfig = useMemo(() => {
|
|
1334
|
+
const merged = {
|
|
1335
|
+
...serverConfig,
|
|
1336
|
+
...customization
|
|
1337
|
+
};
|
|
1338
|
+
return {
|
|
1339
|
+
modeDefault: theme,
|
|
1340
|
+
accentColor: merged.accentColor ?? DEFAULT_CONFIG.accentColor,
|
|
1341
|
+
radius: merged.radius ?? DEFAULT_CONFIG.radius,
|
|
1342
|
+
fontScale: merged.fontScale ?? DEFAULT_CONFIG.fontScale,
|
|
1343
|
+
compact: merged.compact ?? DEFAULT_CONFIG.compact,
|
|
1344
|
+
showTopicTabs: merged.showTopicTabs ?? DEFAULT_CONFIG.showTopicTabs,
|
|
1345
|
+
showMarkAll: merged.showMarkAll ?? DEFAULT_CONFIG.showMarkAll,
|
|
1346
|
+
headerTitle: merged.headerTitle ?? DEFAULT_CONFIG.headerTitle,
|
|
1347
|
+
showDismiss: merged.showDismiss ?? DEFAULT_CONFIG.showDismiss
|
|
1348
|
+
};
|
|
1349
|
+
}, [customization, serverConfig, theme]);
|
|
1350
|
+
const value = useMemo(
|
|
1351
|
+
() => ({
|
|
1352
|
+
open,
|
|
1353
|
+
setOpen,
|
|
1354
|
+
unreadCount,
|
|
1355
|
+
items,
|
|
1356
|
+
loading,
|
|
1357
|
+
refreshing,
|
|
1358
|
+
connectionState,
|
|
1359
|
+
errorMessage,
|
|
1360
|
+
refresh,
|
|
1361
|
+
reconnect,
|
|
1362
|
+
markRead,
|
|
1363
|
+
markAllRead,
|
|
1364
|
+
archive,
|
|
1365
|
+
unarchive,
|
|
1366
|
+
unreadByKind,
|
|
1367
|
+
hasMore,
|
|
1368
|
+
loadingMore,
|
|
1369
|
+
loadMore,
|
|
1370
|
+
tabs: stableTabs,
|
|
1371
|
+
activeTab,
|
|
1372
|
+
setActiveTab,
|
|
1373
|
+
tabUnreadCount,
|
|
1374
|
+
registerPushToken,
|
|
1375
|
+
unregisterPushToken,
|
|
1376
|
+
handleActionClick,
|
|
1377
|
+
effectiveConfig,
|
|
1378
|
+
triggerRef
|
|
1379
|
+
}),
|
|
1380
|
+
[
|
|
1381
|
+
archive,
|
|
1382
|
+
unarchive,
|
|
1383
|
+
unreadByKind,
|
|
1384
|
+
hasMore,
|
|
1385
|
+
loadingMore,
|
|
1386
|
+
loadMore,
|
|
1387
|
+
stableTabs,
|
|
1388
|
+
activeTab,
|
|
1389
|
+
setActiveTab,
|
|
1390
|
+
tabUnreadCount,
|
|
1391
|
+
open,
|
|
1392
|
+
unreadCount,
|
|
1393
|
+
items,
|
|
1394
|
+
loading,
|
|
1395
|
+
refreshing,
|
|
1396
|
+
connectionState,
|
|
1397
|
+
errorMessage,
|
|
1398
|
+
refresh,
|
|
1399
|
+
reconnect,
|
|
1400
|
+
markRead,
|
|
1401
|
+
markAllRead,
|
|
1402
|
+
registerPushToken,
|
|
1403
|
+
unregisterPushToken,
|
|
1404
|
+
handleActionClick,
|
|
1405
|
+
effectiveConfig
|
|
1406
|
+
]
|
|
1407
|
+
);
|
|
1408
|
+
return /* @__PURE__ */ jsx(ItemsDemandCtx.Provider, { value: registerItemsDemand, children: /* @__PURE__ */ jsx(Ctx.Provider, { value, children }) });
|
|
1409
|
+
}
|
|
1410
|
+
function useInboxContext(hookName) {
|
|
1411
|
+
const ctx = useContext(Ctx);
|
|
1412
|
+
if (!ctx) {
|
|
1413
|
+
throw new Error(`${hookName} must be used inside BeeperInboxProvider`);
|
|
1414
|
+
}
|
|
1415
|
+
return ctx;
|
|
1416
|
+
}
|
|
1417
|
+
function useBeeperInbox() {
|
|
1418
|
+
const ctx = useInboxContext("useBeeperInbox");
|
|
1419
|
+
const registerItemsDemand = useContext(ItemsDemandCtx);
|
|
1420
|
+
useEffect(() => registerItemsDemand?.(), [registerItemsDemand]);
|
|
1421
|
+
return ctx;
|
|
1066
1422
|
}
|
|
1067
|
-
function
|
|
1068
|
-
|
|
1423
|
+
function useBeeperInboxUnreadCount() {
|
|
1424
|
+
const { unreadCount, unreadByKind, tabUnreadCount } = useInboxContext("useBeeperInboxUnreadCount");
|
|
1425
|
+
return { unreadCount, unreadByKind, tabUnreadCount };
|
|
1069
1426
|
}
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1427
|
+
function BellGlyph({
|
|
1428
|
+
size = 20,
|
|
1429
|
+
color = "currentColor"
|
|
1430
|
+
}) {
|
|
1431
|
+
return /* @__PURE__ */ jsxs(
|
|
1432
|
+
"svg",
|
|
1433
|
+
{
|
|
1434
|
+
width: size,
|
|
1435
|
+
height: size,
|
|
1436
|
+
viewBox: "0 0 24 24",
|
|
1437
|
+
fill: "none",
|
|
1438
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
1439
|
+
"aria-hidden": true,
|
|
1440
|
+
children: [
|
|
1441
|
+
/* @__PURE__ */ jsx(
|
|
1442
|
+
"path",
|
|
1443
|
+
{
|
|
1444
|
+
d: "M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9",
|
|
1445
|
+
stroke: color,
|
|
1446
|
+
strokeWidth: "2",
|
|
1447
|
+
strokeLinecap: "round",
|
|
1448
|
+
strokeLinejoin: "round"
|
|
1449
|
+
}
|
|
1450
|
+
),
|
|
1451
|
+
/* @__PURE__ */ jsx(
|
|
1452
|
+
"path",
|
|
1453
|
+
{
|
|
1454
|
+
d: "M13.73 21a2 2 0 0 1-3.46 0",
|
|
1455
|
+
stroke: color,
|
|
1456
|
+
strokeWidth: "2",
|
|
1457
|
+
strokeLinecap: "round",
|
|
1458
|
+
strokeLinejoin: "round"
|
|
1459
|
+
}
|
|
1460
|
+
)
|
|
1461
|
+
]
|
|
1084
1462
|
}
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1463
|
+
);
|
|
1464
|
+
}
|
|
1465
|
+
function BeeperInboxBell({
|
|
1466
|
+
variant = "bell",
|
|
1467
|
+
label = "Inbox",
|
|
1468
|
+
className,
|
|
1469
|
+
style,
|
|
1470
|
+
"aria-label": ariaLabel = "Open notifications",
|
|
1471
|
+
renderTrigger
|
|
1472
|
+
}) {
|
|
1473
|
+
const { open, setOpen, unreadCount, effectiveConfig, triggerRef } = useInboxContext("BeeperInboxBell");
|
|
1474
|
+
const toggle = () => setOpen(!open);
|
|
1475
|
+
const fs = effectiveConfig.fontScale;
|
|
1476
|
+
const triggerAriaLabel = unreadCount > 0 ? `${ariaLabel}, ${unreadCount} unread` : ariaLabel;
|
|
1477
|
+
if (renderTrigger) {
|
|
1478
|
+
return /* @__PURE__ */ jsx(Fragment, { children: renderTrigger({
|
|
1479
|
+
unreadCount,
|
|
1480
|
+
open,
|
|
1481
|
+
toggle,
|
|
1482
|
+
accentColor: effectiveConfig.accentColor,
|
|
1483
|
+
fontScale: fs,
|
|
1484
|
+
triggerRef
|
|
1485
|
+
}) });
|
|
1486
|
+
}
|
|
1487
|
+
if (variant === "minimal") {
|
|
1488
|
+
return /* @__PURE__ */ jsxs(
|
|
1489
|
+
"button",
|
|
1490
|
+
{
|
|
1491
|
+
type: "button",
|
|
1492
|
+
ref: (el) => {
|
|
1493
|
+
triggerRef.current = el;
|
|
1494
|
+
},
|
|
1495
|
+
className,
|
|
1496
|
+
"aria-label": triggerAriaLabel,
|
|
1497
|
+
"aria-expanded": open,
|
|
1498
|
+
"aria-controls": "beeper-inbox-panel",
|
|
1499
|
+
onClick: toggle,
|
|
1500
|
+
style: {
|
|
1501
|
+
borderRadius: 999,
|
|
1502
|
+
border: "1px solid #d1d5db",
|
|
1503
|
+
padding: "8px 12px",
|
|
1504
|
+
background: "white",
|
|
1505
|
+
display: "inline-flex",
|
|
1506
|
+
alignItems: "center",
|
|
1507
|
+
gap: 8,
|
|
1508
|
+
fontSize: `${14 * fs}px`,
|
|
1509
|
+
cursor: "pointer",
|
|
1510
|
+
...style
|
|
1511
|
+
},
|
|
1512
|
+
children: [
|
|
1513
|
+
/* @__PURE__ */ jsx("span", { children: label }),
|
|
1514
|
+
/* @__PURE__ */ jsx(
|
|
1515
|
+
"span",
|
|
1516
|
+
{
|
|
1517
|
+
style: {
|
|
1518
|
+
background: effectiveConfig.accentColor,
|
|
1519
|
+
color: "white",
|
|
1089
1520
|
borderRadius: 999,
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1521
|
+
padding: "2px 8px",
|
|
1522
|
+
fontSize: 12,
|
|
1523
|
+
minWidth: "1.25rem",
|
|
1524
|
+
minHeight: "1.25rem",
|
|
1525
|
+
textAlign: "center"
|
|
1526
|
+
},
|
|
1527
|
+
children: unreadCount
|
|
1528
|
+
}
|
|
1529
|
+
)
|
|
1530
|
+
]
|
|
1531
|
+
}
|
|
1532
|
+
);
|
|
1533
|
+
}
|
|
1534
|
+
const useHostStyles = !className;
|
|
1535
|
+
return /* @__PURE__ */ jsxs(
|
|
1536
|
+
"button",
|
|
1537
|
+
{
|
|
1538
|
+
type: "button",
|
|
1539
|
+
ref: (el) => {
|
|
1540
|
+
triggerRef.current = el;
|
|
1541
|
+
},
|
|
1542
|
+
className,
|
|
1543
|
+
"aria-label": triggerAriaLabel,
|
|
1544
|
+
"aria-expanded": open,
|
|
1545
|
+
"aria-controls": "beeper-inbox-panel",
|
|
1546
|
+
onClick: toggle,
|
|
1547
|
+
style: {
|
|
1548
|
+
position: "relative",
|
|
1549
|
+
display: "inline-flex",
|
|
1550
|
+
alignItems: "center",
|
|
1551
|
+
justifyContent: "center",
|
|
1552
|
+
...useHostStyles ? {
|
|
1553
|
+
width: `${44 * fs}px`,
|
|
1554
|
+
height: `${44 * fs}px`,
|
|
1555
|
+
borderRadius: 999,
|
|
1556
|
+
border: "1px solid #d1d5db",
|
|
1557
|
+
background: "white",
|
|
1558
|
+
color: "#374151"
|
|
1559
|
+
} : {},
|
|
1560
|
+
cursor: "pointer",
|
|
1561
|
+
padding: 0,
|
|
1562
|
+
...style
|
|
1563
|
+
},
|
|
1564
|
+
children: [
|
|
1565
|
+
/* @__PURE__ */ jsx(BellGlyph, { size: Math.round(20 * fs) }),
|
|
1566
|
+
unreadCount > 0 ? /* @__PURE__ */ jsx(
|
|
1567
|
+
"span",
|
|
1568
|
+
{
|
|
1569
|
+
style: {
|
|
1570
|
+
position: "absolute",
|
|
1571
|
+
top: 2,
|
|
1572
|
+
right: 2,
|
|
1573
|
+
minWidth: "1rem",
|
|
1574
|
+
height: "1rem",
|
|
1575
|
+
padding: unreadCount > 9 ? "0 4px" : 0,
|
|
1576
|
+
borderRadius: 999,
|
|
1577
|
+
background: effectiveConfig.accentColor,
|
|
1578
|
+
color: "white",
|
|
1579
|
+
fontSize: 10,
|
|
1580
|
+
fontWeight: 600,
|
|
1581
|
+
display: "flex",
|
|
1582
|
+
alignItems: "center",
|
|
1583
|
+
justifyContent: "center",
|
|
1584
|
+
lineHeight: 1
|
|
1585
|
+
},
|
|
1586
|
+
children: unreadCount > 99 ? "99+" : unreadCount
|
|
1587
|
+
}
|
|
1588
|
+
) : null
|
|
1589
|
+
]
|
|
1109
1590
|
}
|
|
1110
|
-
|
|
1111
|
-
return (_jsxs("button", { type: "button", ref: (el) => {
|
|
1112
|
-
triggerRef.current = el;
|
|
1113
|
-
}, className: className, "aria-label": triggerAriaLabel, "aria-expanded": open, "aria-controls": "beeper-inbox-panel", onClick: toggle, style: {
|
|
1114
|
-
position: "relative",
|
|
1115
|
-
display: "inline-flex",
|
|
1116
|
-
alignItems: "center",
|
|
1117
|
-
justifyContent: "center",
|
|
1118
|
-
...(useHostStyles
|
|
1119
|
-
? {
|
|
1120
|
-
width: `${44 * fs}px`,
|
|
1121
|
-
height: `${44 * fs}px`,
|
|
1122
|
-
borderRadius: 999,
|
|
1123
|
-
border: "1px solid #d1d5db",
|
|
1124
|
-
background: "white",
|
|
1125
|
-
color: "#374151",
|
|
1126
|
-
}
|
|
1127
|
-
: {}),
|
|
1128
|
-
cursor: "pointer",
|
|
1129
|
-
padding: 0,
|
|
1130
|
-
...style,
|
|
1131
|
-
}, children: [_jsx(BellGlyph, { size: Math.round(20 * fs) }), unreadCount > 0 ? (_jsx("span", { style: {
|
|
1132
|
-
position: "absolute",
|
|
1133
|
-
top: 2,
|
|
1134
|
-
right: 2,
|
|
1135
|
-
minWidth: "1rem",
|
|
1136
|
-
height: "1rem",
|
|
1137
|
-
padding: unreadCount > 9 ? "0 4px" : 0,
|
|
1138
|
-
borderRadius: 999,
|
|
1139
|
-
background: effectiveConfig.accentColor,
|
|
1140
|
-
color: "white",
|
|
1141
|
-
fontSize: 10,
|
|
1142
|
-
fontWeight: 600,
|
|
1143
|
-
display: "flex",
|
|
1144
|
-
alignItems: "center",
|
|
1145
|
-
justifyContent: "center",
|
|
1146
|
-
lineHeight: 1,
|
|
1147
|
-
}, children: unreadCount > 99 ? "99+" : unreadCount })) : null] }));
|
|
1591
|
+
);
|
|
1148
1592
|
}
|
|
1149
1593
|
function ShadowContainer({ children }) {
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1594
|
+
const hostRef = useRef(null);
|
|
1595
|
+
const [portalTarget, setPortalTarget] = useState(null);
|
|
1596
|
+
useLayoutEffect(() => {
|
|
1597
|
+
const el = hostRef.current;
|
|
1598
|
+
if (!el) {
|
|
1599
|
+
return;
|
|
1600
|
+
}
|
|
1601
|
+
let root = el.shadowRoot;
|
|
1602
|
+
if (!root) {
|
|
1603
|
+
root = el.attachShadow({ mode: "open" });
|
|
1604
|
+
}
|
|
1605
|
+
setPortalTarget(root);
|
|
1606
|
+
return () => {
|
|
1607
|
+
setPortalTarget(null);
|
|
1608
|
+
};
|
|
1609
|
+
}, []);
|
|
1610
|
+
return /* @__PURE__ */ jsx("div", { ref: hostRef, children: portalTarget ? createPortal(
|
|
1611
|
+
/* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1612
|
+
/* @__PURE__ */ jsx("style", { children: `
|
|
1168
1613
|
:host { all: initial; display: block; }
|
|
1169
1614
|
.bw-root { font-family: Manrope, ui-sans-serif, system-ui, sans-serif; width: 100%; max-width: 100%; box-sizing: border-box; }
|
|
1170
|
-
` }),
|
|
1171
|
-
|
|
1615
|
+
` }),
|
|
1616
|
+
children
|
|
1617
|
+
] }),
|
|
1618
|
+
portalTarget
|
|
1619
|
+
) : null });
|
|
1172
1620
|
}
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
return () => {
|
|
1206
|
-
document.removeEventListener("pointerdown", handleInteractOutside, true);
|
|
1207
|
-
document.removeEventListener("focusin", handleInteractOutside, true);
|
|
1208
|
-
};
|
|
1209
|
-
}, [open, setOpen, triggerRef]);
|
|
1210
|
-
useEffect(() => {
|
|
1211
|
-
if (!open) {
|
|
1212
|
-
return;
|
|
1213
|
-
}
|
|
1214
|
-
const trigger = triggerRef.current;
|
|
1215
|
-
const frame = requestAnimationFrame(() => panelRef.current?.focus());
|
|
1216
|
-
const handleKeyDown = (event) => {
|
|
1217
|
-
if (event.key === "Escape") {
|
|
1218
|
-
event.preventDefault();
|
|
1219
|
-
setOpen(false);
|
|
1220
|
-
}
|
|
1221
|
-
};
|
|
1222
|
-
document.addEventListener("keydown", handleKeyDown);
|
|
1223
|
-
return () => {
|
|
1224
|
-
cancelAnimationFrame(frame);
|
|
1225
|
-
document.removeEventListener("keydown", handleKeyDown);
|
|
1226
|
-
const active = document.activeElement;
|
|
1227
|
-
if (trigger && (active === panelRef.current || panelRef.current?.contains(active))) {
|
|
1228
|
-
trigger.focus();
|
|
1229
|
-
}
|
|
1230
|
-
};
|
|
1231
|
-
}, [open, setOpen, triggerRef]);
|
|
1232
|
-
useLayoutEffect(() => {
|
|
1233
|
-
if (!open) {
|
|
1234
|
-
return;
|
|
1235
|
-
}
|
|
1236
|
-
const gap = 10;
|
|
1237
|
-
const chrome = 168;
|
|
1238
|
-
const compute = () => {
|
|
1239
|
-
const w = window.innerWidth;
|
|
1240
|
-
const h = window.innerHeight;
|
|
1241
|
-
if (placement === "fixed-bottom-end") {
|
|
1242
|
-
const top = Math.max(8, h - PANEL_ESTIMATE_HEIGHT - 16);
|
|
1243
|
-
setLayout({
|
|
1244
|
-
top,
|
|
1245
|
-
right: 16,
|
|
1246
|
-
maxListHeight: Math.max(180, Math.min(400, h - top - chrome)),
|
|
1247
|
-
});
|
|
1248
|
-
return;
|
|
1249
|
-
}
|
|
1250
|
-
const el = triggerRef.current;
|
|
1251
|
-
if (!el) {
|
|
1252
|
-
setLayout({
|
|
1253
|
-
top: gap,
|
|
1254
|
-
right: gap,
|
|
1255
|
-
maxListHeight: Math.max(180, Math.min(400, h - gap - chrome)),
|
|
1256
|
-
});
|
|
1257
|
-
return;
|
|
1258
|
-
}
|
|
1259
|
-
const r = el.getBoundingClientRect();
|
|
1260
|
-
let top = r.bottom + gap;
|
|
1261
|
-
let right = w - r.right;
|
|
1262
|
-
right = Math.max(8, Math.min(right, w - PANEL_WIDTH - 8));
|
|
1263
|
-
if (top + PANEL_ESTIMATE_HEIGHT > h - 8) {
|
|
1264
|
-
const above = r.top - PANEL_ESTIMATE_HEIGHT - gap;
|
|
1265
|
-
top = above >= 8 ? above : Math.max(8, h - PANEL_ESTIMATE_HEIGHT - 8);
|
|
1266
|
-
}
|
|
1267
|
-
const maxList = Math.max(180, Math.min(440, h - top - chrome));
|
|
1268
|
-
setLayout({ top, right, maxListHeight: maxList });
|
|
1269
|
-
};
|
|
1270
|
-
compute();
|
|
1271
|
-
const ro = new ResizeObserver(compute);
|
|
1272
|
-
if (triggerRef.current) {
|
|
1273
|
-
ro.observe(triggerRef.current);
|
|
1274
|
-
}
|
|
1275
|
-
window.addEventListener("resize", compute);
|
|
1276
|
-
window.addEventListener("scroll", compute, true);
|
|
1277
|
-
return () => {
|
|
1278
|
-
ro.disconnect();
|
|
1279
|
-
window.removeEventListener("resize", compute);
|
|
1280
|
-
window.removeEventListener("scroll", compute, true);
|
|
1281
|
-
};
|
|
1282
|
-
}, [open, placement, triggerRef]);
|
|
1621
|
+
var PANEL_WIDTH = 384;
|
|
1622
|
+
var PANEL_ESTIMATE_HEIGHT = 440;
|
|
1623
|
+
function BeeperInboxPanel({
|
|
1624
|
+
placement = "anchor"
|
|
1625
|
+
} = {}) {
|
|
1626
|
+
const {
|
|
1627
|
+
open,
|
|
1628
|
+
setOpen,
|
|
1629
|
+
items,
|
|
1630
|
+
loading,
|
|
1631
|
+
refreshing,
|
|
1632
|
+
connectionState,
|
|
1633
|
+
errorMessage,
|
|
1634
|
+
refresh,
|
|
1635
|
+
reconnect,
|
|
1636
|
+
markRead,
|
|
1637
|
+
markAllRead,
|
|
1638
|
+
archive,
|
|
1639
|
+
tabs,
|
|
1640
|
+
activeTab,
|
|
1641
|
+
setActiveTab,
|
|
1642
|
+
tabUnreadCount,
|
|
1643
|
+
handleActionClick,
|
|
1644
|
+
effectiveConfig,
|
|
1645
|
+
triggerRef,
|
|
1646
|
+
hasMore,
|
|
1647
|
+
loadingMore,
|
|
1648
|
+
loadMore
|
|
1649
|
+
} = useInboxContext("BeeperInboxPanel");
|
|
1650
|
+
const [layout, setLayout] = useState({ top: 16, right: 16, maxListHeight: 320 });
|
|
1651
|
+
const panelRef = useRef(null);
|
|
1652
|
+
useEffect(() => {
|
|
1283
1653
|
if (!open) {
|
|
1284
|
-
|
|
1654
|
+
return;
|
|
1285
1655
|
}
|
|
1286
|
-
const
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1656
|
+
const isInsideNode = (event, node) => {
|
|
1657
|
+
if (!node) {
|
|
1658
|
+
return false;
|
|
1659
|
+
}
|
|
1660
|
+
const path = typeof event.composedPath === "function" ? event.composedPath() : void 0;
|
|
1661
|
+
if (path && path.includes(node)) {
|
|
1662
|
+
return true;
|
|
1663
|
+
}
|
|
1664
|
+
const target = event.target;
|
|
1665
|
+
return target instanceof Node ? node.contains(target) : false;
|
|
1296
1666
|
};
|
|
1297
|
-
const
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1667
|
+
const handleInteractOutside = (event) => {
|
|
1668
|
+
if (isInsideNode(event, panelRef.current)) {
|
|
1669
|
+
return;
|
|
1670
|
+
}
|
|
1671
|
+
if (isInsideNode(event, triggerRef.current)) {
|
|
1672
|
+
return;
|
|
1673
|
+
}
|
|
1674
|
+
setOpen(false);
|
|
1675
|
+
};
|
|
1676
|
+
document.addEventListener("pointerdown", handleInteractOutside, true);
|
|
1677
|
+
document.addEventListener("focusin", handleInteractOutside, true);
|
|
1678
|
+
return () => {
|
|
1679
|
+
document.removeEventListener("pointerdown", handleInteractOutside, true);
|
|
1680
|
+
document.removeEventListener("focusin", handleInteractOutside, true);
|
|
1681
|
+
};
|
|
1682
|
+
}, [open, setOpen, triggerRef]);
|
|
1683
|
+
useEffect(() => {
|
|
1684
|
+
if (!open) {
|
|
1685
|
+
return;
|
|
1686
|
+
}
|
|
1687
|
+
const trigger = triggerRef.current;
|
|
1688
|
+
const frame = requestAnimationFrame(() => panelRef.current?.focus());
|
|
1689
|
+
const handleKeyDown = (event) => {
|
|
1690
|
+
if (event.key === "Escape") {
|
|
1691
|
+
event.preventDefault();
|
|
1692
|
+
setOpen(false);
|
|
1693
|
+
}
|
|
1324
1694
|
};
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1695
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
1696
|
+
return () => {
|
|
1697
|
+
cancelAnimationFrame(frame);
|
|
1698
|
+
document.removeEventListener("keydown", handleKeyDown);
|
|
1699
|
+
const active = document.activeElement;
|
|
1700
|
+
if (trigger && (active === panelRef.current || panelRef.current?.contains(active))) {
|
|
1701
|
+
trigger.focus();
|
|
1702
|
+
}
|
|
1703
|
+
};
|
|
1704
|
+
}, [open, setOpen, triggerRef]);
|
|
1705
|
+
useLayoutEffect(() => {
|
|
1706
|
+
if (!open) {
|
|
1707
|
+
return;
|
|
1708
|
+
}
|
|
1709
|
+
const gap = 10;
|
|
1710
|
+
const chrome = 168;
|
|
1711
|
+
const compute = () => {
|
|
1712
|
+
const w = window.innerWidth;
|
|
1713
|
+
const h = window.innerHeight;
|
|
1714
|
+
if (placement === "fixed-bottom-end") {
|
|
1715
|
+
const top2 = Math.max(8, h - PANEL_ESTIMATE_HEIGHT - 16);
|
|
1716
|
+
setLayout({
|
|
1717
|
+
top: top2,
|
|
1718
|
+
right: 16,
|
|
1719
|
+
maxListHeight: Math.max(180, Math.min(400, h - top2 - chrome))
|
|
1720
|
+
});
|
|
1721
|
+
return;
|
|
1722
|
+
}
|
|
1723
|
+
const el = triggerRef.current;
|
|
1724
|
+
if (!el) {
|
|
1725
|
+
setLayout({
|
|
1726
|
+
top: gap,
|
|
1727
|
+
right: gap,
|
|
1728
|
+
maxListHeight: Math.max(180, Math.min(400, h - gap - chrome))
|
|
1729
|
+
});
|
|
1730
|
+
return;
|
|
1731
|
+
}
|
|
1732
|
+
const r = el.getBoundingClientRect();
|
|
1733
|
+
let top = r.bottom + gap;
|
|
1734
|
+
let right = w - r.right;
|
|
1735
|
+
right = Math.max(8, Math.min(right, w - PANEL_WIDTH - 8));
|
|
1736
|
+
if (top + PANEL_ESTIMATE_HEIGHT > h - 8) {
|
|
1737
|
+
const above = r.top - PANEL_ESTIMATE_HEIGHT - gap;
|
|
1738
|
+
top = above >= 8 ? above : Math.max(8, h - PANEL_ESTIMATE_HEIGHT - 8);
|
|
1739
|
+
}
|
|
1740
|
+
const maxList = Math.max(180, Math.min(440, h - top - chrome));
|
|
1741
|
+
setLayout({ top, right, maxListHeight: maxList });
|
|
1742
|
+
};
|
|
1743
|
+
compute();
|
|
1744
|
+
const ro = new ResizeObserver(compute);
|
|
1745
|
+
if (triggerRef.current) {
|
|
1746
|
+
ro.observe(triggerRef.current);
|
|
1747
|
+
}
|
|
1748
|
+
window.addEventListener("resize", compute);
|
|
1749
|
+
window.addEventListener("scroll", compute, true);
|
|
1750
|
+
return () => {
|
|
1751
|
+
ro.disconnect();
|
|
1752
|
+
window.removeEventListener("resize", compute);
|
|
1753
|
+
window.removeEventListener("scroll", compute, true);
|
|
1754
|
+
};
|
|
1755
|
+
}, [open, placement, triggerRef]);
|
|
1756
|
+
if (!open) {
|
|
1757
|
+
return null;
|
|
1758
|
+
}
|
|
1759
|
+
const mode = resolveTheme(effectiveConfig.modeDefault);
|
|
1760
|
+
const dark = mode === "dark";
|
|
1761
|
+
const light = {
|
|
1762
|
+
bg: "#fffaf5",
|
|
1763
|
+
fg: "#2b221d",
|
|
1764
|
+
muted: "#7d685d",
|
|
1765
|
+
border: "#eadbcc",
|
|
1766
|
+
surface: "#fffdf9",
|
|
1767
|
+
itemBg: "#fffcf8",
|
|
1768
|
+
shadow: "0 24px 54px rgba(38, 24, 12, 0.14)"
|
|
1769
|
+
};
|
|
1770
|
+
const toolbarBtn = dark ? {
|
|
1771
|
+
border: "1px solid #3a4250",
|
|
1772
|
+
background: "rgba(255,255,255,0.02)",
|
|
1773
|
+
color: "#e5e7eb",
|
|
1774
|
+
borderRadius: effectiveConfig.radius - 4,
|
|
1775
|
+
padding: "6px 10px",
|
|
1776
|
+
fontSize: "0.85em",
|
|
1777
|
+
cursor: "pointer"
|
|
1778
|
+
} : {
|
|
1779
|
+
border: `1px solid ${light.border}`,
|
|
1780
|
+
background: light.surface,
|
|
1781
|
+
color: light.fg,
|
|
1782
|
+
borderRadius: effectiveConfig.radius - 4,
|
|
1783
|
+
padding: "6px 10px",
|
|
1784
|
+
fontSize: "0.85em",
|
|
1785
|
+
cursor: "pointer"
|
|
1786
|
+
};
|
|
1787
|
+
const linkBtn = {
|
|
1788
|
+
border: "none",
|
|
1789
|
+
background: "transparent",
|
|
1790
|
+
color: effectiveConfig.accentColor,
|
|
1791
|
+
cursor: "pointer",
|
|
1792
|
+
fontSize: "0.88em",
|
|
1793
|
+
fontWeight: 600,
|
|
1794
|
+
padding: "4px 0"
|
|
1795
|
+
};
|
|
1796
|
+
return /* @__PURE__ */ jsx(
|
|
1797
|
+
"div",
|
|
1798
|
+
{
|
|
1799
|
+
style: {
|
|
1800
|
+
position: "fixed",
|
|
1801
|
+
zIndex: 9999,
|
|
1802
|
+
top: layout.top,
|
|
1803
|
+
right: layout.right,
|
|
1804
|
+
width: PANEL_WIDTH,
|
|
1805
|
+
maxWidth: `calc(100vw - 16px)`,
|
|
1806
|
+
pointerEvents: "none"
|
|
1807
|
+
},
|
|
1808
|
+
children: /* @__PURE__ */ jsx("div", { style: { pointerEvents: "auto" }, children: /* @__PURE__ */ jsx(ShadowContainer, { children: /* @__PURE__ */ jsxs(
|
|
1809
|
+
"div",
|
|
1810
|
+
{
|
|
1811
|
+
ref: panelRef,
|
|
1812
|
+
id: "beeper-inbox-panel",
|
|
1813
|
+
role: "dialog",
|
|
1814
|
+
"aria-modal": "false",
|
|
1815
|
+
"aria-labelledby": "beeper-inbox-title",
|
|
1816
|
+
tabIndex: -1,
|
|
1817
|
+
className: "bw-root",
|
|
1818
|
+
style: {
|
|
1819
|
+
width: "100%",
|
|
1820
|
+
boxSizing: "border-box",
|
|
1821
|
+
background: dark ? "#121418" : light.bg,
|
|
1822
|
+
color: dark ? "#f9fafb" : light.fg,
|
|
1823
|
+
border: dark ? "1px solid #2b313b" : `1px solid ${light.border}`,
|
|
1824
|
+
borderRadius: effectiveConfig.radius,
|
|
1825
|
+
boxShadow: dark ? "0 16px 30px rgba(0,0,0,.45)" : light.shadow,
|
|
1826
|
+
padding: effectiveConfig.compact ? 10 : 14,
|
|
1827
|
+
fontSize: `${14 * effectiveConfig.fontScale}px`
|
|
1828
|
+
},
|
|
1829
|
+
children: [
|
|
1830
|
+
/* @__PURE__ */ jsxs(
|
|
1831
|
+
"div",
|
|
1832
|
+
{
|
|
1833
|
+
style: {
|
|
1834
|
+
display: "flex",
|
|
1835
|
+
justifyContent: "space-between",
|
|
1836
|
+
alignItems: "center",
|
|
1837
|
+
gap: 8
|
|
1838
|
+
},
|
|
1839
|
+
children: [
|
|
1840
|
+
/* @__PURE__ */ jsx("strong", { id: "beeper-inbox-title", style: { fontSize: "1.04em", letterSpacing: "-0.03em", fontWeight: 700 }, children: effectiveConfig.headerTitle }),
|
|
1841
|
+
/* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: 6, flexShrink: 0, alignItems: "center" }, children: [
|
|
1842
|
+
refreshing ? /* @__PURE__ */ jsx("small", { role: "status", style: { opacity: 0.65, whiteSpace: "nowrap" }, children: "Refreshing..." }) : null,
|
|
1843
|
+
/* @__PURE__ */ jsx("button", { type: "button", style: toolbarBtn, onClick: () => refresh({ silent: true }), children: "Refresh" }),
|
|
1844
|
+
/* @__PURE__ */ jsx("button", { type: "button", style: toolbarBtn, onClick: () => setOpen(false), children: "Close" })
|
|
1845
|
+
] })
|
|
1846
|
+
]
|
|
1847
|
+
}
|
|
1848
|
+
),
|
|
1849
|
+
effectiveConfig.showTopicTabs && tabs.length > 0 ? /* @__PURE__ */ jsx(
|
|
1850
|
+
"div",
|
|
1851
|
+
{
|
|
1852
|
+
role: "tablist",
|
|
1853
|
+
"aria-label": "Notification categories",
|
|
1854
|
+
style: {
|
|
1855
|
+
display: "flex",
|
|
1856
|
+
gap: 4,
|
|
1857
|
+
marginTop: 10,
|
|
1858
|
+
overflowX: "auto",
|
|
1859
|
+
borderBottom: dark ? "1px solid #2b313b" : `1px solid ${light.border}`
|
|
1860
|
+
},
|
|
1861
|
+
children: tabs.map((tab) => {
|
|
1862
|
+
const selected = tab.key === activeTab;
|
|
1863
|
+
const count = tabUnreadCount(tab);
|
|
1864
|
+
return /* @__PURE__ */ jsxs(
|
|
1865
|
+
"button",
|
|
1866
|
+
{
|
|
1867
|
+
type: "button",
|
|
1868
|
+
role: "tab",
|
|
1869
|
+
"aria-selected": selected,
|
|
1870
|
+
onClick: () => setActiveTab(tab.key),
|
|
1871
|
+
style: {
|
|
1872
|
+
display: "flex",
|
|
1873
|
+
alignItems: "center",
|
|
1874
|
+
gap: 6,
|
|
1875
|
+
border: "none",
|
|
1876
|
+
borderBottom: `2px solid ${selected ? effectiveConfig.accentColor : "transparent"}`,
|
|
1877
|
+
background: "transparent",
|
|
1878
|
+
color: selected ? dark ? "#f9fafb" : light.fg : dark ? "#c7b6a7" : light.muted,
|
|
1879
|
+
cursor: "pointer",
|
|
1880
|
+
fontSize: "0.9em",
|
|
1881
|
+
fontWeight: selected ? 700 : 500,
|
|
1882
|
+
padding: "6px 8px",
|
|
1883
|
+
whiteSpace: "nowrap"
|
|
1884
|
+
},
|
|
1885
|
+
children: [
|
|
1886
|
+
tab.label,
|
|
1887
|
+
count > 0 ? /* @__PURE__ */ jsx(
|
|
1888
|
+
"span",
|
|
1889
|
+
{
|
|
1890
|
+
"aria-label": `${count} unread`,
|
|
1891
|
+
style: {
|
|
1892
|
+
background: effectiveConfig.accentColor,
|
|
1893
|
+
color: "#fff",
|
|
1894
|
+
borderRadius: 999,
|
|
1895
|
+
fontSize: "0.78em",
|
|
1896
|
+
fontWeight: 700,
|
|
1897
|
+
lineHeight: 1,
|
|
1898
|
+
padding: "3px 6px"
|
|
1899
|
+
},
|
|
1900
|
+
children: count > 99 ? "99+" : count
|
|
1901
|
+
}
|
|
1902
|
+
) : null
|
|
1903
|
+
]
|
|
1904
|
+
},
|
|
1905
|
+
tab.key
|
|
1906
|
+
);
|
|
1907
|
+
})
|
|
1908
|
+
}
|
|
1909
|
+
) : null,
|
|
1910
|
+
connectionState !== "live" ? /* @__PURE__ */ jsx("div", { role: connectionState === "error" ? "alert" : "status", children: connectionState === "error" ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1911
|
+
errorMessage ? /* @__PURE__ */ jsx("small", { children: errorMessage }) : null,
|
|
1912
|
+
/* @__PURE__ */ jsx("button", { type: "button", style: toolbarBtn, onClick: () => reconnect(), children: "Reconnect" })
|
|
1913
|
+
] }) : null }) : null,
|
|
1914
|
+
/* @__PURE__ */ jsxs(
|
|
1915
|
+
"div",
|
|
1916
|
+
{
|
|
1917
|
+
style: {
|
|
1918
|
+
marginTop: 12,
|
|
1919
|
+
display: "grid",
|
|
1920
|
+
gap: 8,
|
|
1921
|
+
maxHeight: layout.maxListHeight,
|
|
1922
|
+
minHeight: items.length === 0 && !loading ? 140 : void 0,
|
|
1923
|
+
overflowY: "auto",
|
|
1924
|
+
paddingRight: 2
|
|
1925
|
+
},
|
|
1926
|
+
children: [
|
|
1927
|
+
loading && items.length === 0 ? /* @__PURE__ */ jsx(
|
|
1928
|
+
"div",
|
|
1929
|
+
{
|
|
1930
|
+
role: "status",
|
|
1931
|
+
style: {
|
|
1932
|
+
alignSelf: "center",
|
|
1933
|
+
padding: "18px 0",
|
|
1934
|
+
color: dark ? "#d0d5dd" : light.muted,
|
|
1935
|
+
fontSize: "0.92em"
|
|
1936
|
+
},
|
|
1937
|
+
children: "Loading notifications..."
|
|
1938
|
+
}
|
|
1939
|
+
) : items.length === 0 ? /* @__PURE__ */ jsx(
|
|
1940
|
+
"div",
|
|
1941
|
+
{
|
|
1942
|
+
style: {
|
|
1943
|
+
alignSelf: "center",
|
|
1944
|
+
textAlign: "center",
|
|
1945
|
+
padding: "30px 18px",
|
|
1946
|
+
lineHeight: 1.5,
|
|
1947
|
+
color: dark ? "#c7b6a7" : light.muted,
|
|
1948
|
+
border: dark ? "1px dashed #3a4250" : `1px dashed ${light.border}`,
|
|
1949
|
+
borderRadius: effectiveConfig.radius - 2,
|
|
1950
|
+
background: dark ? "#181411" : "#fffdf9"
|
|
1951
|
+
},
|
|
1952
|
+
children: "No notifications yet. New activity will appear here as soon as this subscriber receives it."
|
|
1953
|
+
}
|
|
1954
|
+
) : items.map((item) => /* @__PURE__ */ jsxs(
|
|
1955
|
+
"div",
|
|
1956
|
+
{
|
|
1957
|
+
style: {
|
|
1338
1958
|
border: dark ? "1px solid #2b313b" : `1px solid ${light.border}`,
|
|
1339
|
-
borderRadius: effectiveConfig.radius,
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1959
|
+
borderRadius: effectiveConfig.radius - 2,
|
|
1960
|
+
padding: 14,
|
|
1961
|
+
background: item.readAt ? dark ? "#181411" : "#fffaf4" : dark ? "#1b1613" : light.itemBg,
|
|
1962
|
+
boxShadow: item.readAt ? "none" : dark ? `inset 0 0 0 1px ${withAlpha(effectiveConfig.accentColor, 0.18)}` : `inset 0 0 0 1px ${withAlpha(effectiveConfig.accentColor, 0.14)}`,
|
|
1963
|
+
opacity: item.resolvedAt ? 0.65 : 1
|
|
1964
|
+
},
|
|
1965
|
+
children: [
|
|
1966
|
+
/* @__PURE__ */ jsxs(
|
|
1967
|
+
"div",
|
|
1968
|
+
{
|
|
1969
|
+
style: {
|
|
1970
|
+
display: "flex",
|
|
1971
|
+
alignItems: "center",
|
|
1972
|
+
gap: 10,
|
|
1973
|
+
fontWeight: 700,
|
|
1974
|
+
marginBottom: 6,
|
|
1975
|
+
letterSpacing: "-0.02em"
|
|
1976
|
+
},
|
|
1977
|
+
children: [
|
|
1978
|
+
item.avatarUrl ? /* @__PURE__ */ jsx(
|
|
1979
|
+
"img",
|
|
1980
|
+
{
|
|
1981
|
+
src: item.avatarUrl,
|
|
1982
|
+
alt: "",
|
|
1983
|
+
width: 32,
|
|
1984
|
+
height: 32,
|
|
1985
|
+
loading: "lazy",
|
|
1986
|
+
style: { borderRadius: "50%", objectFit: "cover", flexShrink: 0 }
|
|
1987
|
+
}
|
|
1988
|
+
) : null,
|
|
1989
|
+
/* @__PURE__ */ jsx("span", { children: item.title ?? "Notification" })
|
|
1990
|
+
]
|
|
1991
|
+
}
|
|
1992
|
+
),
|
|
1993
|
+
/* @__PURE__ */ jsx(
|
|
1994
|
+
"div",
|
|
1995
|
+
{
|
|
1996
|
+
style: {
|
|
1997
|
+
opacity: 0.92,
|
|
1998
|
+
marginBottom: 10,
|
|
1999
|
+
lineHeight: 1.5,
|
|
2000
|
+
color: dark ? "#efe3d8" : "#4e3c34"
|
|
2001
|
+
},
|
|
2002
|
+
children: item.body
|
|
2003
|
+
}
|
|
2004
|
+
),
|
|
2005
|
+
item.imageUrl ? /* @__PURE__ */ jsx(
|
|
2006
|
+
"img",
|
|
2007
|
+
{
|
|
2008
|
+
src: item.imageUrl,
|
|
2009
|
+
alt: "",
|
|
2010
|
+
loading: "lazy",
|
|
2011
|
+
style: {
|
|
2012
|
+
display: "block",
|
|
2013
|
+
width: "100%",
|
|
2014
|
+
aspectRatio: "16 / 9",
|
|
2015
|
+
objectFit: "cover",
|
|
2016
|
+
borderRadius: Math.max(effectiveConfig.radius - 6, 4),
|
|
2017
|
+
marginBottom: 10
|
|
2018
|
+
}
|
|
2019
|
+
}
|
|
2020
|
+
) : null,
|
|
2021
|
+
/* @__PURE__ */ jsxs(
|
|
2022
|
+
"div",
|
|
2023
|
+
{
|
|
2024
|
+
style: {
|
|
2025
|
+
display: "flex",
|
|
2026
|
+
justifyContent: "space-between",
|
|
2027
|
+
alignItems: "center",
|
|
2028
|
+
gap: 12
|
|
2029
|
+
},
|
|
2030
|
+
children: [
|
|
2031
|
+
/* @__PURE__ */ jsxs(
|
|
2032
|
+
"div",
|
|
2033
|
+
{
|
|
2034
|
+
style: {
|
|
2035
|
+
display: "flex",
|
|
2036
|
+
alignItems: "center",
|
|
2037
|
+
gap: 10,
|
|
2038
|
+
flexWrap: "wrap"
|
|
2039
|
+
},
|
|
2040
|
+
children: [
|
|
2041
|
+
/* @__PURE__ */ jsx("small", { style: { opacity: 0.65 }, children: timeAgo(item.createdAt) }),
|
|
2042
|
+
item.actionUrl ? /* @__PURE__ */ jsx(
|
|
2043
|
+
"button",
|
|
2044
|
+
{
|
|
2045
|
+
type: "button",
|
|
2046
|
+
onClick: () => {
|
|
2047
|
+
void handleActionClick(item);
|
|
2048
|
+
},
|
|
2049
|
+
style: linkBtn,
|
|
2050
|
+
children: item.actionLabel ?? "Open"
|
|
2051
|
+
}
|
|
2052
|
+
) : null
|
|
2053
|
+
]
|
|
2054
|
+
}
|
|
2055
|
+
),
|
|
2056
|
+
/* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: 12, alignItems: "center" }, children: [
|
|
2057
|
+
!item.readAt ? /* @__PURE__ */ jsx("button", { type: "button", style: linkBtn, onClick: () => markRead(item._id), children: "Mark read" }) : /* @__PURE__ */ jsx("small", { style: { opacity: 0.65 }, children: "Read" }),
|
|
2058
|
+
effectiveConfig.showDismiss ? /* @__PURE__ */ jsx(
|
|
2059
|
+
"button",
|
|
2060
|
+
{
|
|
2061
|
+
type: "button",
|
|
2062
|
+
style: { ...linkBtn, color: dark ? "#c7b6a7" : light.muted },
|
|
2063
|
+
onClick: () => {
|
|
2064
|
+
void archive(item._id).catch(() => void 0);
|
|
2065
|
+
},
|
|
2066
|
+
children: "Dismiss"
|
|
2067
|
+
}
|
|
2068
|
+
) : null
|
|
2069
|
+
] })
|
|
2070
|
+
]
|
|
2071
|
+
}
|
|
2072
|
+
)
|
|
2073
|
+
]
|
|
2074
|
+
},
|
|
2075
|
+
item._id
|
|
2076
|
+
)),
|
|
2077
|
+
items.length > 0 && hasMore ? /* @__PURE__ */ jsx(
|
|
2078
|
+
"button",
|
|
2079
|
+
{
|
|
2080
|
+
type: "button",
|
|
2081
|
+
style: { ...toolbarBtn, justifySelf: "center" },
|
|
2082
|
+
disabled: loadingMore,
|
|
2083
|
+
onClick: () => {
|
|
2084
|
+
void loadMore().catch(() => void 0);
|
|
2085
|
+
},
|
|
2086
|
+
children: loadingMore ? "Loading..." : "Show older"
|
|
2087
|
+
}
|
|
2088
|
+
) : null
|
|
2089
|
+
]
|
|
2090
|
+
}
|
|
2091
|
+
),
|
|
2092
|
+
effectiveConfig.showMarkAll && items.length > 0 ? /* @__PURE__ */ jsx(
|
|
2093
|
+
"div",
|
|
2094
|
+
{
|
|
2095
|
+
style: {
|
|
2096
|
+
display: "flex",
|
|
2097
|
+
justifyContent: "flex-end",
|
|
2098
|
+
marginTop: 12,
|
|
2099
|
+
paddingTop: 10,
|
|
2100
|
+
borderTop: dark ? "1px solid #2b313b" : `1px solid ${light.border}`
|
|
2101
|
+
},
|
|
2102
|
+
children: /* @__PURE__ */ jsx("button", { type: "button", style: linkBtn, onClick: () => markAllRead(), children: "Mark all as read" })
|
|
2103
|
+
}
|
|
2104
|
+
) : null
|
|
2105
|
+
]
|
|
2106
|
+
}
|
|
2107
|
+
) }) })
|
|
2108
|
+
}
|
|
2109
|
+
);
|
|
1458
2110
|
}
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
2111
|
+
var __internal = {
|
|
2112
|
+
DEFAULT_BEEPER_WIDGET_API_BASE_URL,
|
|
2113
|
+
tabKinds,
|
|
2114
|
+
countTabUnread,
|
|
2115
|
+
buildKindsQuery,
|
|
2116
|
+
parseTokenExpiryMs,
|
|
2117
|
+
isSessionExpiringSoon,
|
|
2118
|
+
pickRealtimeSource,
|
|
2119
|
+
createWidgetSocketClient
|
|
1466
2120
|
};
|
|
2121
|
+
|
|
2122
|
+
export { BeeperInboxBell, BeeperInboxPanel, BeeperInboxProvider, __internal, useBeeperInbox, useBeeperInboxUnreadCount };
|
|
2123
|
+
//# sourceMappingURL=index.js.map
|
|
1467
2124
|
//# sourceMappingURL=index.js.map
|