Skip to content

Commit ea688c3

Browse files
authored
fix: channel preview update on new message when coming from BG to FG (#2585)
* fix: channel preview update on new message when coming from BG to FG * fix: channel preview update on new message when coming from BG to FG
1 parent abf6cd8 commit ea688c3

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

package/src/components/ChannelPreview/ChannelPreview.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const ChannelPreviewWithContext = <
4242
const [forceUpdate, setForceUpdate] = useState(0);
4343
const [unread, setUnread] = useState(channel.countUnread());
4444

45-
const latestMessagePreview = useLatestMessagePreview(channel, forceUpdate);
45+
const latestMessagePreview = useLatestMessagePreview(channel, forceUpdate, lastMessage);
4646

4747
const channelLastMessage = channel.lastMessage();
4848
const channelLastMessageString = `${channelLastMessage?.id}${channelLastMessage?.updated_at}`;

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useTranslationContext } from '../../../contexts/translationContext/Tran
88

99
import { useTranslatedMessage } from '../../../hooks/useTranslatedMessage';
1010
import type { DefaultStreamChatGenerics } from '../../../types/types';
11+
import { stringifyMessage } from '../../../utils/utils';
1112

1213
type LatestMessage<
1314
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
@@ -218,18 +219,20 @@ export const useLatestMessagePreview = <
218219
>(
219220
channel: Channel<StreamChatGenerics>,
220221
forceUpdate: number,
222+
lastMessage?:
223+
| ReturnType<ChannelState<StreamChatGenerics>['formatMessage']>
224+
| MessageResponse<StreamChatGenerics>,
221225
) => {
222226
const { client } = useChatContext<StreamChatGenerics>();
223227
const { t } = useTranslationContext();
224228

225229
const channelConfigExists = typeof channel?.getConfig === 'function';
226230

227-
const messages = channel.state.messages;
228-
const message = messages.length ? messages[messages.length - 1] : undefined;
231+
const translatedLastMessage = useTranslatedMessage<StreamChatGenerics>(lastMessage);
229232

230-
const translatedLastMessage = useTranslatedMessage<StreamChatGenerics>(message);
231-
232-
const channelLastMessageString = `${message?.id}${message?.updated_at}`;
233+
const channelLastMessageString = translatedLastMessage
234+
? stringifyMessage(translatedLastMessage)
235+
: '';
233236

234237
const [readEvents, setReadEvents] = useState(true);
235238
const [latestMessagePreview, setLatestMessagePreview] = useState<

package/src/utils/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
ChannelMemberResponse,
1010
CommandResponse,
1111
FormatMessageResponse,
12+
MessageResponse,
1213
StreamChat,
1314
UserResponse,
1415
} from 'stream-chat';
@@ -614,7 +615,10 @@ export const stringifyMessage = <
614615
text,
615616
type,
616617
updated_at,
617-
}: FormatMessageResponse<StreamChatGenerics> | MessageType<StreamChatGenerics>): string =>
618+
}:
619+
| MessageResponse<StreamChatGenerics>
620+
| FormatMessageResponse<StreamChatGenerics>
621+
| MessageType<StreamChatGenerics>): string =>
618622
`${
619623
latest_reactions ? latest_reactions.map(({ type, user }) => `${type}${user?.id}`).join() : ''
620624
}${

0 commit comments

Comments
 (0)