Skip to content

Commit ff8c6af

Browse files
committed
fix: all issues with state store api change
1 parent 2e1b8e5 commit ff8c6af

File tree

4 files changed

+72
-63
lines changed

4 files changed

+72
-63
lines changed

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ export type LatestMessagePreview<
3737
created_at?: string | Date;
3838
};
3939

40-
export type LatestMessagePreviewSelectorReturnType = [
41-
Record<string, PollVote[]>,
42-
UserResponse | null,
43-
];
40+
export type LatestMessagePreviewSelectorReturnType = {
41+
created_by?: UserResponse | null;
42+
latest_votes_by_option?: Record<string, PollVote[]>;
43+
};
4444

4545
const selector = <StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics>(
4646
nextValue: PollState<StreamChatGenerics>,
47-
): LatestMessagePreviewSelectorReturnType => [
48-
nextValue.latest_votes_by_option,
49-
nextValue.created_by,
50-
];
47+
): LatestMessagePreviewSelectorReturnType => ({
48+
created_by: nextValue.created_by,
49+
latest_votes_by_option: nextValue.latest_votes_by_option,
50+
});
5151

5252
const getMessageSenderName = <
5353
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
@@ -98,7 +98,7 @@ const getLatestMessageDisplayText = <
9898
client: StreamChat<StreamChatGenerics>,
9999
message: LatestMessage<StreamChatGenerics> | undefined,
100100
t: (key: string) => string,
101-
pollState: LatestMessagePreviewSelectorReturnType | [],
101+
pollState: LatestMessagePreviewSelectorReturnType | undefined,
102102
) => {
103103
if (!message) return [{ bold: false, text: t('Nothing yet...') }];
104104
const isMessageTypeDeleted = message.type === 'deleted';
@@ -146,16 +146,16 @@ const getLatestMessageDisplayText = <
146146
];
147147
}
148148
if (message.poll && pollState) {
149-
const [latest_votes_by_option, created_by] = pollState;
149+
const { created_by, latest_votes_by_option } = pollState;
150150
let latestVotes;
151151
if (latest_votes_by_option) {
152152
latestVotes = Object.values(latest_votes_by_option)
153153
.map((votes) => votes?.[0])
154-
.sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime());
154+
.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());
155155
}
156156
let previewAction = 'created';
157157
let previewUser = created_by;
158-
if (latestVotes && latestVotes.length) {
158+
if (latestVotes && latestVotes.length && latestVotes[0]?.user) {
159159
previewAction = 'voted';
160160
previewUser = latestVotes[0]?.user;
161161
}
@@ -215,7 +215,7 @@ const getLatestMessagePreview = <
215215
>(params: {
216216
channel: Channel<StreamChatGenerics>;
217217
client: StreamChat<StreamChatGenerics>;
218-
pollState: LatestMessagePreviewSelectorReturnType | [];
218+
pollState: LatestMessagePreviewSelectorReturnType | undefined;
219219
readEvents: boolean;
220220
t: TFunction;
221221
lastMessage?:
@@ -308,8 +308,9 @@ export const useLatestMessagePreview = <
308308

309309
const pollId = lastMessage?.poll_id ?? '';
310310
const poll = client.polls.fromState(pollId);
311-
const pollState = useStateStore(poll?.state, selector) ?? [];
312-
const [latest_votes_by_option, created_by] = pollState;
311+
const pollState: LatestMessagePreviewSelectorReturnType =
312+
useStateStore(poll?.state, selector) ?? {};
313+
const { created_by, latest_votes_by_option } = pollState;
313314

314315
useEffect(
315316
() =>

package/src/components/Poll/components/PollOption.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ import { Avatar } from '../../Avatar/Avatar';
2121
import { usePollState } from '../hooks/usePollState';
2222
import { usePollStateStore } from '../hooks/usePollStateStore';
2323

24-
type PollOptionSelectorReturnValue = [Record<string, PollVote[]>, string[]];
24+
type PollOptionSelectorReturnValue = {
25+
latest_votes_by_option: Record<string, PollVote[]>;
26+
maxVotedOptionIds: string[];
27+
};
2528

2629
const selector = <StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics>(
2730
nextValue: PollState<StreamChatGenerics>,
28-
): PollOptionSelectorReturnValue => [nextValue.latest_votes_by_option, nextValue.maxVotedOptionIds];
31+
): PollOptionSelectorReturnValue => ({
32+
latest_votes_by_option: nextValue.latest_votes_by_option,
33+
maxVotedOptionIds: nextValue.maxVotedOptionIds,
34+
});
2935

3036
export type PollOptionProps = {
3137
option: PollOptionClass;
@@ -99,7 +105,7 @@ export const PollOption = ({ option, showProgressBar = true }: PollOptionProps)
99105
}
100106
}, [message.id, option.id, ownVotesByOptionId, poll]);
101107

102-
const [latest_votes_by_option, maxVotedOptionIds] = usePollStateStore(selector);
108+
const { latest_votes_by_option, maxVotedOptionIds } = usePollStateStore(selector);
103109

104110
const relevantVotes = useMemo(
105111
() => latest_votes_by_option?.[option.id]?.slice(0, 2) || [],

package/src/components/Poll/hooks/usePollState.ts

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,60 @@ import { usePollContext } from '../../../contexts';
1515

1616
import { DefaultStreamChatGenerics } from '../../../types/types';
1717

18-
export type UsePollStateSelectorReturnType = [
19-
Record<string, number>,
20-
Record<string, PollVote>,
21-
Record<string, PollVote[]>,
22-
number,
23-
PollAnswer | undefined,
24-
PollOption[],
25-
string,
26-
number,
27-
boolean | undefined,
28-
boolean,
29-
boolean | undefined,
30-
boolean | undefined,
31-
UserResponse | null,
32-
VotingVisibility | undefined,
33-
];
18+
export type UsePollStateSelectorReturnType = {
19+
allow_answers: boolean | undefined;
20+
allow_user_suggested_options: boolean | undefined;
21+
answers_count: number;
22+
created_by: UserResponse | null;
23+
enforce_unique_vote: boolean;
24+
is_closed: boolean | undefined;
25+
latest_votes_by_option: Record<string, PollVote[]>;
26+
max_votes_allowed: number;
27+
name: string;
28+
options: PollOption[];
29+
ownAnswer: PollAnswer | undefined;
30+
ownVotesByOptionId: Record<string, PollVote>;
31+
vote_counts_by_option: Record<string, number>;
32+
voting_visibility: VotingVisibility | undefined;
33+
};
3434

3535
const selector = <StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics>(
3636
nextValue: PollState<StreamChatGenerics>,
37-
): UsePollStateSelectorReturnType => [
38-
nextValue.vote_counts_by_option,
39-
nextValue.ownVotesByOptionId,
40-
nextValue.latest_votes_by_option,
41-
nextValue.answers_count,
42-
nextValue.ownAnswer,
43-
nextValue.options,
44-
nextValue.name,
45-
nextValue.max_votes_allowed,
46-
nextValue.is_closed,
47-
nextValue.enforce_unique_vote,
48-
nextValue.allow_answers,
49-
nextValue.allow_user_suggested_options,
50-
nextValue.created_by,
51-
nextValue.voting_visibility,
52-
];
37+
): UsePollStateSelectorReturnType => ({
38+
allow_answers: nextValue.allow_answers,
39+
allow_user_suggested_options: nextValue.allow_user_suggested_options,
40+
answers_count: nextValue.answers_count,
41+
created_by: nextValue.created_by,
42+
enforce_unique_vote: nextValue.enforce_unique_vote,
43+
is_closed: nextValue.is_closed,
44+
latest_votes_by_option: nextValue.latest_votes_by_option,
45+
max_votes_allowed: nextValue.max_votes_allowed,
46+
name: nextValue.name,
47+
options: nextValue.options,
48+
ownAnswer: nextValue.ownAnswer,
49+
ownVotesByOptionId: nextValue.ownVotesByOptionId,
50+
vote_counts_by_option: nextValue.vote_counts_by_option,
51+
voting_visibility: nextValue.voting_visibility,
52+
});
5353

5454
export const usePollState = () => {
5555
const { message, poll } = usePollContext();
56-
const [
57-
vote_counts_by_option,
58-
ownVotesByOptionId,
59-
latest_votes_by_option,
60-
answers_count,
61-
ownAnswer,
62-
options,
63-
name,
64-
max_votes_allowed,
65-
is_closed,
66-
enforce_unique_vote,
56+
const {
6757
allow_answers,
6858
allow_user_suggested_options,
59+
answers_count,
6960
created_by,
61+
enforce_unique_vote,
62+
is_closed,
63+
latest_votes_by_option,
64+
max_votes_allowed,
65+
name,
66+
options,
67+
ownAnswer,
68+
ownVotesByOptionId,
69+
vote_counts_by_option,
7070
voting_visibility,
71-
] = usePollStateStore(selector);
71+
} = usePollStateStore(selector);
7272

7373
const addOption = useCallback(
7474
async (optionText: string) => {

package/src/components/Poll/hooks/usePollStateStore.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { PollState } from 'stream-chat';
33
import { usePollContext } from '../../../contexts';
44
import { useStateStore } from '../../../hooks';
55

6-
export const usePollStateStore = <T extends readonly unknown[]>(
6+
export const usePollStateStore = <
7+
T extends Readonly<Record<string, unknown> | Readonly<unknown[]>>,
8+
>(
79
selector: (nextValue: PollState) => T,
810
): T => {
911
const { poll } = usePollContext();

0 commit comments

Comments
 (0)