Skip to content

Commit

Permalink
fix: channel preview update on new message when coming from BG to FG (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
khushal87 authored Jul 8, 2024
1 parent abf6cd8 commit ea688c3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package/src/components/ChannelPreview/ChannelPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const ChannelPreviewWithContext = <
const [forceUpdate, setForceUpdate] = useState(0);
const [unread, setUnread] = useState(channel.countUnread());

const latestMessagePreview = useLatestMessagePreview(channel, forceUpdate);
const latestMessagePreview = useLatestMessagePreview(channel, forceUpdate, lastMessage);

const channelLastMessage = channel.lastMessage();
const channelLastMessageString = `${channelLastMessage?.id}${channelLastMessage?.updated_at}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useTranslationContext } from '../../../contexts/translationContext/Tran

import { useTranslatedMessage } from '../../../hooks/useTranslatedMessage';
import type { DefaultStreamChatGenerics } from '../../../types/types';
import { stringifyMessage } from '../../../utils/utils';

type LatestMessage<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
Expand Down Expand Up @@ -218,18 +219,20 @@ export const useLatestMessagePreview = <
>(
channel: Channel<StreamChatGenerics>,
forceUpdate: number,
lastMessage?:
| ReturnType<ChannelState<StreamChatGenerics>['formatMessage']>
| MessageResponse<StreamChatGenerics>,
) => {
const { client } = useChatContext<StreamChatGenerics>();
const { t } = useTranslationContext();

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

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

const translatedLastMessage = useTranslatedMessage<StreamChatGenerics>(message);

const channelLastMessageString = `${message?.id}${message?.updated_at}`;
const channelLastMessageString = translatedLastMessage
? stringifyMessage(translatedLastMessage)
: '';

const [readEvents, setReadEvents] = useState(true);
const [latestMessagePreview, setLatestMessagePreview] = useState<
Expand Down
6 changes: 5 additions & 1 deletion package/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
ChannelMemberResponse,
CommandResponse,
FormatMessageResponse,
MessageResponse,
StreamChat,
UserResponse,
} from 'stream-chat';
Expand Down Expand Up @@ -614,7 +615,10 @@ export const stringifyMessage = <
text,
type,
updated_at,
}: FormatMessageResponse<StreamChatGenerics> | MessageType<StreamChatGenerics>): string =>
}:
| MessageResponse<StreamChatGenerics>
| FormatMessageResponse<StreamChatGenerics>
| MessageType<StreamChatGenerics>): string =>
`${
latest_reactions ? latest_reactions.map(({ type, user }) => `${type}${user?.id}`).join() : ''
}${
Expand Down

0 comments on commit ea688c3

Please sign in to comment.