Skip to content

Commit 71024ac

Browse files
committed
fix: getDraft bug and add draft support for thread list
1 parent 75087db commit 71024ac

File tree

4 files changed

+83
-18
lines changed

4 files changed

+83
-18
lines changed

package/src/components/MessageInput/MessageInput.tsx

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -324,20 +324,6 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
324324
};
325325
}, [draftsEnabled, messageComposer]);
326326

327-
/**
328-
* Effect to get the draft data for legacy thread composer and set it to message composer.
329-
* TODO: This can be removed once we remove legacy thread composer.
330-
*/
331-
useEffect(() => {
332-
const threadId = messageComposer.threadId;
333-
if (!threadId || !messageComposer.channel || !messageComposer.compositionIsEmpty) return;
334-
messageComposer.channel.getDraft({ parent_id: threadId }).then(({ draft }) => {
335-
if (draft) {
336-
messageComposer.initState({ composition: draft });
337-
}
338-
});
339-
}, [messageComposer]);
340-
341327
const uploadImagesHandler = async () => {
342328
const imageToUpload = selectedImages.find((selectedImage) => {
343329
const uploadedImage = imageUploads.find(

package/src/components/Thread/Thread.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useCallback, useEffect } from 'react';
22

33
import { ThreadFooterComponent } from './components/ThreadFooterComponent';
44

5+
import { useMessageComposer } from '../../contexts';
56
import { useChannelContext } from '../../contexts/channelContext/ChannelContext';
67
import { ChatContextValue, useChatContext } from '../../contexts/chatContext/ChatContext';
78
import {
@@ -59,6 +60,7 @@ const ThreadWithContext = (props: ThreadPropsWithContext) => {
5960
additionalMessageInputProps,
6061
additionalMessageListProps,
6162
autoFocus = true,
63+
client,
6264
closeThread,
6365
closeThreadOnDismount = true,
6466
disabled,
@@ -71,6 +73,37 @@ const ThreadWithContext = (props: ThreadPropsWithContext) => {
7173
threadInstance,
7274
} = props;
7375

76+
const messageComposer = useMessageComposer();
77+
78+
/**
79+
* Effect to get the draft data for legacy thread composer and set it to message composer.
80+
* TODO: This can be removed once we remove legacy thread composer.
81+
*/
82+
useEffect(() => {
83+
const threadId = messageComposer.threadId;
84+
if (!threadId || !messageComposer.channel || !messageComposer.compositionIsEmpty) return;
85+
86+
if (client.offlineDb && client.userID) {
87+
client.offlineDb
88+
.getDraft({
89+
cid: messageComposer.channel.cid,
90+
currentUserId: client.userID,
91+
parent_id: threadId,
92+
})
93+
.then((draft) => {
94+
if (draft) {
95+
messageComposer.initState({ composition: draft });
96+
}
97+
});
98+
}
99+
100+
messageComposer.channel.getDraft({ parent_id: threadId }).then(({ draft }) => {
101+
if (draft) {
102+
messageComposer.initState({ composition: draft });
103+
}
104+
});
105+
}, [client.offlineDb, client.userID, messageComposer, thread]);
106+
74107
useEffect(() => {
75108
if (threadInstance?.activate) {
76109
threadInstance.activate();

package/src/components/ThreadList/ThreadListItem.tsx

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import React, { useCallback, useMemo } from 'react';
22
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
33

4-
import { LocalMessage, Thread, ThreadState } from 'stream-chat';
4+
import {
5+
AttachmentManagerState,
6+
DraftMessage,
7+
LocalMessage,
8+
TextComposerState,
9+
Thread,
10+
ThreadState,
11+
} from 'stream-chat';
512

613
import {
714
TranslationContextValue,
@@ -70,11 +77,13 @@ export const attachmentTypeIconMap = {
7077

7178
const getTitleFromMessage = ({
7279
currentUserId,
80+
draftMessage,
7381
message,
7482
t,
7583
}: {
7684
t: TranslationContextValue['t'];
7785
currentUserId?: string;
86+
draftMessage?: DraftMessage;
7887
message?: LocalMessage;
7988
}) => {
8089
const attachment = message?.attachments?.at(0);
@@ -86,8 +95,15 @@ const getTitleFromMessage = ({
8695
} `
8796
: '';
8897

89-
const messageBelongsToCurrentUserPrefix =
90-
message?.user?.id === currentUserId ? `${t('You')}: ` : '';
98+
const messageBelongsToCurrentUserPrefix = draftMessage
99+
? 'Draft:'
100+
: message?.user?.id === currentUserId
101+
? `${t('You')}: `
102+
: '';
103+
104+
if (draftMessage) {
105+
return `${attachmentIcon}${messageBelongsToCurrentUserPrefix}${draftMessage.text || t('🏙 Attachment...') || 'N/A'}`;
106+
}
91107

92108
if (message?.deleted_at && message.parent_id) {
93109
return `${messageBelongsToCurrentUserPrefix}${t('This reply was deleted')}.`;
@@ -106,6 +122,14 @@ const getTitleFromMessage = ({
106122
}`;
107123
};
108124

125+
const textComposerStateSelector = (state: TextComposerState) => ({
126+
text: state.text,
127+
});
128+
129+
const stateSelector = (state: AttachmentManagerState) => ({
130+
attachments: state.attachments,
131+
});
132+
109133
export const ThreadListItemComponent = () => {
110134
const {
111135
channel,
@@ -126,6 +150,27 @@ export const ThreadListItemComponent = () => {
126150
threadListItem,
127151
},
128152
} = useTheme();
153+
const { text: draftText } = useStateStore(
154+
thread.messageComposer.textComposer.state,
155+
textComposerStateSelector,
156+
);
157+
158+
const { attachments } = useStateStore(
159+
thread.messageComposer.attachmentManager.state,
160+
stateSelector,
161+
);
162+
163+
const draftMessage: DraftMessage | undefined = useMemo(
164+
() =>
165+
!thread.messageComposer.compositionIsEmpty
166+
? {
167+
attachments,
168+
id: thread.messageComposer.id,
169+
text: draftText,
170+
}
171+
: undefined,
172+
[thread.messageComposer, attachments, draftText],
173+
);
129174

130175
return (
131176
<TouchableOpacity
@@ -194,6 +239,7 @@ export const ThreadListItemComponent = () => {
194239
? 'This thread was deleted.'
195240
: getTitleFromMessage({
196241
currentUserId: client.userID,
242+
draftMessage,
197243
message: lastReply,
198244
t,
199245
})}

package/src/store/apis/getDraft.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const getDraft = async ({
4545
channelRow: channelRows[0] as unknown as TableRow<'channels'>,
4646
currentUserId,
4747
draftMessageRow: draftMessageRows[0] as unknown as TableRow<'draftMessage'>,
48-
draftRow: draftRow[0] as unknown as TableRow<'draft'>,
48+
draftRow: draftRow as unknown as TableRow<'draft'>,
4949
pollRow: polls[0],
5050
quotedMessageRow: quotedMessageRows,
5151
});

0 commit comments

Comments
 (0)