@poolse/react 3.7.1 → 3.8.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/CHANGELOG.md +21 -0
- package/README.md +52 -25
- package/dist/index.cjs +385 -50
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +170 -8
- package/dist/index.d.ts +170 -8
- package/dist/index.js +363 -54
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -11,6 +11,10 @@ var jsxRuntime = require('react/jsx-runtime');
|
|
|
11
11
|
var ConversationsStore = class {
|
|
12
12
|
constructor(chat) {
|
|
13
13
|
this.chat = chat;
|
|
14
|
+
const cached = chat.cache.peekConversations();
|
|
15
|
+
if (cached && cached.length > 0) {
|
|
16
|
+
this.state = { ...this.state, conversations: cached, loading: false };
|
|
17
|
+
}
|
|
14
18
|
}
|
|
15
19
|
chat;
|
|
16
20
|
state = {
|
|
@@ -29,6 +33,14 @@ var ConversationsStore = class {
|
|
|
29
33
|
nextCursor = null;
|
|
30
34
|
/** Whether rows beyond the first page are held. */
|
|
31
35
|
pagedBeyondFirst = false;
|
|
36
|
+
destroyed = false;
|
|
37
|
+
hydrating = null;
|
|
38
|
+
/** Called by `chat.destroy()`. @internal */
|
|
39
|
+
destroy() {
|
|
40
|
+
this.destroyed = true;
|
|
41
|
+
this.deactivate();
|
|
42
|
+
this.listeners.clear();
|
|
43
|
+
}
|
|
32
44
|
getSnapshot() {
|
|
33
45
|
return this.state;
|
|
34
46
|
}
|
|
@@ -78,6 +90,7 @@ var ConversationsStore = class {
|
|
|
78
90
|
}
|
|
79
91
|
/** Zero a conversation's unread and mention counts. */
|
|
80
92
|
markRead(conversationId) {
|
|
93
|
+
this.chat.unread.apply(conversationId, 0, 0);
|
|
81
94
|
this.setConversations(
|
|
82
95
|
(prev) => patchRow(
|
|
83
96
|
prev,
|
|
@@ -105,8 +118,11 @@ var ConversationsStore = class {
|
|
|
105
118
|
this.setConversations((prev) => prepend(prev, conversation));
|
|
106
119
|
}
|
|
107
120
|
activate() {
|
|
108
|
-
|
|
121
|
+
if (this.destroyed) return;
|
|
122
|
+
const showing = this.state.conversations.length > 0;
|
|
123
|
+
void this.load(!this.loaded && !showing).catch(() => {
|
|
109
124
|
});
|
|
125
|
+
if (!this.loaded && !showing) this.hydrate();
|
|
110
126
|
let off = null;
|
|
111
127
|
let boundId = null;
|
|
112
128
|
const bind = (me) => {
|
|
@@ -123,6 +139,16 @@ var ConversationsStore = class {
|
|
|
123
139
|
off?.();
|
|
124
140
|
};
|
|
125
141
|
}
|
|
142
|
+
// Show the stored list (a previous session's) while the first fetch
|
|
143
|
+
// is out — or instead of it, offline.
|
|
144
|
+
hydrate() {
|
|
145
|
+
this.hydrating ??= this.chat.cache.loadConversations().then((rows) => {
|
|
146
|
+
if (this.destroyed || this.loaded || !rows || rows.length === 0) return;
|
|
147
|
+
if (this.state.conversations.length > 0) return;
|
|
148
|
+
this.chat.timings._once("cachedConversations");
|
|
149
|
+
this.set({ ...this.state, conversations: rows, loading: false });
|
|
150
|
+
});
|
|
151
|
+
}
|
|
126
152
|
deactivate() {
|
|
127
153
|
this.unbind?.();
|
|
128
154
|
this.unbind = null;
|
|
@@ -156,6 +182,7 @@ var ConversationsStore = class {
|
|
|
156
182
|
if (this.inflight) return this.inflight;
|
|
157
183
|
const run = this.chat.conversations.list().then(
|
|
158
184
|
(list) => {
|
|
185
|
+
if (this.destroyed) return;
|
|
159
186
|
this.loaded = true;
|
|
160
187
|
let conversations = list.data;
|
|
161
188
|
if (this.pagedBeyondFirst) {
|
|
@@ -176,6 +203,7 @@ var ConversationsStore = class {
|
|
|
176
203
|
});
|
|
177
204
|
},
|
|
178
205
|
(err) => {
|
|
206
|
+
if (this.destroyed) return;
|
|
179
207
|
this.set({
|
|
180
208
|
...this.state,
|
|
181
209
|
loading: false,
|
|
@@ -193,6 +221,10 @@ var ConversationsStore = class {
|
|
|
193
221
|
if (next !== this.state.conversations) this.set({ ...this.state, conversations: next });
|
|
194
222
|
}
|
|
195
223
|
set(state) {
|
|
224
|
+
if (state.conversations !== this.state.conversations) {
|
|
225
|
+
this.chat.unread.observe(state.conversations);
|
|
226
|
+
if (this.loaded) this.chat.cache.setConversations(state.conversations);
|
|
227
|
+
}
|
|
196
228
|
this.state = state;
|
|
197
229
|
for (const l of this.listeners) l();
|
|
198
230
|
}
|
|
@@ -232,12 +264,27 @@ function mergeUpdated(prev, next, fromMe) {
|
|
|
232
264
|
const rest = prev.slice(0, idx).concat(prev.slice(idx + 1));
|
|
233
265
|
return [merged, ...rest];
|
|
234
266
|
}
|
|
267
|
+
function conversationsStoreOf(chat) {
|
|
268
|
+
return chat.scoped("react:conversations", (c) => new ConversationsStore(c));
|
|
269
|
+
}
|
|
235
270
|
var PoolseContext = react.createContext(null);
|
|
236
|
-
function PoolseProvider({
|
|
271
|
+
function PoolseProvider({
|
|
272
|
+
client,
|
|
273
|
+
config: configProp,
|
|
274
|
+
background,
|
|
275
|
+
trackActivity = true,
|
|
276
|
+
children
|
|
277
|
+
}) {
|
|
278
|
+
if (!client && !configProp) {
|
|
279
|
+
throw new Error("<PoolseProvider>: pass `client` or `config`.");
|
|
280
|
+
}
|
|
281
|
+
const config = configProp ?? { getToken: () => null };
|
|
237
282
|
const configRef = react.useRef(config);
|
|
238
283
|
configRef.current = config;
|
|
239
284
|
const initialConnectionRef = react.useRef(null);
|
|
240
|
-
const
|
|
285
|
+
const externalRef = react.useRef(client !== void 0);
|
|
286
|
+
const ownChat = react.useMemo(() => {
|
|
287
|
+
if (externalRef.current) return null;
|
|
241
288
|
const initial = configRef.current;
|
|
242
289
|
initialConnectionRef.current = {
|
|
243
290
|
apiUrl: initial.apiUrl,
|
|
@@ -265,6 +312,9 @@ function PoolseProvider({ config, trackActivity = true, children }) {
|
|
|
265
312
|
...initial.userResolver ? { userResolver: (id) => configRef.current.userResolver(id) } : {}
|
|
266
313
|
});
|
|
267
314
|
}, []);
|
|
315
|
+
const chat = client ?? ownChat;
|
|
316
|
+
if (!chat)
|
|
317
|
+
throw new Error("<PoolseProvider>: `client` was removed after mount; remount instead.");
|
|
268
318
|
if (process.env.NODE_ENV !== "production") {
|
|
269
319
|
react.useEffect(() => {
|
|
270
320
|
const initial = initialConnectionRef.current;
|
|
@@ -277,17 +327,22 @@ function PoolseProvider({ config, trackActivity = true, children }) {
|
|
|
277
327
|
}, [config.apiUrl, config.wsUrl, config.socketPath]);
|
|
278
328
|
}
|
|
279
329
|
react.useEffect(() => {
|
|
330
|
+
if (!ownChat) return;
|
|
280
331
|
return () => {
|
|
281
|
-
|
|
332
|
+
void ownChat.destroy();
|
|
282
333
|
};
|
|
283
|
-
}, [
|
|
334
|
+
}, [ownChat]);
|
|
335
|
+
react.useEffect(() => {
|
|
336
|
+
if (background === void 0) return;
|
|
337
|
+
chat.setBackground(background);
|
|
338
|
+
}, [chat, background]);
|
|
284
339
|
const trackActivityRef = react.useRef(trackActivity);
|
|
285
340
|
react.useEffect(() => {
|
|
286
341
|
const opts = trackActivityRef.current;
|
|
287
342
|
if (opts === false) return;
|
|
288
343
|
return chat.presence.trackActivity(opts === true ? {} : opts);
|
|
289
344
|
}, [chat]);
|
|
290
|
-
const value = react.useMemo(() => ({ chat, conversations:
|
|
345
|
+
const value = react.useMemo(() => ({ chat, conversations: conversationsStoreOf(chat) }), [chat]);
|
|
291
346
|
return /* @__PURE__ */ jsxRuntime.jsx(PoolseContext.Provider, { value, children });
|
|
292
347
|
}
|
|
293
348
|
function usePoolse() {
|
|
@@ -296,6 +351,9 @@ function usePoolse() {
|
|
|
296
351
|
function useConversationsStore() {
|
|
297
352
|
return usePoolseContext("useConversations").conversations;
|
|
298
353
|
}
|
|
354
|
+
function useOptionalPoolse() {
|
|
355
|
+
return react.useContext(PoolseContext)?.chat ?? null;
|
|
356
|
+
}
|
|
299
357
|
function usePoolseContext(hook) {
|
|
300
358
|
const value = react.useContext(PoolseContext);
|
|
301
359
|
if (!value) {
|
|
@@ -336,42 +394,8 @@ function useMe() {
|
|
|
336
394
|
}, [chat]);
|
|
337
395
|
return state;
|
|
338
396
|
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
const [conversation, setConversation] = react.useState(null);
|
|
342
|
-
const [loading, setLoading] = react.useState(id !== null);
|
|
343
|
-
const [error, setError] = react.useState(null);
|
|
344
|
-
const fetcher = react.useCallback(
|
|
345
|
-
async (signal) => {
|
|
346
|
-
if (id === null) return;
|
|
347
|
-
setLoading(true);
|
|
348
|
-
setError(null);
|
|
349
|
-
try {
|
|
350
|
-
const conv = await chat.conversations.one(id).show(signal);
|
|
351
|
-
setConversation(conv);
|
|
352
|
-
} catch (err) {
|
|
353
|
-
if (err?.name === "AbortError") return;
|
|
354
|
-
setError(err instanceof Error ? err : new Error(String(err)));
|
|
355
|
-
} finally {
|
|
356
|
-
setLoading(false);
|
|
357
|
-
}
|
|
358
|
-
},
|
|
359
|
-
[chat, id]
|
|
360
|
-
);
|
|
361
|
-
react.useEffect(() => {
|
|
362
|
-
if (id === null) {
|
|
363
|
-
setConversation(null);
|
|
364
|
-
setLoading(false);
|
|
365
|
-
setError(null);
|
|
366
|
-
return;
|
|
367
|
-
}
|
|
368
|
-
const controller = new AbortController();
|
|
369
|
-
void fetcher(controller.signal);
|
|
370
|
-
return () => controller.abort();
|
|
371
|
-
}, [fetcher, id]);
|
|
372
|
-
const refetch = react.useCallback(() => fetcher(), [fetcher]);
|
|
373
|
-
return { conversation, loading, error, refetch };
|
|
374
|
-
}
|
|
397
|
+
|
|
398
|
+
// src/use-conversations.ts
|
|
375
399
|
function useConversations() {
|
|
376
400
|
const store = useConversationsStore();
|
|
377
401
|
useMe();
|
|
@@ -416,6 +440,189 @@ function countsOf(conversations, key) {
|
|
|
416
440
|
}
|
|
417
441
|
return out;
|
|
418
442
|
}
|
|
443
|
+
|
|
444
|
+
// src/use-unread.ts
|
|
445
|
+
function useUnreadTotals(client) {
|
|
446
|
+
const fromContext = useOptionalPoolse();
|
|
447
|
+
const chat = client ?? fromContext;
|
|
448
|
+
if (!chat) {
|
|
449
|
+
throw new Error("useUnreadTotals(): pass a client, or render inside a <PoolseProvider>.");
|
|
450
|
+
}
|
|
451
|
+
const subscribe = react.useCallback((fn) => chat.unread.subscribe(fn), [chat]);
|
|
452
|
+
const totals = react.useSyncExternalStore(subscribe, () => chat.unread.get());
|
|
453
|
+
const refresh = react.useCallback(() => chat.unread.refresh(), [chat]);
|
|
454
|
+
return react.useMemo(() => ({ ...totals, refresh }), [totals, refresh]);
|
|
455
|
+
}
|
|
456
|
+
function useUnreadQueue(opts = {}) {
|
|
457
|
+
const chat = usePoolse();
|
|
458
|
+
const { conversations, markConversationRead } = useConversations();
|
|
459
|
+
const { mentionsFirst = false, filter } = opts;
|
|
460
|
+
const [skipped, setSkipped] = react.useState(() => /* @__PURE__ */ new Set());
|
|
461
|
+
const [currentId, setCurrentId] = react.useState(null);
|
|
462
|
+
const queue = react.useMemo(
|
|
463
|
+
() => orderUnread(conversations, { mentionsFirst, ...filter ? { filter } : {} }),
|
|
464
|
+
[conversations, mentionsFirst, filter]
|
|
465
|
+
);
|
|
466
|
+
const offered = react.useMemo(() => queue.filter((c) => !skipped.has(c.id)), [queue, skipped]);
|
|
467
|
+
const pinned = currentId ? conversations.find((c) => c.id === currentId) ?? null : null;
|
|
468
|
+
const current = pinned ?? offered[0] ?? null;
|
|
469
|
+
react.useEffect(() => {
|
|
470
|
+
if (currentId && !pinned) setCurrentId(null);
|
|
471
|
+
}, [currentId, pinned]);
|
|
472
|
+
const markRead = react.useCallback(
|
|
473
|
+
async (conversationId) => {
|
|
474
|
+
markConversationRead(conversationId);
|
|
475
|
+
const page = await chat.conversations.one(conversationId).messages.list({ limit: 1 });
|
|
476
|
+
const latest = page.data[0];
|
|
477
|
+
if (latest) await chat.conversations.one(conversationId).messages.markRead(latest.id);
|
|
478
|
+
},
|
|
479
|
+
[chat, markConversationRead]
|
|
480
|
+
);
|
|
481
|
+
const after = (id) => offered.find((c) => c.id !== id) ?? null;
|
|
482
|
+
const next = () => {
|
|
483
|
+
if (current) void markRead(current.id).catch(() => {
|
|
484
|
+
});
|
|
485
|
+
const following = after(current?.id);
|
|
486
|
+
setCurrentId(following?.id ?? null);
|
|
487
|
+
return following;
|
|
488
|
+
};
|
|
489
|
+
const skip = () => {
|
|
490
|
+
const id = current?.id;
|
|
491
|
+
if (id) setSkipped((s) => new Set(s).add(id));
|
|
492
|
+
const following = after(id);
|
|
493
|
+
setCurrentId(following?.id ?? null);
|
|
494
|
+
return following;
|
|
495
|
+
};
|
|
496
|
+
const reset = react.useCallback(() => setSkipped(/* @__PURE__ */ new Set()), []);
|
|
497
|
+
return {
|
|
498
|
+
queue,
|
|
499
|
+
current,
|
|
500
|
+
remaining: offered.filter((c) => c.id !== current?.id).length,
|
|
501
|
+
next,
|
|
502
|
+
skip,
|
|
503
|
+
markRead,
|
|
504
|
+
reset
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
function orderUnread(conversations, opts = {}) {
|
|
508
|
+
const unread = conversations.filter(
|
|
509
|
+
(c) => (c.unread_count ?? 0) > 0 && (!opts.filter || opts.filter(c))
|
|
510
|
+
);
|
|
511
|
+
const at = (c) => c.last_message_at ? Date.parse(c.last_message_at) : 0;
|
|
512
|
+
return unread.sort((a, b) => {
|
|
513
|
+
if (opts.mentionsFirst) {
|
|
514
|
+
const ma = (a.mention_unread_count ?? 0) > 0 ? 1 : 0;
|
|
515
|
+
const mb = (b.mention_unread_count ?? 0) > 0 ? 1 : 0;
|
|
516
|
+
if (ma !== mb) return mb - ma;
|
|
517
|
+
}
|
|
518
|
+
return at(b) - at(a);
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
function useOutbox(opts = {}) {
|
|
522
|
+
const fromContext = useOptionalPoolse();
|
|
523
|
+
const chat = opts.client ?? fromContext;
|
|
524
|
+
if (!chat) throw new Error("useOutbox(): pass a client, or render inside a <PoolseProvider>.");
|
|
525
|
+
const subscribe = react.useCallback((fn) => chat.outbox.subscribe(fn), [chat]);
|
|
526
|
+
const all = react.useSyncExternalStore(subscribe, () => chat.outbox.entries());
|
|
527
|
+
const { conversationId } = opts;
|
|
528
|
+
const entries = react.useMemo(
|
|
529
|
+
() => conversationId ? all.filter((e) => e.conversationId === conversationId) : all,
|
|
530
|
+
[all, conversationId]
|
|
531
|
+
);
|
|
532
|
+
const retry = react.useCallback(
|
|
533
|
+
async (id) => {
|
|
534
|
+
try {
|
|
535
|
+
await chat.outbox.retry(id);
|
|
536
|
+
} catch (err) {
|
|
537
|
+
if (!(err instanceof sdk.MessageQueuedError)) throw err;
|
|
538
|
+
}
|
|
539
|
+
},
|
|
540
|
+
[chat]
|
|
541
|
+
);
|
|
542
|
+
const remove = react.useCallback((id) => chat.outbox.remove(id), [chat]);
|
|
543
|
+
const flush = react.useCallback(() => chat.outbox.flush(), [chat]);
|
|
544
|
+
return {
|
|
545
|
+
entries,
|
|
546
|
+
pending: entries.filter((e) => e.status !== "failed").length,
|
|
547
|
+
failed: entries.filter((e) => e.status === "failed").length,
|
|
548
|
+
retry,
|
|
549
|
+
remove,
|
|
550
|
+
flush
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
function useClientTimings(client) {
|
|
554
|
+
const fromContext = useOptionalPoolse();
|
|
555
|
+
const chat = client ?? fromContext;
|
|
556
|
+
if (!chat) {
|
|
557
|
+
throw new Error("useClientTimings(): pass a client, or render inside a <PoolseProvider>.");
|
|
558
|
+
}
|
|
559
|
+
const snapshot = react.useRef(null);
|
|
560
|
+
const subscribe = react.useCallback(
|
|
561
|
+
(fn) => chat.timings.on(() => {
|
|
562
|
+
snapshot.current = null;
|
|
563
|
+
fn();
|
|
564
|
+
}),
|
|
565
|
+
[chat]
|
|
566
|
+
);
|
|
567
|
+
const get = () => {
|
|
568
|
+
if (!snapshot.current || snapshot.current.chat !== chat) {
|
|
569
|
+
snapshot.current = { chat, value: chat.timings.get() };
|
|
570
|
+
}
|
|
571
|
+
return snapshot.current.value;
|
|
572
|
+
};
|
|
573
|
+
return react.useSyncExternalStore(subscribe, get);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// src/conversation-search.ts
|
|
577
|
+
function filterConversations(conversations, query, opts = {}) {
|
|
578
|
+
const needle = query.trim().toLowerCase();
|
|
579
|
+
if (!needle) return conversations.slice();
|
|
580
|
+
return conversations.filter((c) => {
|
|
581
|
+
const hay = [c.name ?? "", c.last_message_preview ?? ""];
|
|
582
|
+
for (const ext of c.member_external_ids ?? []) {
|
|
583
|
+
if (ext === opts.meExternalId) continue;
|
|
584
|
+
hay.push(ext);
|
|
585
|
+
if (opts.labelFor) hay.push(opts.labelFor(ext));
|
|
586
|
+
}
|
|
587
|
+
return hay.some((h) => h.toLowerCase().includes(needle));
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
function useConversation(id) {
|
|
591
|
+
const chat = usePoolse();
|
|
592
|
+
const [conversation, setConversation] = react.useState(null);
|
|
593
|
+
const [loading, setLoading] = react.useState(id !== null);
|
|
594
|
+
const [error, setError] = react.useState(null);
|
|
595
|
+
const fetcher = react.useCallback(
|
|
596
|
+
async (signal) => {
|
|
597
|
+
if (id === null) return;
|
|
598
|
+
setLoading(true);
|
|
599
|
+
setError(null);
|
|
600
|
+
try {
|
|
601
|
+
const conv = await chat.conversations.one(id).show(signal);
|
|
602
|
+
setConversation(conv);
|
|
603
|
+
} catch (err) {
|
|
604
|
+
if (err?.name === "AbortError") return;
|
|
605
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
606
|
+
} finally {
|
|
607
|
+
setLoading(false);
|
|
608
|
+
}
|
|
609
|
+
},
|
|
610
|
+
[chat, id]
|
|
611
|
+
);
|
|
612
|
+
react.useEffect(() => {
|
|
613
|
+
if (id === null) {
|
|
614
|
+
setConversation(null);
|
|
615
|
+
setLoading(false);
|
|
616
|
+
setError(null);
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
const controller = new AbortController();
|
|
620
|
+
void fetcher(controller.signal);
|
|
621
|
+
return () => controller.abort();
|
|
622
|
+
}, [fetcher, id]);
|
|
623
|
+
const refetch = react.useCallback(() => fetcher(), [fetcher]);
|
|
624
|
+
return { conversation, loading, error, refetch };
|
|
625
|
+
}
|
|
419
626
|
function useMembers(conversationId) {
|
|
420
627
|
const chat = usePoolse();
|
|
421
628
|
const [members, setMembers] = react.useState([]);
|
|
@@ -516,10 +723,14 @@ function useMessages(conversationId) {
|
|
|
516
723
|
const chat = usePoolse();
|
|
517
724
|
const conversationsStore = useConversationsStore();
|
|
518
725
|
const { me } = useMe();
|
|
519
|
-
const [messages, setMessages] = react.useState(
|
|
520
|
-
|
|
726
|
+
const [messages, setMessages] = react.useState(
|
|
727
|
+
() => initialRows(chat, conversationId) ?? []
|
|
728
|
+
);
|
|
729
|
+
const [loading, setLoading] = react.useState(() => !initialRows(chat, conversationId));
|
|
521
730
|
const [error, setError] = react.useState(null);
|
|
522
731
|
const [hasMore, setHasMore] = react.useState(true);
|
|
732
|
+
const [fromCache, setFromCache] = react.useState(() => !!chat.cache.peekMessages(conversationId));
|
|
733
|
+
const rowsForRef = react.useRef(null);
|
|
523
734
|
const oldestSequenceRef = react.useRef(null);
|
|
524
735
|
const messagesRef = react.useRef(messages);
|
|
525
736
|
messagesRef.current = messages;
|
|
@@ -530,33 +741,77 @@ function useMessages(conversationId) {
|
|
|
530
741
|
meIdRef.current = me?.id ?? null;
|
|
531
742
|
const meExternalIdRef = react.useRef(me?.external_id ?? null);
|
|
532
743
|
meExternalIdRef.current = me?.external_id ?? null;
|
|
744
|
+
react.useEffect(() => {
|
|
745
|
+
if (rowsForRef.current !== conversationId) return;
|
|
746
|
+
if (messages.some((m) => m.conversation_id !== conversationId)) return;
|
|
747
|
+
if (loading) return;
|
|
748
|
+
chat.cache.setMessages(conversationId, messages);
|
|
749
|
+
}, [chat, conversationId, messages, loading]);
|
|
533
750
|
react.useEffect(() => {
|
|
534
751
|
let cancelled = false;
|
|
535
|
-
|
|
536
|
-
|
|
752
|
+
let fresh = false;
|
|
753
|
+
const cached = chat.cache.peekMessages(conversationId);
|
|
754
|
+
rowsForRef.current = conversationId;
|
|
537
755
|
setError(null);
|
|
538
756
|
setHasMore(true);
|
|
539
|
-
|
|
757
|
+
if (cached) {
|
|
758
|
+
setMessages(withOutbox(cached, chat, conversationId));
|
|
759
|
+
setLoading(false);
|
|
760
|
+
setFromCache(true);
|
|
761
|
+
oldestSequenceRef.current = cached[0]?.sequence ?? null;
|
|
762
|
+
chat.timings._once("cachedMessages");
|
|
763
|
+
} else {
|
|
764
|
+
setLoading(true);
|
|
765
|
+
setMessages(withOutbox([], chat, conversationId));
|
|
766
|
+
setFromCache(false);
|
|
767
|
+
oldestSequenceRef.current = null;
|
|
768
|
+
void chat.cache.loadMessages(conversationId).then((stored) => {
|
|
769
|
+
if (cancelled || fresh || !stored || stored.length === 0) return;
|
|
770
|
+
setMessages(withOutbox(stored, chat, conversationId));
|
|
771
|
+
setLoading(false);
|
|
772
|
+
setError(null);
|
|
773
|
+
setFromCache(true);
|
|
774
|
+
oldestSequenceRef.current = stored[0]?.sequence ?? null;
|
|
775
|
+
chat.timings._once("cachedMessages");
|
|
776
|
+
});
|
|
777
|
+
}
|
|
540
778
|
const handle = chat.conversations.one(conversationId).messages;
|
|
541
779
|
handle.list({ limit: PAGE_SIZE }).then((page) => {
|
|
542
780
|
if (cancelled) return;
|
|
781
|
+
fresh = true;
|
|
543
782
|
const ordered = [...page.data].reverse();
|
|
544
|
-
setMessages(ordered);
|
|
783
|
+
setMessages((prev) => keepLocal(ordered, prev));
|
|
545
784
|
setHasMore(page.data.length === PAGE_SIZE);
|
|
546
785
|
oldestSequenceRef.current = ordered[0]?.sequence ?? null;
|
|
547
786
|
setLoading(false);
|
|
787
|
+
setFromCache(false);
|
|
548
788
|
}).catch((err) => {
|
|
549
789
|
if (cancelled) return;
|
|
790
|
+
if (chat.cache.peekMessages(conversationId)) {
|
|
791
|
+
setLoading(false);
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
550
794
|
setError(err instanceof Error ? err : new Error(String(err)));
|
|
551
795
|
setLoading(false);
|
|
552
796
|
});
|
|
553
797
|
refetchRef.current = async () => {
|
|
554
798
|
const page = await handle.list({ limit: PAGE_SIZE });
|
|
555
799
|
const ordered = [...page.data].reverse();
|
|
556
|
-
setMessages(ordered);
|
|
800
|
+
setMessages((prev) => keepLocal(ordered, prev));
|
|
557
801
|
setHasMore(page.data.length === PAGE_SIZE);
|
|
558
802
|
oldestSequenceRef.current = ordered[0]?.sequence ?? null;
|
|
803
|
+
setFromCache(false);
|
|
559
804
|
};
|
|
805
|
+
const syncOutbox = () => {
|
|
806
|
+
if (cancelled) return;
|
|
807
|
+
const entries = chat.outbox.entries(conversationId);
|
|
808
|
+
setMessages((prev) => applyOutbox(prev, entries));
|
|
809
|
+
};
|
|
810
|
+
const offOutbox = chat.outbox.subscribe(syncOutbox);
|
|
811
|
+
const offSent = chat.outbox.onSent((entry, sent) => {
|
|
812
|
+
if (cancelled || entry.conversationId !== conversationId) return;
|
|
813
|
+
setMessages((prev) => upsertById(prev, sent));
|
|
814
|
+
});
|
|
560
815
|
const conv = chat.realtime.conversation(conversationId);
|
|
561
816
|
const offNew = conv.onMessage((msg) => {
|
|
562
817
|
if (msg.thread_root_id) {
|
|
@@ -595,7 +850,10 @@ function useMessages(conversationId) {
|
|
|
595
850
|
connectedBefore ||= wasConnected;
|
|
596
851
|
if (!reconnected || cancelled) return;
|
|
597
852
|
handle.list({ limit: PAGE_SIZE }).then((page) => {
|
|
598
|
-
if (!cancelled)
|
|
853
|
+
if (!cancelled) {
|
|
854
|
+
setMessages((prev) => mergeLatest(prev, page.data));
|
|
855
|
+
setFromCache(false);
|
|
856
|
+
}
|
|
599
857
|
}).catch(() => {
|
|
600
858
|
});
|
|
601
859
|
});
|
|
@@ -606,6 +864,8 @@ function useMessages(conversationId) {
|
|
|
606
864
|
offDeleted();
|
|
607
865
|
offThread();
|
|
608
866
|
offStatus();
|
|
867
|
+
offOutbox();
|
|
868
|
+
offSent();
|
|
609
869
|
};
|
|
610
870
|
}, [chat, conversationId]);
|
|
611
871
|
const loadMore = react.useCallback(async () => {
|
|
@@ -687,6 +947,12 @@ function useMessages(conversationId) {
|
|
|
687
947
|
inserted_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
688
948
|
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
689
949
|
};
|
|
950
|
+
if (chat.outboxEnabled) {
|
|
951
|
+
setMessages((prev) => upsertById(prev, { ...tempMsg, local_status: "sending" }));
|
|
952
|
+
const sent = await chat.outbox.send(conversationId, { ...attrs, id }, tempMsg);
|
|
953
|
+
setMessages((prev) => upsertById(prev, sent));
|
|
954
|
+
return sent;
|
|
955
|
+
}
|
|
690
956
|
setMessages((prev) => [...prev, tempMsg]);
|
|
691
957
|
try {
|
|
692
958
|
const sent = await chat.conversations.one(conversationId).messages.send({ ...attrs, id });
|
|
@@ -752,6 +1018,24 @@ function useMessages(conversationId) {
|
|
|
752
1018
|
},
|
|
753
1019
|
[chat, conversationId, conversationsStore]
|
|
754
1020
|
);
|
|
1021
|
+
const retry = react.useCallback(
|
|
1022
|
+
async (messageId) => {
|
|
1023
|
+
try {
|
|
1024
|
+
await chat.outbox.retry(messageId);
|
|
1025
|
+
} catch (err) {
|
|
1026
|
+
if (err instanceof sdk.MessageQueuedError) return;
|
|
1027
|
+
throw err;
|
|
1028
|
+
}
|
|
1029
|
+
},
|
|
1030
|
+
[chat]
|
|
1031
|
+
);
|
|
1032
|
+
const discard = react.useCallback(
|
|
1033
|
+
(messageId) => {
|
|
1034
|
+
chat.outbox.remove(messageId);
|
|
1035
|
+
setMessages((prev) => prev.filter((m) => m.id !== messageId || !m.local_status));
|
|
1036
|
+
},
|
|
1037
|
+
[chat]
|
|
1038
|
+
);
|
|
755
1039
|
const refetch = react.useCallback(async () => {
|
|
756
1040
|
const fn = refetchRef.current;
|
|
757
1041
|
if (!fn) return;
|
|
@@ -765,12 +1049,37 @@ function useMessages(conversationId) {
|
|
|
765
1049
|
loadMore,
|
|
766
1050
|
loadUntil,
|
|
767
1051
|
send,
|
|
1052
|
+
retry,
|
|
1053
|
+
discard,
|
|
768
1054
|
edit,
|
|
769
1055
|
delete: deleteMessage,
|
|
770
1056
|
markReadUpTo,
|
|
1057
|
+
fromCache,
|
|
771
1058
|
refetch
|
|
772
1059
|
};
|
|
773
1060
|
}
|
|
1061
|
+
function initialRows(chat, conversationId) {
|
|
1062
|
+
const cached = chat.cache.peekMessages(conversationId);
|
|
1063
|
+
return cached ? withOutbox(cached, chat, conversationId) : null;
|
|
1064
|
+
}
|
|
1065
|
+
function withOutbox(rows, chat, conversationId) {
|
|
1066
|
+
return applyOutbox(rows, chat.outbox.entries(conversationId));
|
|
1067
|
+
}
|
|
1068
|
+
function applyOutbox(prev, entries) {
|
|
1069
|
+
const byId = new Map(entries.map((e) => [e.id, e]));
|
|
1070
|
+
let next = prev.filter((m) => !m.local_status || byId.has(m.id));
|
|
1071
|
+
for (const entry of entries) {
|
|
1072
|
+
const idx = next.findIndex((m) => m.id === entry.id);
|
|
1073
|
+
if (idx !== -1 && !next[idx].local_status) continue;
|
|
1074
|
+
if (idx !== -1 && next[idx] === entry.message) continue;
|
|
1075
|
+
next = upsertById(next, entry.message);
|
|
1076
|
+
}
|
|
1077
|
+
return next.length === prev.length && next.every((m, i) => m === prev[i]) ? prev : next;
|
|
1078
|
+
}
|
|
1079
|
+
function keepLocal(page, prev) {
|
|
1080
|
+
const local = prev.filter((m) => m.local_status && !page.some((p) => p.id === m.id));
|
|
1081
|
+
return local.length === 0 ? page : [...page, ...local];
|
|
1082
|
+
}
|
|
774
1083
|
function patchById(prev, id, fn) {
|
|
775
1084
|
const idx = prev.findIndex((m) => m.id === id);
|
|
776
1085
|
if (idx === -1) return prev;
|
|
@@ -1778,10 +2087,22 @@ Object.defineProperty(exports, "AuthError", {
|
|
|
1778
2087
|
enumerable: true,
|
|
1779
2088
|
get: function () { return sdk.AuthError; }
|
|
1780
2089
|
});
|
|
2090
|
+
Object.defineProperty(exports, "MessageDiscardedError", {
|
|
2091
|
+
enumerable: true,
|
|
2092
|
+
get: function () { return sdk.MessageDiscardedError; }
|
|
2093
|
+
});
|
|
2094
|
+
Object.defineProperty(exports, "MessageQueuedError", {
|
|
2095
|
+
enumerable: true,
|
|
2096
|
+
get: function () { return sdk.MessageQueuedError; }
|
|
2097
|
+
});
|
|
1781
2098
|
Object.defineProperty(exports, "NetworkError", {
|
|
1782
2099
|
enumerable: true,
|
|
1783
2100
|
get: function () { return sdk.NetworkError; }
|
|
1784
2101
|
});
|
|
2102
|
+
Object.defineProperty(exports, "Poolse", {
|
|
2103
|
+
enumerable: true,
|
|
2104
|
+
get: function () { return sdk.Poolse; }
|
|
2105
|
+
});
|
|
1785
2106
|
Object.defineProperty(exports, "PoolseError", {
|
|
1786
2107
|
enumerable: true,
|
|
1787
2108
|
get: function () { return sdk.PoolseError; }
|
|
@@ -1794,6 +2115,10 @@ Object.defineProperty(exports, "callLogDisplayFor", {
|
|
|
1794
2115
|
enumerable: true,
|
|
1795
2116
|
get: function () { return sdk.callLogDisplayFor; }
|
|
1796
2117
|
});
|
|
2118
|
+
Object.defineProperty(exports, "createIndexedDbStorage", {
|
|
2119
|
+
enumerable: true,
|
|
2120
|
+
get: function () { return sdk.createIndexedDbStorage; }
|
|
2121
|
+
});
|
|
1797
2122
|
Object.defineProperty(exports, "describeCallLog", {
|
|
1798
2123
|
enumerable: true,
|
|
1799
2124
|
get: function () { return sdk.describeCallLog; }
|
|
@@ -1818,6 +2143,10 @@ Object.defineProperty(exports, "isTombstone", {
|
|
|
1818
2143
|
enumerable: true,
|
|
1819
2144
|
get: function () { return sdk.isTombstone; }
|
|
1820
2145
|
});
|
|
2146
|
+
Object.defineProperty(exports, "memoryStorage", {
|
|
2147
|
+
enumerable: true,
|
|
2148
|
+
get: function () { return sdk.memoryStorage; }
|
|
2149
|
+
});
|
|
1821
2150
|
Object.defineProperty(exports, "mergeMessage", {
|
|
1822
2151
|
enumerable: true,
|
|
1823
2152
|
get: function () { return sdk.mergeMessage; }
|
|
@@ -1838,7 +2167,9 @@ exports.EDIT_WINDOW_MS = EDIT_WINDOW_MS;
|
|
|
1838
2167
|
exports.PoolseProvider = PoolseProvider;
|
|
1839
2168
|
exports.canEditOwnMessage = canEditOwnMessage;
|
|
1840
2169
|
exports.computeGroupPosition = computeGroupPosition;
|
|
2170
|
+
exports.filterConversations = filterConversations;
|
|
1841
2171
|
exports.formatDayLabel = formatDayLabel;
|
|
2172
|
+
exports.orderUnread = orderUnread;
|
|
1842
2173
|
exports.receiptsOf = receiptsOf;
|
|
1843
2174
|
exports.sameDay = sameDay;
|
|
1844
2175
|
exports.sameGroup = sameGroup;
|
|
@@ -1846,6 +2177,7 @@ exports.useAppEvent = useAppEvent;
|
|
|
1846
2177
|
exports.useAttachmentUpload = useAttachmentUpload;
|
|
1847
2178
|
exports.useAttachmentUrl = useAttachmentUrl;
|
|
1848
2179
|
exports.useCallRoom = useCallRoom;
|
|
2180
|
+
exports.useClientTimings = useClientTimings;
|
|
1849
2181
|
exports.useConversation = useConversation;
|
|
1850
2182
|
exports.useConversationPresence = useConversationPresence;
|
|
1851
2183
|
exports.useConversations = useConversations;
|
|
@@ -1854,6 +2186,7 @@ exports.useMe = useMe;
|
|
|
1854
2186
|
exports.useMembers = useMembers;
|
|
1855
2187
|
exports.useMessageStreams = useMessageStreams;
|
|
1856
2188
|
exports.useMessages = useMessages;
|
|
2189
|
+
exports.useOutbox = useOutbox;
|
|
1857
2190
|
exports.usePoolse = usePoolse;
|
|
1858
2191
|
exports.usePresence = usePresence;
|
|
1859
2192
|
exports.usePresenceMany = usePresenceMany;
|
|
@@ -1863,6 +2196,8 @@ exports.useRealtimeStatus = useRealtimeStatus;
|
|
|
1863
2196
|
exports.useRoom = useRoom;
|
|
1864
2197
|
exports.useThread = useThread;
|
|
1865
2198
|
exports.useTyping = useTyping;
|
|
2199
|
+
exports.useUnreadQueue = useUnreadQueue;
|
|
2200
|
+
exports.useUnreadTotals = useUnreadTotals;
|
|
1866
2201
|
exports.useUser = useUser;
|
|
1867
2202
|
exports.useUserPresence = useUserPresence;
|
|
1868
2203
|
//# sourceMappingURL=index.cjs.map
|