Skip to content

Commit a81e677

Browse files
committed
fix: handle only draft events in channel preview and thread list item
1 parent f4f74ef commit a81e677

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

package/src/components/ChannelPreview/hooks/useChannelPreviewData.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,32 @@ export const useChannelPreviewData = (
2323
const { forceUpdate: contextForceUpdate } = useChannelsContext();
2424
const channelListForceUpdate = forceUpdateOverride ?? contextForceUpdate;
2525

26-
useEffect(() => {
27-
const unsubscribe = channel.messageComposer.registerSubscriptions();
28-
return () => {
29-
unsubscribe();
30-
};
31-
}, [channel.messageComposer]);
32-
3326
const channelLastMessage = channel.lastMessage();
3427
const channelLastMessageString = `${channelLastMessage?.id}${channelLastMessage?.updated_at}`;
3528

29+
useEffect(() => {
30+
const { unsubscribe } = client.on('draft.updated', (event) => {
31+
const { cid, draft } = event;
32+
if (!draft || cid !== channel.cid) {
33+
return;
34+
}
35+
channel.messageComposer.initState({ composition: draft });
36+
});
37+
return unsubscribe;
38+
}, [channel, client]);
39+
40+
useEffect(() => {
41+
const { unsubscribe } = client.on('draft.deleted', (event) => {
42+
const { cid, draft } = event;
43+
if (!draft || cid !== channel.cid) {
44+
return;
45+
}
46+
47+
channel.messageComposer.clear();
48+
});
49+
return unsubscribe;
50+
}, [channel, client]);
51+
3652
useEffect(() => {
3753
const { unsubscribe } = client.on('notification.mark_read', () => {
3854
setUnread(channel.countUnread());

package/src/components/ThreadList/ThreadListItem.tsx

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,30 @@ export const ThreadListItemComponent = () => {
279279
);
280280

281281
useEffect(() => {
282-
const unsubscribe = thread.messageComposer.registerSubscriptions();
283-
return () => {
284-
unsubscribe();
285-
};
286-
}, [thread.messageComposer]);
282+
const { unsubscribe } = client.on('draft.updated', (event) => {
283+
const { cid, draft } = event;
284+
if (!draft) return;
285+
if (cid !== channel.cid || draft.parent_id !== thread.id) {
286+
return;
287+
}
288+
289+
thread.messageComposer.initState({ composition: draft });
290+
});
291+
return unsubscribe;
292+
}, [channel.cid, client, thread.id, thread.messageComposer]);
293+
294+
useEffect(() => {
295+
const { unsubscribe } = client.on('draft.deleted', (event) => {
296+
const { cid, draft } = event;
297+
if (!draft) return;
298+
if (cid !== channel.cid || draft.parent_id !== thread.id) {
299+
return;
300+
}
301+
302+
thread.messageComposer.clear();
303+
});
304+
return unsubscribe;
305+
}, [channel.cid, client, thread.id, thread.messageComposer]);
287306

288307
const draftMessage: DraftMessage | undefined = useMemo(
289308
() =>

0 commit comments

Comments
 (0)