@poolse/react 3.2.0 → 3.3.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 +22 -0
- package/README.md +27 -20
- package/dist/index.cjs +260 -96
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +56 -22
- package/dist/index.d.ts +56 -22
- package/dist/index.js +260 -97
- package/dist/index.js.map +1 -1
- package/package.json +10 -10
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,28 @@ All notable changes to `@poolse/react` are documented here. Format follows
|
|
|
4
4
|
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/); versions follow
|
|
5
5
|
[semver](https://semver.org).
|
|
6
6
|
|
|
7
|
+
## [3.3.0] - 2026-11-20
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- `useConversations().mentionUnreadCounts`.
|
|
12
|
+
- `useMessages().loadUntil(messageId, maxPages?)`: pages back until a message
|
|
13
|
+
is loaded; resolves whether it was found.
|
|
14
|
+
- `useAppEvent(handler)` for `app:event`.
|
|
15
|
+
- Re-exported types `AppEvent`, `ConversationReadEvent`, `NotifyLevel`,
|
|
16
|
+
`NotificationPreferences`, `OwnMembership`, `OwnMembershipUpdateRequest`.
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- `useConversations()` state is shared by every caller under one
|
|
21
|
+
`<PoolseProvider>` (one fetch, one list). `markConversationRead` and a
|
|
22
|
+
successful `useMessages().markReadUpTo` zero the unread and mention counts
|
|
23
|
+
in every view; `conversation:read` from the user channel applies the
|
|
24
|
+
server's counts. A remount shows the rows it had at once and refreshes
|
|
25
|
+
quietly.
|
|
26
|
+
- `useUser` keeps the previous profile on screen while an invalidated one
|
|
27
|
+
re-resolves.
|
|
28
|
+
|
|
7
29
|
## [3.2.0] — 2026-11-17
|
|
8
30
|
|
|
9
31
|
Stable release of everything in the 3.2.0 prereleases below (streaming agent replies, screen sharing, full screen, the redesigned call screen, screen sharing from iOS).
|
package/README.md
CHANGED
|
@@ -62,26 +62,27 @@ new `key` to switch user.
|
|
|
62
62
|
|
|
63
63
|
## Hooks
|
|
64
64
|
|
|
65
|
-
| Hook | Returns
|
|
66
|
-
| -------------------------------------------------------------------------------- |
|
|
67
|
-
| `usePoolse()` | The `Poolse` instance.
|
|
68
|
-
| `useMe()` | `{ me, loading, error }`
|
|
69
|
-
| `useConversation(id \| null)` | `{ conversation, loading, error, refetch }`
|
|
70
|
-
| `useConversations()` | `{ conversations, loading, error, refetch, create, unreadCounts, markConversationRead }`
|
|
71
|
-
| `useMessages(conversationId)` | `{ messages, loading, error, hasMore, loadMore, send, edit, delete, markReadUpTo, refetch }`, oldest first
|
|
72
|
-
| `useThread(conversationId, rootId)` | `{ replies, loading, error, hasMore, loadMore, sendReply, edit, delete }`
|
|
73
|
-
| `useReactions(messageId, { conversationId, initialReactions?, currentUserId? })` | `{ reactions, addReaction, removeReaction }`
|
|
74
|
-
| `useTyping(conversationId)` | `{ typing: Set<externalId>, signalTyping }`
|
|
75
|
-
| `usePresence(conversationId)` | `{ online: Set<externalId> }`
|
|
76
|
-
| `useMembers(conversationId)` | `{ members, loading, error, refetch, addMembers(externalIds, { role? }), removeMember(externalId) }`
|
|
77
|
-
| `useAttachmentUpload()` | `{ queue, attachment, uploading, error, upload(input), uploadAll(inputs), cancel(localId), remove(localId), reset }`
|
|
78
|
-
| `useAttachmentUrl(id \| null)` | `{ url, loading, error }` (not renewed; URLs last one hour)
|
|
79
|
-
| `useUser(externalId)` | `{ profile, loading }`
|
|
80
|
-
| `
|
|
81
|
-
| `
|
|
82
|
-
| `
|
|
83
|
-
| `
|
|
84
|
-
| `
|
|
65
|
+
| Hook | Returns |
|
|
66
|
+
| -------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
|
67
|
+
| `usePoolse()` | The `Poolse` instance. |
|
|
68
|
+
| `useMe()` | `{ me, loading, error }` |
|
|
69
|
+
| `useConversation(id \| null)` | `{ conversation, loading, error, refetch }` |
|
|
70
|
+
| `useConversations()` | `{ conversations, loading, error, refetch, create, unreadCounts, mentionUnreadCounts, markConversationRead }` |
|
|
71
|
+
| `useMessages(conversationId)` | `{ messages, loading, error, hasMore, loadMore, loadUntil(messageId), send, edit, delete, markReadUpTo, refetch }`, oldest first |
|
|
72
|
+
| `useThread(conversationId, rootId)` | `{ replies, loading, error, hasMore, loadMore, sendReply, edit, delete }` |
|
|
73
|
+
| `useReactions(messageId, { conversationId, initialReactions?, currentUserId? })` | `{ reactions, addReaction, removeReaction }` |
|
|
74
|
+
| `useTyping(conversationId)` | `{ typing: Set<externalId>, signalTyping }` |
|
|
75
|
+
| `usePresence(conversationId)` | `{ online: Set<externalId> }` |
|
|
76
|
+
| `useMembers(conversationId)` | `{ members, loading, error, refetch, addMembers(externalIds, { role? }), removeMember(externalId) }` |
|
|
77
|
+
| `useAttachmentUpload()` | `{ queue, attachment, uploading, error, upload(input), uploadAll(inputs), cancel(localId), remove(localId), reset }` |
|
|
78
|
+
| `useAttachmentUrl(id \| null)` | `{ url, loading, error }` (not renewed; URLs last one hour) |
|
|
79
|
+
| `useUser(externalId)` | `{ profile, loading }` |
|
|
80
|
+
| `useAppEvent(handler)` | Calls `handler({ type, data })` for each `app:event` sent to the user |
|
|
81
|
+
| `useRealtimeStatus()` | `'idle' \| 'connecting' \| 'connected' \| 'reconnecting' \| 'closed'` |
|
|
82
|
+
| `useReadReceipts()` | `{ visible, mode, shared, canChange, setShared }` |
|
|
83
|
+
| `useLiveCall(conversationId \| null)` | `{ call, participantIds, refresh }` |
|
|
84
|
+
| `useRoom(conversationId \| null, { livekit })` | `{ live, presentIds, joined, joining, join, leave, media, error }` |
|
|
85
|
+
| `useCallRoom(conversationId \| null, { livekit, … })` | Call media: participants, mic, camera, devices |
|
|
85
86
|
|
|
86
87
|
Helpers: `canEditOwnMessage`, `EDIT_WINDOW_MS`, `receiptsOf`,
|
|
87
88
|
`computeGroupPosition`, `sameGroup`, `sameDay`, `formatDayLabel`, plus
|
|
@@ -99,6 +100,12 @@ Notes that trip people up:
|
|
|
99
100
|
- From 3.1, `useMessages` (the newest 50, merged in) and `useConversations`
|
|
100
101
|
reload from REST after the socket reconnects. The first connect doesn't
|
|
101
102
|
count as a reconnect.
|
|
103
|
+
- Every `useConversations()` under one provider shares one list, so a
|
|
104
|
+
`markReadUpTo` (or `markConversationRead`) anywhere clears the badge in
|
|
105
|
+
every view, and a read on another device arrives as `conversation:read`.
|
|
106
|
+
- After `chat.users.invalidate(externalId)` — or a `member:updated` app event,
|
|
107
|
+
which does it for you — `useUser` re-resolves, keeping the old profile on
|
|
108
|
+
screen until the new one lands.
|
|
102
109
|
- If the user is removed from a conversation, its mounted hooks stop
|
|
103
110
|
updating; hooks mounted later join a fresh channel.
|
|
104
111
|
|
package/dist/index.cjs
CHANGED
|
@@ -6,6 +6,178 @@ var react = require('react');
|
|
|
6
6
|
var jsxRuntime = require('react/jsx-runtime');
|
|
7
7
|
|
|
8
8
|
// src/provider.tsx
|
|
9
|
+
|
|
10
|
+
// src/conversations-store.ts
|
|
11
|
+
var ConversationsStore = class {
|
|
12
|
+
constructor(chat) {
|
|
13
|
+
this.chat = chat;
|
|
14
|
+
}
|
|
15
|
+
chat;
|
|
16
|
+
state = { conversations: [], loading: true, error: null };
|
|
17
|
+
listeners = /* @__PURE__ */ new Set();
|
|
18
|
+
loaded = false;
|
|
19
|
+
inflight = null;
|
|
20
|
+
unbind = null;
|
|
21
|
+
getSnapshot() {
|
|
22
|
+
return this.state;
|
|
23
|
+
}
|
|
24
|
+
subscribe(listener) {
|
|
25
|
+
this.listeners.add(listener);
|
|
26
|
+
if (this.listeners.size === 1) this.activate();
|
|
27
|
+
return () => {
|
|
28
|
+
if (!this.listeners.delete(listener)) return;
|
|
29
|
+
if (this.listeners.size === 0) this.deactivate();
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/** Reload the list from REST. Shows `loading` until it lands. */
|
|
33
|
+
refetch() {
|
|
34
|
+
return this.load(true);
|
|
35
|
+
}
|
|
36
|
+
async create(attrs) {
|
|
37
|
+
const conv = await this.chat.conversations.create(attrs);
|
|
38
|
+
this.setConversations((prev) => prepend(prev, conv));
|
|
39
|
+
return conv;
|
|
40
|
+
}
|
|
41
|
+
/** Zero a conversation's unread and mention counts. */
|
|
42
|
+
markRead(conversationId) {
|
|
43
|
+
this.setConversations(
|
|
44
|
+
(prev) => patchRow(
|
|
45
|
+
prev,
|
|
46
|
+
conversationId,
|
|
47
|
+
(c) => c.unread_count === 0 && !c.mention_unread_count ? c : { ...c, unread_count: 0, mention_unread_count: 0 }
|
|
48
|
+
)
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
/** Apply a `conversation:read` push. */
|
|
52
|
+
applyRead(evt) {
|
|
53
|
+
this.setConversations(
|
|
54
|
+
(prev) => patchRow(prev, evt.conversation_id, (c) => ({
|
|
55
|
+
...c,
|
|
56
|
+
unread_count: evt.unread_count,
|
|
57
|
+
mention_unread_count: evt.mention_unread_count
|
|
58
|
+
}))
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
/** Merge a `conversation:updated` push. */
|
|
62
|
+
applyUpdated(conversation, fromMe) {
|
|
63
|
+
this.setConversations((prev) => mergeUpdated(prev, conversation, fromMe));
|
|
64
|
+
}
|
|
65
|
+
/** Add (or replace) a row from `conversation:created`. */
|
|
66
|
+
applyCreated(conversation) {
|
|
67
|
+
this.setConversations((prev) => prepend(prev, conversation));
|
|
68
|
+
}
|
|
69
|
+
activate() {
|
|
70
|
+
void this.load(!this.loaded).catch(() => {
|
|
71
|
+
});
|
|
72
|
+
let off = null;
|
|
73
|
+
let boundId = null;
|
|
74
|
+
const bind = (me) => {
|
|
75
|
+
if (me.id === boundId) return;
|
|
76
|
+
off?.();
|
|
77
|
+
boundId = me.id;
|
|
78
|
+
off = this.bindRealtime(me.id);
|
|
79
|
+
};
|
|
80
|
+
const known = this.chat.me.peek();
|
|
81
|
+
if (known) bind(known);
|
|
82
|
+
const offMe = this.chat.me.onChange(bind);
|
|
83
|
+
this.unbind = () => {
|
|
84
|
+
offMe();
|
|
85
|
+
off?.();
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
deactivate() {
|
|
89
|
+
this.unbind?.();
|
|
90
|
+
this.unbind = null;
|
|
91
|
+
}
|
|
92
|
+
bindRealtime(meId) {
|
|
93
|
+
const realtime = this.chat.realtime;
|
|
94
|
+
const user = realtime.user(meId);
|
|
95
|
+
const offCreated = user.onConversationCreated((conv) => this.applyCreated(conv));
|
|
96
|
+
const offUpdated = user.onConversationUpdated(
|
|
97
|
+
({ conversation, by_user_id }) => this.applyUpdated(conversation, by_user_id === meId)
|
|
98
|
+
);
|
|
99
|
+
const offRead = user.onConversationRead((evt) => this.applyRead(evt));
|
|
100
|
+
let wasConnected = realtime.getStatus() === "connected";
|
|
101
|
+
let connectedBefore = wasConnected;
|
|
102
|
+
const offStatus = realtime.onStatus((status) => {
|
|
103
|
+
const reconnected = status === "connected" && !wasConnected && connectedBefore;
|
|
104
|
+
connectedBefore ||= status === "connected";
|
|
105
|
+
wasConnected = status === "connected";
|
|
106
|
+
if (reconnected) void this.load(false).catch(() => {
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
return () => {
|
|
110
|
+
offCreated();
|
|
111
|
+
offUpdated();
|
|
112
|
+
offRead();
|
|
113
|
+
offStatus();
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
load(showLoading) {
|
|
117
|
+
if (showLoading && !this.state.loading) this.set({ ...this.state, loading: true });
|
|
118
|
+
if (this.inflight) return this.inflight;
|
|
119
|
+
const run = this.chat.conversations.list().then(
|
|
120
|
+
(list) => {
|
|
121
|
+
this.loaded = true;
|
|
122
|
+
this.set({ conversations: list.data, loading: false, error: null });
|
|
123
|
+
},
|
|
124
|
+
(err) => {
|
|
125
|
+
this.set({
|
|
126
|
+
...this.state,
|
|
127
|
+
loading: false,
|
|
128
|
+
error: err instanceof Error ? err : new Error(String(err))
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
).finally(() => {
|
|
132
|
+
if (this.inflight === run) this.inflight = null;
|
|
133
|
+
});
|
|
134
|
+
this.inflight = run;
|
|
135
|
+
return run;
|
|
136
|
+
}
|
|
137
|
+
setConversations(fn) {
|
|
138
|
+
const next = fn(this.state.conversations);
|
|
139
|
+
if (next !== this.state.conversations) this.set({ ...this.state, conversations: next });
|
|
140
|
+
}
|
|
141
|
+
set(state) {
|
|
142
|
+
this.state = state;
|
|
143
|
+
for (const l of this.listeners) l();
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
function patchRow(prev, id, fn) {
|
|
147
|
+
const idx = prev.findIndex((c) => c.id === id);
|
|
148
|
+
if (idx === -1) return prev;
|
|
149
|
+
const row = fn(prev[idx]);
|
|
150
|
+
if (row === prev[idx]) return prev;
|
|
151
|
+
const next = prev.slice();
|
|
152
|
+
next[idx] = row;
|
|
153
|
+
return next;
|
|
154
|
+
}
|
|
155
|
+
function prepend(prev, conv) {
|
|
156
|
+
const idx = prev.findIndex((c) => c.id === conv.id);
|
|
157
|
+
if (idx === -1) return [conv, ...prev];
|
|
158
|
+
const next = prev.slice();
|
|
159
|
+
next[idx] = conv;
|
|
160
|
+
return next;
|
|
161
|
+
}
|
|
162
|
+
function mergeUpdated(prev, next, fromMe) {
|
|
163
|
+
const idx = prev.findIndex((c) => c.id === next.id);
|
|
164
|
+
if (idx === -1) return [next, ...prev];
|
|
165
|
+
const existing = prev[idx];
|
|
166
|
+
const wireUnread = typeof next.unread_count === "number" ? next.unread_count : null;
|
|
167
|
+
const unread_count = wireUnread !== null ? wireUnread : fromMe ? 0 : (existing.unread_count ?? 0) + 1;
|
|
168
|
+
const merged = {
|
|
169
|
+
...existing,
|
|
170
|
+
last_message_at: next.last_message_at,
|
|
171
|
+
last_message_preview: next.last_message_preview,
|
|
172
|
+
last_sequence: next.last_sequence,
|
|
173
|
+
unread_count
|
|
174
|
+
};
|
|
175
|
+
if (typeof next.mention_unread_count === "number") {
|
|
176
|
+
merged.mention_unread_count = next.mention_unread_count;
|
|
177
|
+
}
|
|
178
|
+
const rest = prev.slice(0, idx).concat(prev.slice(idx + 1));
|
|
179
|
+
return [merged, ...rest];
|
|
180
|
+
}
|
|
9
181
|
var PoolseContext = react.createContext(null);
|
|
10
182
|
function PoolseProvider({ config, children }) {
|
|
11
183
|
const configRef = react.useRef(config);
|
|
@@ -55,17 +227,23 @@ function PoolseProvider({ config, children }) {
|
|
|
55
227
|
chat.destroy();
|
|
56
228
|
};
|
|
57
229
|
}, [chat]);
|
|
58
|
-
const value = react.useMemo(() => ({ chat }), [chat]);
|
|
230
|
+
const value = react.useMemo(() => ({ chat, conversations: new ConversationsStore(chat) }), [chat]);
|
|
59
231
|
return /* @__PURE__ */ jsxRuntime.jsx(PoolseContext.Provider, { value, children });
|
|
60
232
|
}
|
|
61
233
|
function usePoolse() {
|
|
234
|
+
return usePoolseContext("usePoolse").chat;
|
|
235
|
+
}
|
|
236
|
+
function useConversationsStore() {
|
|
237
|
+
return usePoolseContext("useConversations").conversations;
|
|
238
|
+
}
|
|
239
|
+
function usePoolseContext(hook) {
|
|
62
240
|
const value = react.useContext(PoolseContext);
|
|
63
241
|
if (!value) {
|
|
64
242
|
throw new Error(
|
|
65
|
-
|
|
243
|
+
`${hook}(): no <PoolseProvider> in the tree. Wrap your component with \`<PoolseProvider config={{apiUrl, getToken}}>\` first.`
|
|
66
244
|
);
|
|
67
245
|
}
|
|
68
|
-
return value
|
|
246
|
+
return value;
|
|
69
247
|
}
|
|
70
248
|
function useMe() {
|
|
71
249
|
const chat = usePoolse();
|
|
@@ -135,99 +313,44 @@ function useConversation(id) {
|
|
|
135
313
|
return { conversation, loading, error, refetch };
|
|
136
314
|
}
|
|
137
315
|
function useConversations() {
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
const [
|
|
141
|
-
const [loading, setLoading] = react.useState(true);
|
|
142
|
-
const [error, setError] = react.useState(null);
|
|
143
|
-
const refetch = react.useCallback(async () => {
|
|
144
|
-
setLoading(true);
|
|
145
|
-
try {
|
|
146
|
-
const list = await chat.conversations.list();
|
|
147
|
-
setConversations(list.data);
|
|
148
|
-
setError(null);
|
|
149
|
-
} catch (err) {
|
|
150
|
-
setError(err instanceof Error ? err : new Error(String(err)));
|
|
151
|
-
} finally {
|
|
152
|
-
setLoading(false);
|
|
153
|
-
}
|
|
154
|
-
}, [chat]);
|
|
155
|
-
const create = react.useCallback(
|
|
156
|
-
async (attrs) => {
|
|
157
|
-
const conv = await chat.conversations.create(attrs);
|
|
158
|
-
setConversations((prev) => prepend(prev, conv));
|
|
159
|
-
return conv;
|
|
160
|
-
},
|
|
161
|
-
[chat]
|
|
162
|
-
);
|
|
163
|
-
react.useEffect(() => {
|
|
164
|
-
void refetch();
|
|
165
|
-
}, [refetch]);
|
|
316
|
+
const store = useConversationsStore();
|
|
317
|
+
useMe();
|
|
318
|
+
const [snapshot, setSnapshot] = react.useState(() => store.getSnapshot());
|
|
166
319
|
react.useEffect(() => {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}, [chat, me]);
|
|
193
|
-
const markConversationRead = react.useCallback((conversationId) => {
|
|
194
|
-
setConversations(
|
|
195
|
-
(prev) => prev.map((c) => c.id === conversationId ? { ...c, unread_count: 0 } : c)
|
|
196
|
-
);
|
|
197
|
-
}, []);
|
|
198
|
-
const unreadCounts = react.useMemo(() => {
|
|
199
|
-
const out = {};
|
|
200
|
-
for (const c of conversations) {
|
|
201
|
-
if (typeof c.unread_count === "number" && c.unread_count > 0) {
|
|
202
|
-
out[c.id] = c.unread_count;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
return out;
|
|
206
|
-
}, [conversations]);
|
|
207
|
-
return { conversations, loading, error, refetch, create, unreadCounts, markConversationRead };
|
|
208
|
-
}
|
|
209
|
-
function prepend(prev, conv) {
|
|
210
|
-
const idx = prev.findIndex((c) => c.id === conv.id);
|
|
211
|
-
if (idx === -1) return [conv, ...prev];
|
|
212
|
-
const next = prev.slice();
|
|
213
|
-
next[idx] = conv;
|
|
214
|
-
return next;
|
|
215
|
-
}
|
|
216
|
-
function mergeUpdated(prev, next, fromMe) {
|
|
217
|
-
const idx = prev.findIndex((c) => c.id === next.id);
|
|
218
|
-
if (idx === -1) return [next, ...prev];
|
|
219
|
-
const existing = prev[idx];
|
|
220
|
-
const wireUnread = typeof next.unread_count === "number" ? next.unread_count : null;
|
|
221
|
-
const unread_count = wireUnread !== null ? wireUnread : fromMe ? 0 : (existing.unread_count ?? 0) + 1;
|
|
222
|
-
const merged = {
|
|
223
|
-
...existing,
|
|
224
|
-
last_message_at: next.last_message_at,
|
|
225
|
-
last_message_preview: next.last_message_preview,
|
|
226
|
-
last_sequence: next.last_sequence,
|
|
227
|
-
unread_count
|
|
320
|
+
const off = store.subscribe(() => setSnapshot(store.getSnapshot()));
|
|
321
|
+
setSnapshot(store.getSnapshot());
|
|
322
|
+
return off;
|
|
323
|
+
}, [store]);
|
|
324
|
+
const refetch = react.useCallback(() => store.refetch(), [store]);
|
|
325
|
+
const create = react.useCallback((attrs) => store.create(attrs), [store]);
|
|
326
|
+
const markConversationRead = react.useCallback(
|
|
327
|
+
(conversationId) => store.markRead(conversationId),
|
|
328
|
+
[store]
|
|
329
|
+
);
|
|
330
|
+
const { conversations, loading, error } = snapshot;
|
|
331
|
+
const unreadCounts = react.useMemo(() => countsOf(conversations, "unread_count"), [conversations]);
|
|
332
|
+
const mentionUnreadCounts = react.useMemo(
|
|
333
|
+
() => countsOf(conversations, "mention_unread_count"),
|
|
334
|
+
[conversations]
|
|
335
|
+
);
|
|
336
|
+
return {
|
|
337
|
+
conversations,
|
|
338
|
+
loading,
|
|
339
|
+
error,
|
|
340
|
+
refetch,
|
|
341
|
+
create,
|
|
342
|
+
unreadCounts,
|
|
343
|
+
mentionUnreadCounts,
|
|
344
|
+
markConversationRead
|
|
228
345
|
};
|
|
229
|
-
|
|
230
|
-
|
|
346
|
+
}
|
|
347
|
+
function countsOf(conversations, key) {
|
|
348
|
+
const out = {};
|
|
349
|
+
for (const c of conversations) {
|
|
350
|
+
const n = c[key];
|
|
351
|
+
if (typeof n === "number" && n > 0) out[c.id] = n;
|
|
352
|
+
}
|
|
353
|
+
return out;
|
|
231
354
|
}
|
|
232
355
|
function useMembers(conversationId) {
|
|
233
356
|
const chat = usePoolse();
|
|
@@ -327,12 +450,17 @@ function useMembers(conversationId) {
|
|
|
327
450
|
var PAGE_SIZE = 50;
|
|
328
451
|
function useMessages(conversationId) {
|
|
329
452
|
const chat = usePoolse();
|
|
453
|
+
const conversationsStore = useConversationsStore();
|
|
330
454
|
const { me } = useMe();
|
|
331
455
|
const [messages, setMessages] = react.useState([]);
|
|
332
456
|
const [loading, setLoading] = react.useState(true);
|
|
333
457
|
const [error, setError] = react.useState(null);
|
|
334
458
|
const [hasMore, setHasMore] = react.useState(true);
|
|
335
459
|
const oldestSequenceRef = react.useRef(null);
|
|
460
|
+
const messagesRef = react.useRef(messages);
|
|
461
|
+
messagesRef.current = messages;
|
|
462
|
+
const hasMoreRef = react.useRef(hasMore);
|
|
463
|
+
hasMoreRef.current = hasMore;
|
|
336
464
|
const refetchRef = react.useRef(null);
|
|
337
465
|
const meIdRef = react.useRef(me?.id ?? null);
|
|
338
466
|
meIdRef.current = me?.id ?? null;
|
|
@@ -430,6 +558,30 @@ function useMessages(conversationId) {
|
|
|
430
558
|
setHasMore(page.data.length === PAGE_SIZE);
|
|
431
559
|
oldestSequenceRef.current = older[0]?.sequence ?? oldestSequenceRef.current;
|
|
432
560
|
}, [chat, conversationId, hasMore, loading]);
|
|
561
|
+
const loadUntil = react.useCallback(
|
|
562
|
+
async (messageId, maxPages = 20) => {
|
|
563
|
+
if (messagesRef.current.some((m) => m.id === messageId)) return true;
|
|
564
|
+
const handle = chat.conversations.one(conversationId).messages;
|
|
565
|
+
for (let i = 0; i < maxPages; i++) {
|
|
566
|
+
const before = oldestSequenceRef.current;
|
|
567
|
+
if (before === null || !hasMoreRef.current) return false;
|
|
568
|
+
const page = await handle.list({ limit: PAGE_SIZE, before });
|
|
569
|
+
const older = [...page.data].reverse();
|
|
570
|
+
if (older.length > 0) {
|
|
571
|
+
setMessages((prev) => {
|
|
572
|
+
const have = new Set(prev.map((m) => m.id));
|
|
573
|
+
return [...older.filter((m) => !have.has(m.id)), ...prev];
|
|
574
|
+
});
|
|
575
|
+
oldestSequenceRef.current = older[0].sequence;
|
|
576
|
+
}
|
|
577
|
+
hasMoreRef.current = page.data.length === PAGE_SIZE;
|
|
578
|
+
setHasMore(hasMoreRef.current);
|
|
579
|
+
if (older.some((m) => m.id === messageId)) return true;
|
|
580
|
+
}
|
|
581
|
+
return false;
|
|
582
|
+
},
|
|
583
|
+
[chat, conversationId]
|
|
584
|
+
);
|
|
433
585
|
const send = react.useCallback(
|
|
434
586
|
async (attrs) => {
|
|
435
587
|
const id = attrs.id ?? sdk.safeUuid();
|
|
@@ -524,8 +676,9 @@ function useMessages(conversationId) {
|
|
|
524
676
|
const markReadUpTo = react.useCallback(
|
|
525
677
|
async (messageId) => {
|
|
526
678
|
await chat.conversations.one(conversationId).messages.markRead(messageId);
|
|
679
|
+
conversationsStore.markRead(conversationId);
|
|
527
680
|
},
|
|
528
|
-
[chat, conversationId]
|
|
681
|
+
[chat, conversationId, conversationsStore]
|
|
529
682
|
);
|
|
530
683
|
const refetch = react.useCallback(async () => {
|
|
531
684
|
const fn = refetchRef.current;
|
|
@@ -538,6 +691,7 @@ function useMessages(conversationId) {
|
|
|
538
691
|
error,
|
|
539
692
|
hasMore,
|
|
540
693
|
loadMore,
|
|
694
|
+
loadUntil,
|
|
541
695
|
send,
|
|
542
696
|
edit,
|
|
543
697
|
delete: deleteMessage,
|
|
@@ -1093,6 +1247,7 @@ function useAttachmentUrl(attachmentId) {
|
|
|
1093
1247
|
var EMPTY = { profile: null, loading: false };
|
|
1094
1248
|
function useUser(externalId) {
|
|
1095
1249
|
const chat = usePoolse();
|
|
1250
|
+
const shownForRef = react.useRef(null);
|
|
1096
1251
|
const [state, setState] = react.useState(() => {
|
|
1097
1252
|
if (!externalId) return EMPTY;
|
|
1098
1253
|
const cached = chat.users.peek(externalId);
|
|
@@ -1107,9 +1262,11 @@ function useUser(externalId) {
|
|
|
1107
1262
|
const apply = () => {
|
|
1108
1263
|
const cached = chat.users.peek(externalId);
|
|
1109
1264
|
if (cached === void 0) {
|
|
1110
|
-
|
|
1265
|
+
const keep = shownForRef.current === externalId;
|
|
1266
|
+
setState((prev) => ({ profile: keep ? prev.profile : null, loading: true }));
|
|
1111
1267
|
void chat.users.get(externalId);
|
|
1112
1268
|
} else {
|
|
1269
|
+
shownForRef.current = externalId;
|
|
1113
1270
|
setState({ profile: cached, loading: false });
|
|
1114
1271
|
}
|
|
1115
1272
|
};
|
|
@@ -1119,6 +1276,12 @@ function useUser(externalId) {
|
|
|
1119
1276
|
}, [chat, externalId]);
|
|
1120
1277
|
return state;
|
|
1121
1278
|
}
|
|
1279
|
+
function useAppEvent(handler) {
|
|
1280
|
+
const chat = usePoolse();
|
|
1281
|
+
const handlerRef = react.useRef(handler);
|
|
1282
|
+
handlerRef.current = handler;
|
|
1283
|
+
react.useEffect(() => chat.onAppEvent((evt) => handlerRef.current(evt)), [chat]);
|
|
1284
|
+
}
|
|
1122
1285
|
function computeGroupPosition(msg, prev, next, windowMs) {
|
|
1123
1286
|
const continuesPrev = sameGroup(msg, prev, windowMs);
|
|
1124
1287
|
const continuesNext = sameGroup(next, msg, windowMs);
|
|
@@ -1544,6 +1707,7 @@ exports.formatDayLabel = formatDayLabel;
|
|
|
1544
1707
|
exports.receiptsOf = receiptsOf;
|
|
1545
1708
|
exports.sameDay = sameDay;
|
|
1546
1709
|
exports.sameGroup = sameGroup;
|
|
1710
|
+
exports.useAppEvent = useAppEvent;
|
|
1547
1711
|
exports.useAttachmentUpload = useAttachmentUpload;
|
|
1548
1712
|
exports.useAttachmentUrl = useAttachmentUrl;
|
|
1549
1713
|
exports.useCallRoom = useCallRoom;
|