Skip to content

Commit 00ba4d9

Browse files
committed
fix: poll related edge cases with offline storage
1 parent 5052d03 commit 00ba4d9

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ export type LatestMessagePreview<
4040
export type LatestMessagePreviewSelectorReturnType = {
4141
createdBy?: UserResponse | null;
4242
latestVotesByOption?: Record<string, PollVote[]>;
43+
name?: string;
4344
};
4445

4546
const selector = <StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics>(
4647
nextValue: PollState<StreamChatGenerics>,
4748
): LatestMessagePreviewSelectorReturnType => ({
4849
createdBy: nextValue.created_by,
4950
latestVotesByOption: nextValue.latest_votes_by_option,
51+
name: nextValue.name,
5052
});
5153

5254
const getMessageSenderName = <
@@ -145,8 +147,8 @@ const getLatestMessageDisplayText = <
145147
{ bold: false, text: t('🏙 Attachment...') },
146148
];
147149
}
148-
if (message.poll && pollState) {
149-
const { createdBy, latestVotesByOption } = pollState;
150+
if (message.poll_id && pollState) {
151+
const { createdBy, latestVotesByOption, name } = pollState;
150152
let latestVotes;
151153
if (latestVotesByOption) {
152154
latestVotes = Object.values(latestVotesByOption)
@@ -161,7 +163,7 @@ const getLatestMessageDisplayText = <
161163
}
162164
const previewMessage = `${
163165
client.userID === previewUser?.id ? 'You' : previewUser?.name
164-
} ${previewAction}: ${message.poll.name}`;
166+
} ${previewAction}: ${name}`;
165167
return [
166168
{ bold: false, text: '📊 ' },
167169
{ bold: false, text: previewMessage },
@@ -310,7 +312,7 @@ export const useLatestMessagePreview = <
310312
const poll = client.polls.fromState(pollId);
311313
const pollState: LatestMessagePreviewSelectorReturnType =
312314
useStateStore(poll?.state, selector) ?? {};
313-
const { createdBy, latestVotesByOption } = pollState;
315+
const { createdBy, latestVotesByOption, name } = pollState;
314316

315317
useEffect(
316318
() =>
@@ -325,7 +327,15 @@ export const useLatestMessagePreview = <
325327
}),
326328
),
327329
// eslint-disable-next-line react-hooks/exhaustive-deps
328-
[channelLastMessageString, forceUpdate, readEvents, readStatus, latestVotesByOption, createdBy],
330+
[
331+
channelLastMessageString,
332+
forceUpdate,
333+
readEvents,
334+
readStatus,
335+
latestVotesByOption,
336+
createdBy,
337+
name,
338+
],
329339
);
330340

331341
return latestMessagePreview;

package/src/components/Reply/Reply.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import dayjs from 'dayjs';
66

77
import merge from 'lodash/merge';
88

9-
import type { Attachment } from 'stream-chat';
9+
import type { Attachment, PollState } from 'stream-chat';
1010

11+
import { useChatContext } from '../../contexts';
1112
import { useMessageContext } from '../../contexts/messageContext/MessageContext';
1213
import {
1314
MessageInputContext,
@@ -22,6 +23,7 @@ import {
2223
TranslationContextValue,
2324
useTranslationContext,
2425
} from '../../contexts/translationContext/TranslationContext';
26+
import { useStateStore } from '../../hooks';
2527
import { DefaultStreamChatGenerics, FileTypes } from '../../types/types';
2628
import { getResizedImageUrl } from '../../utils/getResizedImageUrl';
2729
import { getTrimmedAttachmentTitle } from '../../utils/getTrimmedAttachmentTitle';
@@ -31,6 +33,7 @@ import { FileIcon as FileIconDefault } from '../Attachment/FileIcon';
3133
import { VideoThumbnail } from '../Attachment/VideoThumbnail';
3234
import { MessageAvatar as MessageAvatarDefault } from '../Message/MessageSimple/MessageAvatar';
3335
import { MessageTextContainer } from '../Message/MessageSimple/MessageTextContainer';
36+
import { MessageType } from '../MessageList/hooks/useMessageList';
3437

3538
const styles = StyleSheet.create({
3639
container: {
@@ -72,6 +75,16 @@ const styles = StyleSheet.create({
7275
},
7376
});
7477

78+
export type ReplySelectorReturnType = {
79+
name?: string;
80+
};
81+
82+
const selector = <StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics>(
83+
nextValue: PollState<StreamChatGenerics>,
84+
): ReplySelectorReturnType => ({
85+
name: nextValue.name,
86+
});
87+
7588
type ReplyPropsWithContext<
7689
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
7790
> = Pick<MessageInputContextValue<StreamChatGenerics>, 'quotedMessage'> &
@@ -134,6 +147,7 @@ const ReplyWithContext = <
134147
>(
135148
props: ReplyPropsWithContext<StreamChatGenerics>,
136149
) => {
150+
const { client } = useChatContext();
137151
const {
138152
attachmentSize = 40,
139153
FileAttachmentIcon,
@@ -167,6 +181,9 @@ const ReplyWithContext = <
167181
},
168182
} = useTheme();
169183

184+
const poll = client.polls.fromState((quotedMessage as MessageType)?.poll_id ?? '');
185+
const { name: pollName }: ReplySelectorReturnType = useStateStore(poll?.state, selector) ?? {};
186+
170187
const messageText = typeof quotedMessage === 'boolean' ? '' : quotedMessage.text || '';
171188

172189
const emojiOnlyText = useMemo(() => {
@@ -262,8 +279,8 @@ const ReplyWithContext = <
262279
text:
263280
quotedMessage.type === 'deleted'
264281
? `_${t('Message deleted')}_`
265-
: quotedMessage.poll
266-
? `📊 ${quotedMessage.poll.name}`
282+
: pollName
283+
? `📊 ${pollName}`
267284
: quotedMessage.text
268285
? quotedMessage.text.length > 170
269286
? `${quotedMessage.text.slice(0, 170)}...`

package/src/store/apis/updateMessage.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ export const updateMessage = ({
3030
return queries;
3131
}
3232

33-
const storableMessage = mapMessageToStorable(message);
33+
const { poll, poll_id } = messages[0];
34+
35+
const storableMessage = mapMessageToStorable({
36+
...(poll ? { poll: JSON.parse(poll) } : {}),
37+
...(poll_id ? { poll_id } : {}),
38+
...message,
39+
});
3440

3541
queries.push(
3642
createUpdateQuery('messages', storableMessage, {

package/src/store/apis/updatePollMessage.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ export const updatePollMessage = ({
2222
);
2323

2424
for (const message of messagesWithPoll) {
25+
const { latest_votes, own_votes } = JSON.parse(message.poll);
2526
const storablePoll = JSON.stringify({
2627
...poll,
27-
latest_votes: message.poll.latest_votes,
28-
own_votes: message.poll.own_votes,
28+
latest_votes,
29+
own_votes,
2930
});
3031
const storableMessage = { ...message, poll: storablePoll };
3132

0 commit comments

Comments
 (0)