Repository navigation
Expand file tree
/
Copy pathuseRoomMessages.js
More file actions
215 lines (193 loc) · 6.98 KB
/
Copy pathuseRoomMessages.js
File metadata and controls
215 lines (193 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import {
deleteMessage,
editMessage,
getMessage,
listMessages,
sendMessage,
subscribeToRoom,
uploadImage,
} from "../api/messages";
const HISTORY_PAGE_SIZE = 50;
/**
* One room's messages: REST history on mount, the shared chat socket for
* anything that happens afterwards (new, edited, deleted). Our own sends are
* appended from the POST response and arrive again as message.new; both paths
* dedupe by id. `state` mirrors the "loading" | "success" | "error" convention
* the views already use.
*/
export function useRoomMessages(roomId) {
const [messages, setMessages] = useState([]);
// A local DM draft has no history or room subscription until its first send.
const [state, setState] = useState(roomId ? "loading" : "success");
const [sending, setSending] = useState(false);
const [hasOlder, setHasOlder] = useState(false);
const [loadingOlder, setLoadingOlder] = useState(false);
const [olderError, setOlderError] = useState(null);
const pagingRef = useRef(null);
const appendMessage = useCallback((message) => {
if (!message?.id) return;
setMessages((previous) =>
previous.some((item) => item.id === message.id) ? previous : [...previous, message],
);
}, []);
/**
* Shared by the socket's message.edited frame and by our own PATCH response:
* whichever lands first wins, and the other is a no-op (same id, same text).
*/
const applyEdit = useCallback((id, content, editedAt) => {
setMessages((previous) =>
previous.map((item) => (item.id === id ? { ...item, content, edited_at: editedAt } : item)),
);
}, []);
/** Same idea for deletes: our own DELETE and the message.deleted frame both land here. */
const applyDelete = useCallback((id) => {
setMessages((previous) =>
previous.map((item) =>
item.id === id ? { ...item, content: null, deleted: true, attachments: [] } : item,
),
);
}, []);
const applyUpdate = useCallback(
(update) => {
switch (update.type) {
case "new":
appendMessage(update.message);
break;
case "edited":
applyEdit(update.id, update.content, update.edited_at);
break;
case "deleted":
applyDelete(update.id);
break;
default:
break;
}
},
[appendMessage, applyEdit, applyDelete],
);
useEffect(() => {
if (!roomId) return undefined;
let cancelled = false;
const paging = { roomId, active: true, cursor: null, hasOlder: false, loading: false };
pagingRef.current = paging;
setState("loading");
setMessages([]);
setHasOlder(false);
setLoadingOlder(false);
setOlderError(null);
// Subscribe first so nothing sent while history loads is lost; the merge
// below keeps anything that arrived live and dedupes by id.
const unsubscribe = subscribeToRoom(roomId, applyUpdate);
listMessages(roomId, { limit: HISTORY_PAGE_SIZE })
.then((history) => {
if (cancelled) return;
paging.cursor = history[0]?.id ?? null;
paging.hasOlder = history.length === HISTORY_PAGE_SIZE;
setHasOlder(paging.hasOlder);
setMessages((live) => {
const known = new Set(history.map((item) => item.id));
return [...history, ...live.filter((item) => !known.has(item.id))];
});
setState("success");
})
.catch(() => {
if (!cancelled) setState("error");
});
return () => {
cancelled = true;
paging.active = false;
unsubscribe();
};
}, [roomId, applyUpdate]);
const loadOlder = useCallback(async () => {
const paging = pagingRef.current;
// The ref closes the gap before React renders the loading flag, preventing
// overlapping requests when multiple top/scroll events fire together.
if (!paging?.active || paging.roomId !== roomId || !paging.hasOlder || paging.loading) return;
paging.loading = true;
setLoadingOlder(true);
setOlderError(null);
try {
const page = await listMessages(roomId, { before: paging.cursor, limit: HISTORY_PAGE_SIZE });
if (!paging.active) return;
const nextCursor = page[0]?.id ?? paging.cursor;
paging.hasOlder = page.length === HISTORY_PAGE_SIZE && nextCursor !== paging.cursor;
paging.cursor = nextCursor;
setHasOlder(paging.hasOlder);
setMessages((current) => {
const known = new Set(current.map((message) => message.id));
const older = page.filter((message) => {
if (known.has(message.id)) return false;
known.add(message.id);
return true;
});
return older.length ? [...older, ...current] : current;
});
} catch (error) {
if (paging.active) setOlderError(error?.message || "Couldn't load older messages.");
} finally {
paging.loading = false;
if (paging.active) setLoadingOlder(false);
}
}, [roomId]);
/**
* Images are uploaded first, in parallel, then sent with the text in one
* POST. Throws if any step fails, so the composer keeps the text and images.
*/
const send = useCallback(
async (content, replyToId, files = []) => {
const text = (content || "").trim();
if (!text && !files.length) return;
setSending(true);
try {
const uploadIds = await Promise.all(files.map((file) => uploadImage(roomId, file)));
appendMessage(await sendMessage(roomId, text, replyToId, uploadIds));
} finally {
setSending(false);
}
},
[roomId, appendMessage],
);
/** Image links expire after a few minutes: the message again, with fresh ones. Returns it. */
const refreshImages = useCallback(async (messageId) => {
const fresh = await getMessage(messageId);
setMessages((previous) =>
previous.map((item) => (item.id === fresh.id ? { ...item, attachments: fresh.attachments } : item)),
);
return fresh;
}, []);
/**
* Throws on refusal (EDIT_WINDOW_EXPIRED, NOT_SENDER) so the row can show
* why: swallowing it here would leave the old text on screen with no reason.
*/
const edit = useCallback(
async (messageId, content) => {
const text = (content || "").trim();
if (!text) return;
const saved = await editMessage(messageId, text);
applyEdit(saved.id, saved.content, saved.edited_at);
},
[applyEdit],
);
/**
* Sender, or a room owner/admin. Throws on refusal (NOT_ALLOWED) so the
* confirm dialog can show why. Deleting twice is a 204 both times.
*/
const remove = useCallback(
async (messageId) => {
await deleteMessage(messageId);
applyDelete(messageId);
},
[applyDelete],
);
/** Reply targets are looked up in the loaded page; nothing is refetched. */
const messagesById = useMemo(
() => new Map(messages.map((message) => [message.id, message])),
[messages],
);
return {
messages, messagesById, state, sending, send, edit, remove, refreshImages,
hasOlder, loadingOlder, olderError, loadOlder,
};
}