@poolse/react 3.7.1 → 3.9.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/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
- void this.load(!this.loaded).catch(() => {
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({ config, trackActivity = true, children }) {
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 chat = react.useMemo(() => {
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
- chat.destroy();
332
+ void ownChat.destroy();
282
333
  };
283
- }, [chat]);
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: new ConversationsStore(chat) }), [chat]);
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
- function useConversation(id) {
340
- const chat = usePoolse();
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
- const [loading, setLoading] = react.useState(true);
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
- setLoading(true);
536
- setMessages([]);
752
+ let fresh = false;
753
+ const cached = chat.cache.peekMessages(conversationId);
754
+ rowsForRef.current = conversationId;
537
755
  setError(null);
538
756
  setHasMore(true);
539
- oldestSequenceRef.current = null;
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);
804
+ };
805
+ const syncOutbox = () => {
806
+ if (cancelled) return;
807
+ const entries = chat.outbox.entries(conversationId);
808
+ setMessages((prev) => applyOutbox(prev, entries));
559
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) setMessages((prev) => mergeLatest(prev, page.data));
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;
@@ -1459,6 +1768,7 @@ function useCallRoom(conversationId, opts) {
1459
1768
  const [activeDevices, setActiveDevices] = react.useState({});
1460
1769
  const [error, setError] = react.useState(null);
1461
1770
  const [sfuAvailable, setSfuAvailable] = react.useState(null);
1771
+ const [canPublish, setCanPublish] = react.useState(true);
1462
1772
  const optsRef = react.useRef(opts);
1463
1773
  optsRef.current = opts;
1464
1774
  const joined = react.useRef(false);
@@ -1470,7 +1780,8 @@ function useCallRoom(conversationId, opts) {
1470
1780
  ...optsRef.current.video !== void 0 ? { video: optsRef.current.video } : {},
1471
1781
  ...optsRef.current.muted !== void 0 ? { muted: optsRef.current.muted } : {},
1472
1782
  ...optsRef.current.facing !== void 0 ? { facing: optsRef.current.facing } : {},
1473
- ...optsRef.current.aloneGraceMs !== void 0 ? { aloneGraceMs: optsRef.current.aloneGraceMs } : {}
1783
+ ...optsRef.current.aloneGraceMs !== void 0 ? { aloneGraceMs: optsRef.current.aloneGraceMs } : {},
1784
+ ...optsRef.current.republishOnPermission !== void 0 ? { republishOnPermission: optsRef.current.republishOnPermission } : {}
1474
1785
  });
1475
1786
  }, [poolse, conversationId]);
1476
1787
  const refreshDevices = react.useCallback((target) => {
@@ -1495,6 +1806,7 @@ function useCallRoom(conversationId, opts) {
1495
1806
  setScreen(target.isScreenShareEnabled());
1496
1807
  setCanShare(target.canShareScreen());
1497
1808
  setFacing(target.getFacingMode());
1809
+ setCanPublish(target.canPublish());
1498
1810
  }, []);
1499
1811
  react.useEffect(() => {
1500
1812
  if (!room) {
@@ -1514,12 +1826,14 @@ function useCallRoom(conversationId, opts) {
1514
1826
  const offAlone = room.onAlone(() => setAlone(true));
1515
1827
  const offDevices = room.onDevices(() => refreshDevices(room));
1516
1828
  const offError = room.onError(setError);
1829
+ const offPublish = room.onPublishPermission(() => sync(room));
1517
1830
  return () => {
1518
1831
  offStatus();
1519
1832
  offRoster();
1520
1833
  offAlone();
1521
1834
  offDevices();
1522
1835
  offError();
1836
+ offPublish();
1523
1837
  held.current = null;
1524
1838
  if (joined.current) {
1525
1839
  joined.current = false;
@@ -1612,6 +1926,11 @@ function useCallRoom(conversationId, opts) {
1612
1926
  },
1613
1927
  [room]
1614
1928
  );
1929
+ const republish = react.useCallback(async () => {
1930
+ if (!room) return;
1931
+ await room.republish();
1932
+ sync(room);
1933
+ }, [room, sync]);
1615
1934
  return {
1616
1935
  status,
1617
1936
  participants,
@@ -1636,9 +1955,110 @@ function useCallRoom(conversationId, opts) {
1636
1955
  setMicrophoneEnabled,
1637
1956
  toggleMic,
1638
1957
  setScreenShareEnabled,
1639
- toggleScreenShare
1958
+ toggleScreenShare,
1959
+ canPublish,
1960
+ republish
1640
1961
  };
1641
1962
  }
1963
+ var IDLE_RECORDING = {
1964
+ available: true,
1965
+ active: false,
1966
+ id: null,
1967
+ mode: null,
1968
+ started_at: null,
1969
+ started_by_user_id: null,
1970
+ auto: false,
1971
+ consent_required: false,
1972
+ consent: { accepted: [], declined: [] }
1973
+ };
1974
+ var without = (list, id) => list.filter((x) => x !== id);
1975
+ function applyCaptureEvent(state, e) {
1976
+ const rec = state.recording ?? IDLE_RECORDING;
1977
+ switch (e.type) {
1978
+ case "recording:started": {
1979
+ const r = e.evt.recording;
1980
+ const starter = r.started_by_user_id;
1981
+ return {
1982
+ ...state,
1983
+ recording: {
1984
+ ...rec,
1985
+ active: true,
1986
+ id: r.id,
1987
+ mode: r.mode,
1988
+ started_at: r.started_at,
1989
+ started_by_user_id: starter,
1990
+ auto: r.auto,
1991
+ consent_required: e.evt.consent_required,
1992
+ // Starting is accepting.
1993
+ consent: starter ? {
1994
+ accepted: [...without(rec.consent.accepted, starter), starter],
1995
+ declined: without(rec.consent.declined, starter)
1996
+ } : rec.consent
1997
+ }
1998
+ };
1999
+ }
2000
+ case "recording:stopped":
2001
+ case "recording:ready":
2002
+ if (rec.id !== null && rec.id !== e.evt.recording.id) return state;
2003
+ return {
2004
+ ...state,
2005
+ recording: { ...rec, active: false, id: null, mode: null, consent_required: false }
2006
+ };
2007
+ case "recording:consent": {
2008
+ const { user_id: who, decision } = e.evt;
2009
+ const accepted = without(rec.consent.accepted, who);
2010
+ const declined = without(rec.consent.declined, who);
2011
+ (decision === "accepted" ? accepted : declined).push(who);
2012
+ return { ...state, recording: { ...rec, consent: { accepted, declined } } };
2013
+ }
2014
+ case "transcript:changed": {
2015
+ const t = e.evt.transcript;
2016
+ return {
2017
+ ...state,
2018
+ transcription: {
2019
+ available: state.transcription?.available ?? true,
2020
+ active: t.status === "live",
2021
+ id: t.id,
2022
+ status: t.status,
2023
+ language: t.language
2024
+ }
2025
+ };
2026
+ }
2027
+ }
2028
+ }
2029
+ function subscribeCapture(chat, conversationId, callId, fn) {
2030
+ const conv = chat.realtime.conversation(conversationId);
2031
+ const mine = (id) => callId === null || id === callId;
2032
+ const offs = [
2033
+ conv.onRecordingStarted((evt) => mine(evt.call_id) && fn({ type: "recording:started", evt })),
2034
+ conv.onRecordingStopped((evt) => mine(evt.call_id) && fn({ type: "recording:stopped", evt })),
2035
+ conv.onRecordingReady((evt) => mine(evt.call_id) && fn({ type: "recording:ready", evt })),
2036
+ conv.onRecordingConsent((evt) => mine(evt.call_id) && fn({ type: "recording:consent", evt })),
2037
+ conv.onTranscriptStarted((evt) => mine(evt.call_id) && fn({ type: "transcript:changed", evt })),
2038
+ conv.onTranscriptStopped((evt) => mine(evt.call_id) && fn({ type: "transcript:changed", evt })),
2039
+ conv.onTranscriptReady((evt) => mine(evt.call_id) && fn({ type: "transcript:changed", evt }))
2040
+ ];
2041
+ return () => offs.forEach((off) => off());
2042
+ }
2043
+ function useMeId(chat) {
2044
+ const [id, setId] = react.useState(() => chat.me.peek()?.id ?? null);
2045
+ react.useEffect(() => {
2046
+ setId(chat.me.peek()?.id ?? null);
2047
+ return chat.me.onChange((me) => setId(me.id));
2048
+ }, [chat]);
2049
+ return id;
2050
+ }
2051
+ function onReconnect(chat, fn) {
2052
+ let wasConnected = chat.realtime.getStatus() === "connected";
2053
+ let connectedBefore = wasConnected;
2054
+ return chat.realtime.onStatus((status) => {
2055
+ if (status === "connected" && !wasConnected && connectedBefore) fn();
2056
+ wasConnected = status === "connected";
2057
+ connectedBefore ||= wasConnected;
2058
+ });
2059
+ }
2060
+
2061
+ // src/use-live-call.ts
1642
2062
  function useLiveCall(conversationId) {
1643
2063
  const chat = usePoolse();
1644
2064
  const [call, setCall] = react.useState(null);
@@ -1671,6 +2091,21 @@ function useLiveCall(conversationId) {
1671
2091
  if (evt.call_id === callIdRef.current) setParticipantIds(evt.participants);
1672
2092
  else refresh();
1673
2093
  });
2094
+ const offCapture = subscribeCapture(chat, conversationId, null, (e) => {
2095
+ if (e.evt.call_id !== callIdRef.current) return;
2096
+ setCall((c) => {
2097
+ if (!c) return c;
2098
+ const next = applyCaptureEvent(
2099
+ { recording: c.recording ?? null, transcription: c.transcription ?? null },
2100
+ e
2101
+ );
2102
+ return {
2103
+ ...c,
2104
+ ...next.recording ? { recording: next.recording } : {},
2105
+ ...next.transcription ? { transcription: next.transcription } : {}
2106
+ };
2107
+ });
2108
+ });
1674
2109
  const offFinished = conv.onCallFinished((evt) => {
1675
2110
  if (evt.call_id !== callIdRef.current) return;
1676
2111
  seq.current++;
@@ -1687,6 +2122,7 @@ function useLiveCall(conversationId) {
1687
2122
  offStarted();
1688
2123
  offParticipants();
1689
2124
  offFinished();
2125
+ offCapture();
1690
2126
  offStatus();
1691
2127
  };
1692
2128
  }, [chat, conversationId, apply, refresh]);
@@ -1741,6 +2177,392 @@ function useRoom(conversationId, opts) {
1741
2177
  error: error ?? media.error
1742
2178
  };
1743
2179
  }
2180
+ function useRecording(callId, conversationId, initial) {
2181
+ const chat = usePoolse();
2182
+ const meId = useMeId(chat);
2183
+ const [state, setState] = react.useState(
2184
+ () => initial && initial.id === callId ? { recording: initial.recording ?? null, transcription: initial.transcription ?? null } : { recording: null, transcription: null }
2185
+ );
2186
+ const [notice, setNotice] = react.useState(null);
2187
+ const [lastRecording, setLastRecording] = react.useState(null);
2188
+ const [pending, setPending] = react.useState(false);
2189
+ const [error, setError] = react.useState(null);
2190
+ const [transcriptConsent, setTranscriptConsent] = react.useState(false);
2191
+ const seq = react.useRef(0);
2192
+ const hasInitial = react.useRef(!!initial && initial.id === callId);
2193
+ const refresh = react.useCallback(() => {
2194
+ if (!callId || !conversationId) return;
2195
+ const mine = ++seq.current;
2196
+ chat.liveCalls.get(conversationId).then((c) => {
2197
+ if (mine !== seq.current) return;
2198
+ if (c && c.id === callId)
2199
+ setState({ recording: c.recording ?? null, transcription: c.transcription ?? null });
2200
+ else
2201
+ setState((s) => ({
2202
+ recording: s.recording ? { ...s.recording, active: false } : null,
2203
+ transcription: s.transcription ? { ...s.transcription, active: false } : null
2204
+ }));
2205
+ }).catch(() => {
2206
+ });
2207
+ }, [chat, callId, conversationId]);
2208
+ react.useEffect(() => {
2209
+ if (!callId || !conversationId) {
2210
+ setState({ recording: null, transcription: null });
2211
+ return;
2212
+ }
2213
+ if (hasInitial.current) hasInitial.current = false;
2214
+ else refresh();
2215
+ const offCapture = subscribeCapture(chat, conversationId, callId, (e) => {
2216
+ seq.current++;
2217
+ setState((s) => applyCaptureEvent(s, e));
2218
+ if (e.type === "recording:started") {
2219
+ setNotice(e.evt.notice);
2220
+ setLastRecording(e.evt.recording);
2221
+ } else if (e.type === "recording:stopped" || e.type === "recording:ready") {
2222
+ setLastRecording(e.evt.recording);
2223
+ }
2224
+ });
2225
+ const conv = chat.realtime.conversation(conversationId);
2226
+ const offFinished = conv.onCallFinished((evt) => {
2227
+ if (evt.call_id !== callId) return;
2228
+ seq.current++;
2229
+ setState((s) => ({
2230
+ recording: s.recording ? { ...s.recording, active: false } : null,
2231
+ transcription: s.transcription ? { ...s.transcription, active: false } : null
2232
+ }));
2233
+ });
2234
+ const offStatus = onReconnect(chat, refresh);
2235
+ return () => {
2236
+ offCapture();
2237
+ offFinished();
2238
+ offStatus();
2239
+ };
2240
+ }, [chat, callId, conversationId, refresh]);
2241
+ const transcribing = !!state.transcription?.active;
2242
+ react.useEffect(() => {
2243
+ if (!conversationId || !transcribing) {
2244
+ setTranscriptConsent(false);
2245
+ return;
2246
+ }
2247
+ let cancelled = false;
2248
+ chat.calls.recording.settings(conversationId).then((s) => {
2249
+ if (!cancelled) setTranscriptConsent(s.effective.consent === "required");
2250
+ }).catch(() => {
2251
+ });
2252
+ return () => {
2253
+ cancelled = true;
2254
+ };
2255
+ }, [chat, conversationId, transcribing]);
2256
+ const run = react.useCallback(async (fn) => {
2257
+ setPending(true);
2258
+ setError(null);
2259
+ try {
2260
+ await fn();
2261
+ return true;
2262
+ } catch (err) {
2263
+ setError(err instanceof Error ? err : new Error(String(err)));
2264
+ return false;
2265
+ } finally {
2266
+ setPending(false);
2267
+ }
2268
+ }, []);
2269
+ const start = react.useCallback(
2270
+ (mode) => run(async () => {
2271
+ if (!callId) throw new Error("no call");
2272
+ const r = await chat.calls.recording.start(callId, mode ? { mode } : {});
2273
+ setLastRecording(r);
2274
+ }),
2275
+ [chat, callId, run]
2276
+ );
2277
+ const stop = react.useCallback(
2278
+ () => run(async () => {
2279
+ if (!callId) throw new Error("no call");
2280
+ const r = await chat.calls.recording.stop(callId);
2281
+ setLastRecording(r);
2282
+ }),
2283
+ [chat, callId, run]
2284
+ );
2285
+ const consent = react.useCallback(
2286
+ (decision) => run(async () => {
2287
+ if (!callId) throw new Error("no call");
2288
+ const res = await chat.calls.consent(callId, decision);
2289
+ setState(
2290
+ (s) => applyCaptureEvent(s, {
2291
+ type: "recording:consent",
2292
+ evt: { cv: 1, call_id: res.call_id, user_id: res.user_id, decision: res.decision }
2293
+ })
2294
+ );
2295
+ }),
2296
+ [chat, callId, run]
2297
+ );
2298
+ const rec = state.recording;
2299
+ const active = !!rec?.active;
2300
+ const capturing = active || transcribing;
2301
+ const consentRequired = active && !!rec?.consent_required || transcribing && transcriptConsent;
2302
+ const myConsent = meId && rec ? rec.consent.accepted.includes(meId) || active && rec.started_by_user_id === meId ? "accepted" : rec.consent.declined.includes(meId) ? "declined" : null : null;
2303
+ return {
2304
+ available: rec?.available ?? false,
2305
+ active,
2306
+ recording: rec,
2307
+ transcription: state.transcription,
2308
+ capturing,
2309
+ consentRequired,
2310
+ myConsent,
2311
+ needsConsent: capturing && consentRequired && myConsent !== "accepted",
2312
+ notice,
2313
+ lastRecording,
2314
+ pending,
2315
+ error,
2316
+ start,
2317
+ stop,
2318
+ consent,
2319
+ refresh
2320
+ };
2321
+ }
2322
+ function useLiveTranscript(callId, opts = {}) {
2323
+ const chat = usePoolse();
2324
+ const enabled = opts.enabled ?? true;
2325
+ const max = opts.maxSegments ?? 1e3;
2326
+ const [segments, setSegments] = react.useState([]);
2327
+ const [transcript, setTranscript] = react.useState(null);
2328
+ const [conversationId, setConversationId] = react.useState(null);
2329
+ const [joined, setJoined] = react.useState(false);
2330
+ const [error, setError] = react.useState(null);
2331
+ const [pending, setPending] = react.useState(false);
2332
+ react.useEffect(() => {
2333
+ setSegments([]);
2334
+ setTranscript(null);
2335
+ setJoined(false);
2336
+ setError(null);
2337
+ if (!callId || !enabled) return;
2338
+ let handle;
2339
+ try {
2340
+ handle = chat.realtime.transcript(callId);
2341
+ } catch (err) {
2342
+ setError(err instanceof Error ? err : new Error(String(err)));
2343
+ return;
2344
+ }
2345
+ const offJoined = handle.onJoined((reply) => {
2346
+ setJoined(true);
2347
+ setError(null);
2348
+ setConversationId(reply.conversation_id || null);
2349
+ setTranscript(reply.transcript);
2350
+ setSegments(reply.segments.slice(-max).map((s) => ({ ...s, final: true })));
2351
+ });
2352
+ const offSegment = handle.onSegment((evt) => {
2353
+ const { cv: _cv, call_id: _call, transcript_id: _t, ...seg } = evt;
2354
+ setSegments((list) => sdk.applyLiveSegment(list, seg, max));
2355
+ });
2356
+ const offError = handle.onError((reason) => {
2357
+ setJoined(false);
2358
+ setError(new Error(reason));
2359
+ });
2360
+ return () => {
2361
+ offJoined();
2362
+ offSegment();
2363
+ offError();
2364
+ handle.release();
2365
+ };
2366
+ }, [chat, callId, enabled, max]);
2367
+ react.useEffect(() => {
2368
+ if (!callId || !conversationId || !enabled) return;
2369
+ const conv = chat.realtime.conversation(conversationId);
2370
+ const apply = (evt) => {
2371
+ if (evt.call_id === callId) setTranscript(evt.transcript);
2372
+ };
2373
+ const offs = [
2374
+ conv.onTranscriptStarted(apply),
2375
+ conv.onTranscriptStopped(apply),
2376
+ conv.onTranscriptReady(apply),
2377
+ conv.onTranscriptSpeakers((evt) => {
2378
+ if (evt.call_id !== callId) return;
2379
+ const names = new Map(evt.speakers.map((s) => [s.key, s.name]));
2380
+ setSegments(
2381
+ (list) => list.map(
2382
+ (s) => names.has(s.speaker) ? { ...s, speaker_name: names.get(s.speaker) } : s
2383
+ )
2384
+ );
2385
+ })
2386
+ ];
2387
+ return () => offs.forEach((off) => off());
2388
+ }, [chat, callId, conversationId, enabled]);
2389
+ const run = react.useCallback(async (fn) => {
2390
+ setPending(true);
2391
+ setError(null);
2392
+ try {
2393
+ setTranscript(await fn());
2394
+ return true;
2395
+ } catch (err) {
2396
+ setError(err instanceof Error ? err : new Error(String(err)));
2397
+ return false;
2398
+ } finally {
2399
+ setPending(false);
2400
+ }
2401
+ }, []);
2402
+ const start = react.useCallback(
2403
+ () => callId ? run(() => chat.calls.transcription.start(callId)) : Promise.resolve(false),
2404
+ [chat, callId, run]
2405
+ );
2406
+ const stop = react.useCallback(
2407
+ () => callId ? run(() => chat.calls.transcription.stop(callId)) : Promise.resolve(false),
2408
+ [chat, callId, run]
2409
+ );
2410
+ const finals = react.useMemo(() => segments.filter((s) => s.final), [segments]);
2411
+ const interim = react.useMemo(() => segments.filter((s) => !s.final), [segments]);
2412
+ return {
2413
+ segments,
2414
+ finals,
2415
+ interim,
2416
+ transcript,
2417
+ live: transcript?.status === "live",
2418
+ joined,
2419
+ error,
2420
+ pending,
2421
+ start,
2422
+ stop
2423
+ };
2424
+ }
2425
+ function withSpeakers(t, speakers) {
2426
+ const names = new Map(speakers.map((s) => [s.key, s.name]));
2427
+ return {
2428
+ ...t,
2429
+ speakers,
2430
+ segments: t.segments.map(
2431
+ (s) => names.has(s.speaker) ? { ...s, speaker_name: names.get(s.speaker) ?? null } : s
2432
+ )
2433
+ };
2434
+ }
2435
+ function useTranscript(callId) {
2436
+ const chat = usePoolse();
2437
+ const [transcript, setTranscript] = react.useState(null);
2438
+ const [loading, setLoading] = react.useState(!!callId);
2439
+ const [missing, setMissing] = react.useState(false);
2440
+ const [error, setError] = react.useState(null);
2441
+ const seq = react.useRef(0);
2442
+ const refresh = react.useCallback(() => {
2443
+ if (!callId) return;
2444
+ const mine = ++seq.current;
2445
+ setLoading(true);
2446
+ chat.calls.transcript.get(callId).then((t) => {
2447
+ if (mine !== seq.current) return;
2448
+ setTranscript(t);
2449
+ setMissing(false);
2450
+ setError(null);
2451
+ }).catch((err) => {
2452
+ if (mine !== seq.current) return;
2453
+ if (err instanceof sdk.ApiError && err.status === 404) {
2454
+ setMissing(true);
2455
+ setTranscript(null);
2456
+ } else setError(err instanceof Error ? err : new Error(String(err)));
2457
+ }).finally(() => {
2458
+ if (mine === seq.current) setLoading(false);
2459
+ });
2460
+ }, [chat, callId]);
2461
+ react.useEffect(() => {
2462
+ setTranscript(null);
2463
+ setMissing(false);
2464
+ setError(null);
2465
+ setLoading(!!callId);
2466
+ refresh();
2467
+ }, [callId, refresh]);
2468
+ const conversationId = transcript?.conversation_id ?? null;
2469
+ react.useEffect(() => {
2470
+ if (!callId || !conversationId) return;
2471
+ const conv = chat.realtime.conversation(conversationId);
2472
+ const offSpeakers = conv.onTranscriptSpeakers((evt) => {
2473
+ if (evt.call_id !== callId) return;
2474
+ seq.current++;
2475
+ setTranscript((t) => t ? withSpeakers(t, evt.speakers) : t);
2476
+ });
2477
+ const offReady = conv.onTranscriptReady((evt) => {
2478
+ if (evt.call_id === callId) refresh();
2479
+ });
2480
+ const offStatus = onReconnect(chat, refresh);
2481
+ return () => {
2482
+ offSpeakers();
2483
+ offReady();
2484
+ offStatus();
2485
+ };
2486
+ }, [chat, callId, conversationId, refresh]);
2487
+ const renameSpeakers = react.useCallback(
2488
+ async (names) => {
2489
+ if (!callId) return;
2490
+ const t = await chat.calls.transcript.renameSpeakers(callId, names);
2491
+ seq.current++;
2492
+ setTranscript(t);
2493
+ },
2494
+ [chat, callId]
2495
+ );
2496
+ return { transcript, loading, missing, error, renameSpeakers, refresh };
2497
+ }
2498
+ function useCaptureList(conversationId, load, subscribe) {
2499
+ const chat = usePoolse();
2500
+ const [items, setItems] = react.useState([]);
2501
+ const [loading, setLoading] = react.useState(!!conversationId);
2502
+ const [error, setError] = react.useState(null);
2503
+ const seq = react.useRef(0);
2504
+ const loadRef = react.useRef(load);
2505
+ loadRef.current = load;
2506
+ const subscribeRef = react.useRef(subscribe);
2507
+ subscribeRef.current = subscribe;
2508
+ const refresh = react.useCallback(() => {
2509
+ if (!conversationId) return;
2510
+ const mine = ++seq.current;
2511
+ setLoading(true);
2512
+ loadRef.current(conversationId).then((list) => {
2513
+ if (mine !== seq.current) return;
2514
+ setItems(list);
2515
+ setError(null);
2516
+ }).catch((err) => {
2517
+ if (mine === seq.current) setError(err instanceof Error ? err : new Error(String(err)));
2518
+ }).finally(() => {
2519
+ if (mine === seq.current) setLoading(false);
2520
+ });
2521
+ }, [conversationId]);
2522
+ react.useEffect(() => {
2523
+ setItems([]);
2524
+ if (!conversationId) {
2525
+ setLoading(false);
2526
+ return;
2527
+ }
2528
+ refresh();
2529
+ const off = subscribeRef.current(conversationId, refresh);
2530
+ const offStatus = onReconnect(chat, refresh);
2531
+ return () => {
2532
+ off();
2533
+ offStatus();
2534
+ };
2535
+ }, [chat, conversationId, refresh]);
2536
+ return { items, loading, error, refresh };
2537
+ }
2538
+ function useTranscripts(conversationId, opts = {}) {
2539
+ const chat = usePoolse();
2540
+ return useCaptureList(
2541
+ conversationId,
2542
+ (id) => chat.calls.transcript.list(id, opts.limit !== void 0 ? { limit: opts.limit } : {}),
2543
+ (id, refresh) => {
2544
+ const conv = chat.realtime.conversation(id);
2545
+ const offs = [conv.onTranscriptStarted(refresh), conv.onTranscriptReady(refresh)];
2546
+ return () => offs.forEach((off) => off());
2547
+ }
2548
+ );
2549
+ }
2550
+ function useRecordings(conversationId, opts = {}) {
2551
+ const chat = usePoolse();
2552
+ return useCaptureList(
2553
+ conversationId,
2554
+ (id) => chat.calls.recording.list(id, opts.limit !== void 0 ? { limit: opts.limit } : {}),
2555
+ (id, refresh) => {
2556
+ const conv = chat.realtime.conversation(id);
2557
+ const offs = [
2558
+ conv.onRecordingStarted(refresh),
2559
+ conv.onRecordingStopped(refresh),
2560
+ conv.onRecordingReady(refresh)
2561
+ ];
2562
+ return () => offs.forEach((off) => off());
2563
+ }
2564
+ );
2565
+ }
1744
2566
  function useReadReceipts() {
1745
2567
  const chat = usePoolse();
1746
2568
  const { me } = useMe();
@@ -1778,10 +2600,26 @@ Object.defineProperty(exports, "AuthError", {
1778
2600
  enumerable: true,
1779
2601
  get: function () { return sdk.AuthError; }
1780
2602
  });
2603
+ Object.defineProperty(exports, "MessageDiscardedError", {
2604
+ enumerable: true,
2605
+ get: function () { return sdk.MessageDiscardedError; }
2606
+ });
2607
+ Object.defineProperty(exports, "MessageQueuedError", {
2608
+ enumerable: true,
2609
+ get: function () { return sdk.MessageQueuedError; }
2610
+ });
1781
2611
  Object.defineProperty(exports, "NetworkError", {
1782
2612
  enumerable: true,
1783
2613
  get: function () { return sdk.NetworkError; }
1784
2614
  });
2615
+ Object.defineProperty(exports, "POOLSE_SERVICE_IDENTITY_PREFIX", {
2616
+ enumerable: true,
2617
+ get: function () { return sdk.POOLSE_SERVICE_IDENTITY_PREFIX; }
2618
+ });
2619
+ Object.defineProperty(exports, "Poolse", {
2620
+ enumerable: true,
2621
+ get: function () { return sdk.Poolse; }
2622
+ });
1785
2623
  Object.defineProperty(exports, "PoolseError", {
1786
2624
  enumerable: true,
1787
2625
  get: function () { return sdk.PoolseError; }
@@ -1790,14 +2628,38 @@ Object.defineProperty(exports, "RateLimitedError", {
1790
2628
  enumerable: true,
1791
2629
  get: function () { return sdk.RateLimitedError; }
1792
2630
  });
2631
+ Object.defineProperty(exports, "applyLiveSegment", {
2632
+ enumerable: true,
2633
+ get: function () { return sdk.applyLiveSegment; }
2634
+ });
1793
2635
  Object.defineProperty(exports, "callLogDisplayFor", {
1794
2636
  enumerable: true,
1795
2637
  get: function () { return sdk.callLogDisplayFor; }
1796
2638
  });
2639
+ Object.defineProperty(exports, "captureErrorMessage", {
2640
+ enumerable: true,
2641
+ get: function () { return sdk.captureErrorMessage; }
2642
+ });
2643
+ Object.defineProperty(exports, "captureLabel", {
2644
+ enumerable: true,
2645
+ get: function () { return sdk.captureLabel; }
2646
+ });
2647
+ Object.defineProperty(exports, "createIndexedDbStorage", {
2648
+ enumerable: true,
2649
+ get: function () { return sdk.createIndexedDbStorage; }
2650
+ });
1797
2651
  Object.defineProperty(exports, "describeCallLog", {
1798
2652
  enumerable: true,
1799
2653
  get: function () { return sdk.describeCallLog; }
1800
2654
  });
2655
+ Object.defineProperty(exports, "describeRecordingNotice", {
2656
+ enumerable: true,
2657
+ get: function () { return sdk.describeRecordingNotice; }
2658
+ });
2659
+ Object.defineProperty(exports, "findInText", {
2660
+ enumerable: true,
2661
+ get: function () { return sdk.findInText; }
2662
+ });
1801
2663
  Object.defineProperty(exports, "formatCallDuration", {
1802
2664
  enumerable: true,
1803
2665
  get: function () { return sdk.formatCallDuration; }
@@ -1806,6 +2668,14 @@ Object.defineProperty(exports, "formatCallLogPreview", {
1806
2668
  enumerable: true,
1807
2669
  get: function () { return sdk.formatCallLogPreview; }
1808
2670
  });
2671
+ Object.defineProperty(exports, "formatRecordingNoticePreview", {
2672
+ enumerable: true,
2673
+ get: function () { return sdk.formatRecordingNoticePreview; }
2674
+ });
2675
+ Object.defineProperty(exports, "groupTranscript", {
2676
+ enumerable: true,
2677
+ get: function () { return sdk.groupTranscript; }
2678
+ });
1809
2679
  Object.defineProperty(exports, "isCallLogMessage", {
1810
2680
  enumerable: true,
1811
2681
  get: function () { return sdk.isCallLogMessage; }
@@ -1814,10 +2684,18 @@ Object.defineProperty(exports, "isMessageVisibleTo", {
1814
2684
  enumerable: true,
1815
2685
  get: function () { return sdk.isMessageVisibleTo; }
1816
2686
  });
2687
+ Object.defineProperty(exports, "isServiceParticipant", {
2688
+ enumerable: true,
2689
+ get: function () { return sdk.isServiceParticipant; }
2690
+ });
1817
2691
  Object.defineProperty(exports, "isTombstone", {
1818
2692
  enumerable: true,
1819
2693
  get: function () { return sdk.isTombstone; }
1820
2694
  });
2695
+ Object.defineProperty(exports, "memoryStorage", {
2696
+ enumerable: true,
2697
+ get: function () { return sdk.memoryStorage; }
2698
+ });
1821
2699
  Object.defineProperty(exports, "mergeMessage", {
1822
2700
  enumerable: true,
1823
2701
  get: function () { return sdk.mergeMessage; }
@@ -1830,15 +2708,34 @@ Object.defineProperty(exports, "parseCallLogBody", {
1830
2708
  enumerable: true,
1831
2709
  get: function () { return sdk.parseCallLogBody; }
1832
2710
  });
2711
+ Object.defineProperty(exports, "parseRecordingNotice", {
2712
+ enumerable: true,
2713
+ get: function () { return sdk.parseRecordingNotice; }
2714
+ });
2715
+ Object.defineProperty(exports, "parseRecordingNoticeBody", {
2716
+ enumerable: true,
2717
+ get: function () { return sdk.parseRecordingNoticeBody; }
2718
+ });
2719
+ Object.defineProperty(exports, "recordingNoticeDisplayFor", {
2720
+ enumerable: true,
2721
+ get: function () { return sdk.recordingNoticeDisplayFor; }
2722
+ });
2723
+ Object.defineProperty(exports, "speakerName", {
2724
+ enumerable: true,
2725
+ get: function () { return sdk.speakerName; }
2726
+ });
1833
2727
  Object.defineProperty(exports, "toTombstone", {
1834
2728
  enumerable: true,
1835
2729
  get: function () { return sdk.toTombstone; }
1836
2730
  });
1837
2731
  exports.EDIT_WINDOW_MS = EDIT_WINDOW_MS;
1838
2732
  exports.PoolseProvider = PoolseProvider;
2733
+ exports.applyCaptureEvent = applyCaptureEvent;
1839
2734
  exports.canEditOwnMessage = canEditOwnMessage;
1840
2735
  exports.computeGroupPosition = computeGroupPosition;
2736
+ exports.filterConversations = filterConversations;
1841
2737
  exports.formatDayLabel = formatDayLabel;
2738
+ exports.orderUnread = orderUnread;
1842
2739
  exports.receiptsOf = receiptsOf;
1843
2740
  exports.sameDay = sameDay;
1844
2741
  exports.sameGroup = sameGroup;
@@ -1846,23 +2743,32 @@ exports.useAppEvent = useAppEvent;
1846
2743
  exports.useAttachmentUpload = useAttachmentUpload;
1847
2744
  exports.useAttachmentUrl = useAttachmentUrl;
1848
2745
  exports.useCallRoom = useCallRoom;
2746
+ exports.useClientTimings = useClientTimings;
1849
2747
  exports.useConversation = useConversation;
1850
2748
  exports.useConversationPresence = useConversationPresence;
1851
2749
  exports.useConversations = useConversations;
1852
2750
  exports.useLiveCall = useLiveCall;
2751
+ exports.useLiveTranscript = useLiveTranscript;
1853
2752
  exports.useMe = useMe;
1854
2753
  exports.useMembers = useMembers;
1855
2754
  exports.useMessageStreams = useMessageStreams;
1856
2755
  exports.useMessages = useMessages;
2756
+ exports.useOutbox = useOutbox;
1857
2757
  exports.usePoolse = usePoolse;
1858
2758
  exports.usePresence = usePresence;
1859
2759
  exports.usePresenceMany = usePresenceMany;
1860
2760
  exports.useReactions = useReactions;
1861
2761
  exports.useReadReceipts = useReadReceipts;
1862
2762
  exports.useRealtimeStatus = useRealtimeStatus;
2763
+ exports.useRecording = useRecording;
2764
+ exports.useRecordings = useRecordings;
1863
2765
  exports.useRoom = useRoom;
1864
2766
  exports.useThread = useThread;
2767
+ exports.useTranscript = useTranscript;
2768
+ exports.useTranscripts = useTranscripts;
1865
2769
  exports.useTyping = useTyping;
2770
+ exports.useUnreadQueue = useUnreadQueue;
2771
+ exports.useUnreadTotals = useUnreadTotals;
1866
2772
  exports.useUser = useUser;
1867
2773
  exports.useUserPresence = useUserPresence;
1868
2774
  //# sourceMappingURL=index.cjs.map