Skip to content

Commit 45e5959

Browse files
authored
feat: rename poll state selector props (#2745)
* feat: rename selector properties * fix: convert PollVote to component
1 parent bf62d33 commit 45e5959

File tree

12 files changed

+111
-119
lines changed

12 files changed

+111
-119
lines changed

docusaurus/docs/reactnative/hooks/poll/use-poll-state-store.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ import { PollVote, PollState } from 'stream-chat';
1616
import { usePollStateStore } from 'stream-chat-react-native';
1717

1818
type PollOptionSelectorReturnValue = {
19-
latest_votes_by_option: Record<string, PollVote[]>;
19+
latestVotesByOption: Record<string, PollVote[]>;
2020
maxVotedOptionIds: string[];
2121
};
2222

2323
const selector = <StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics>(
2424
nextValue: PollState<StreamChatGenerics>,
2525
): PollOptionSelectorReturnValue => ({
26-
latest_votes_by_option: nextValue.latest_votes_by_option,
26+
latestVotesByOption: nextValue.latestVotesByOption,
2727
maxVotedOptionIds: nextValue.maxVotedOptionIds,
2828
});
2929

30-
const { latest_votes_by_option, maxVotedOptionIds } = usePollStateStore(selector);
30+
const { latestVotesByOption, maxVotedOptionIds } = usePollStateStore(selector);
3131
```

docusaurus/docs/reactnative/hooks/poll/use-poll-state.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ It is highly recommended that the user familiarizes themselves with the [`Poll`
1515

1616
The `usePollState` hook returns an object containing values from the `PollState`.
1717

18-
### `allow_answers`
18+
### `allowAnswers`
1919

2020
A property depicting whether answers (comments) are allowed to the `poll`.
2121

2222
| Type |
2323
| --------- | ----------- |
2424
| `boolean` | `undefined` |
2525

26-
### `allow_user_suggested_options`
26+
### `allowUserSuggestedOptions`
2727

2828
A property depicting whether user option suggestions are allowed to the `poll`.
2929

3030
| Type |
3131
| --------- | ----------- |
3232
| `boolean` | `undefined` |
3333

34-
### `answers_count`
34+
### `answersCount`
3535

3636
A property containing the number of answers (comments) to the `poll`.
3737

@@ -49,7 +49,7 @@ This property will be `null` for anonymous polls.
4949
| -------- | ------ |
5050
| `object` | `null` |
5151

52-
### `enforce_unique_vote`
52+
### `enforceUniqueVote`
5353

5454
A property depicting whether each user should have only one and unique vote or they would be able to vote multiple times during the lifespan of a `poll`.
5555

@@ -65,7 +65,7 @@ A property depicting whether the `poll` is still open for voting or not.
6565
| --------- |
6666
| `boolean` |
6767

68-
### `latest_votes_by_option`
68+
### `latestVotesByOption`
6969

7070
A property containing the latest votes for each option stored in an object by option ID. Only the last 10 votes are maintained here. If all votes are needed for an option, the [`usePollOptionVotesPagination`](./use-poll-option-votes-pagination.mdx) hook should be used instead.
7171

@@ -123,15 +123,15 @@ A property containing the current user's own votes in a given `poll`, stored in
123123
| -------------------------- |
124124
| `Record<string, PollVote>` |
125125

126-
### `vote_counts_by_option`
126+
### `voteCountsByOption`
127127

128128
A property containing the vote counts in a given `poll`, stored in an object by option ID.
129129

130130
| Type |
131131
| ------------------------ |
132132
| `Record<string, number>` |
133133

134-
### `voting_visibility`
134+
### `votingVisibility`
135135

136136
A property depicting the visibility of votes in a given `poll`. It conforms to the `VotingVisibility` enum found [here](https://github.com/GetStream/stream-chat-js/blob/b447512922b19bc7e3668bd9df81debcb673dd81/src/types.ts).
137137

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ export type LatestMessagePreview<
3838
};
3939

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

4545
const selector = <StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics>(
4646
nextValue: PollState<StreamChatGenerics>,
4747
): LatestMessagePreviewSelectorReturnType => ({
48-
created_by: nextValue.created_by,
49-
latest_votes_by_option: nextValue.latest_votes_by_option,
48+
createdBy: nextValue.created_by,
49+
latestVotesByOption: nextValue.latest_votes_by_option,
5050
});
5151

5252
const getMessageSenderName = <
@@ -146,15 +146,15 @@ const getLatestMessageDisplayText = <
146146
];
147147
}
148148
if (message.poll && pollState) {
149-
const { created_by, latest_votes_by_option } = pollState;
149+
const { createdBy, latestVotesByOption } = pollState;
150150
let latestVotes;
151-
if (latest_votes_by_option) {
152-
latestVotes = Object.values(latest_votes_by_option)
151+
if (latestVotesByOption) {
152+
latestVotes = Object.values(latestVotesByOption)
153153
.map((votes) => votes?.[0])
154154
.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());
155155
}
156156
let previewAction = 'created';
157-
let previewUser = created_by;
157+
let previewUser = createdBy;
158158
if (latestVotes && latestVotes.length && latestVotes[0]?.user) {
159159
previewAction = 'voted';
160160
previewUser = latestVotes[0]?.user;
@@ -310,7 +310,7 @@ export const useLatestMessagePreview = <
310310
const poll = client.polls.fromState(pollId);
311311
const pollState: LatestMessagePreviewSelectorReturnType =
312312
useStateStore(poll?.state, selector) ?? {};
313-
const { created_by, latest_votes_by_option } = pollState;
313+
const { createdBy, latestVotesByOption } = pollState;
314314

315315
useEffect(
316316
() =>
@@ -325,14 +325,7 @@ export const useLatestMessagePreview = <
325325
}),
326326
),
327327
// eslint-disable-next-line react-hooks/exhaustive-deps
328-
[
329-
channelLastMessageString,
330-
forceUpdate,
331-
readEvents,
332-
readStatus,
333-
latest_votes_by_option,
334-
created_by,
335-
],
328+
[channelLastMessageString, forceUpdate, readEvents, readStatus, latestVotesByOption, createdBy],
336329
);
337330

338331
return latestMessagePreview;

package/src/components/Poll/Poll.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ export const PollButtons = () => (
4747

4848
export const PollHeader = () => {
4949
const { t } = useTranslationContext();
50-
const { enforce_unique_vote, is_closed, max_votes_allowed, name } = usePollState();
50+
const { enforceUniqueVote, isClosed, maxVotesAllowed, name } = usePollState();
5151
const subtitle = useMemo(() => {
52-
if (is_closed) return t('Vote ended');
53-
if (enforce_unique_vote) return t('Select one');
54-
if (max_votes_allowed) return t('Select up to {{count}}', { count: max_votes_allowed });
52+
if (isClosed) return t('Vote ended');
53+
if (enforceUniqueVote) return t('Select one');
54+
if (maxVotesAllowed) return t('Select up to {{count}}', { count: maxVotesAllowed });
5555
return t('Select one or more');
56-
}, [is_closed, t, enforce_unique_vote, max_votes_allowed]);
56+
}, [isClosed, t, enforceUniqueVote, maxVotesAllowed]);
5757

5858
const {
5959
theme: {

package/src/components/Poll/components/Button.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,18 @@ export const ViewResultsButton = (props: PollButtonProps) => {
115115

116116
export const EndVoteButton = () => {
117117
const { t } = useTranslationContext();
118-
const { created_by, endVote, is_closed } = usePollState();
118+
const { createdBy, endVote, isClosed } = usePollState();
119119
const { client } = useChatContext();
120120

121-
return !is_closed && created_by?.id === client.userID ? (
121+
return !isClosed && createdBy?.id === client.userID ? (
122122
<GenericPollButton onPress={endVote} title={t<string>('End Vote')} />
123123
) : null;
124124
};
125125

126126
export const AddCommentButton = (props: PollButtonProps) => {
127127
const { t } = useTranslationContext();
128128
const { message, poll } = usePollContext();
129-
const { addComment, allow_answers, is_closed, ownAnswer } = usePollState();
129+
const { addComment, allowAnswers, isClosed, ownAnswer } = usePollState();
130130
const [showAddCommentDialog, setShowAddCommentDialog] = useState(false);
131131
const { onPress } = props;
132132

@@ -141,7 +141,7 @@ export const AddCommentButton = (props: PollButtonProps) => {
141141

142142
return (
143143
<>
144-
{!is_closed && allow_answers ? (
144+
{!isClosed && allowAnswers ? (
145145
<GenericPollButton onPress={onPressHandler} title={t<string>('Add a comment')} />
146146
) : null}
147147
{showAddCommentDialog ? (
@@ -160,7 +160,7 @@ export const AddCommentButton = (props: PollButtonProps) => {
160160
export const ShowAllCommentsButton = (props: PollButtonProps) => {
161161
const { t } = useTranslationContext();
162162
const { message, poll } = usePollContext();
163-
const { answers_count } = usePollState();
163+
const { answersCount } = usePollState();
164164
const [showAnswers, setShowAnswers] = useState(false);
165165
const { onPress } = props;
166166

@@ -181,10 +181,10 @@ export const ShowAllCommentsButton = (props: PollButtonProps) => {
181181

182182
return (
183183
<>
184-
{answers_count && answers_count > 0 ? (
184+
{answersCount && answersCount > 0 ? (
185185
<GenericPollButton
186186
onPress={onPressHandler}
187-
title={t<string>('View {{count}} comments', { count: answers_count })}
187+
title={t<string>('View {{count}} comments', { count: answersCount })}
188188
/>
189189
) : null}
190190
{showAnswers ? (
@@ -262,7 +262,7 @@ export const AnswerListAddCommentButton = (props: PollButtonProps) => {
262262
export const SuggestOptionButton = (props: PollButtonProps) => {
263263
const { t } = useTranslationContext();
264264
const { message, poll } = usePollContext();
265-
const { addOption, allow_user_suggested_options, is_closed } = usePollState();
265+
const { addOption, allowUserSuggestedOptions, isClosed } = usePollState();
266266
const [showAddOptionDialog, setShowAddOptionDialog] = useState(false);
267267
const { onPress } = props;
268268

@@ -277,7 +277,7 @@ export const SuggestOptionButton = (props: PollButtonProps) => {
277277

278278
return (
279279
<>
280-
{!is_closed && allow_user_suggested_options ? (
280+
{!isClosed && allowUserSuggestedOptions ? (
281281
<GenericPollButton onPress={onPressHandler} title={t<string>('Suggest an option')} />
282282
) : null}
283283
{showAddOptionDialog ? (
@@ -343,7 +343,7 @@ export const ShowAllOptionsButton = (props: PollButtonProps) => {
343343

344344
export const VoteButton = ({ onPress, option }: PollVoteButtonProps) => {
345345
const { message, poll } = usePollContext();
346-
const { is_closed, ownVotesByOptionId } = usePollState();
346+
const { isClosed, ownVotesByOptionId } = usePollState();
347347
const ownCapabilities = useOwnCapabilitiesContext();
348348

349349
const {
@@ -374,7 +374,7 @@ export const VoteButton = ({ onPress, option }: PollVoteButtonProps) => {
374374
toggleVote();
375375
}, [message, onPress, poll, toggleVote]);
376376

377-
return ownCapabilities.castPollVote && !is_closed ? (
377+
return ownCapabilities.castPollVote && !isClosed ? (
378378
<TouchableOpacity
379379
onPress={onPressHandler}
380380
style={[
@@ -398,7 +398,7 @@ export const VoteButton = ({ onPress, option }: PollVoteButtonProps) => {
398398
export const ShowAllVotesButton = (props: ShowAllVotesButtonProps) => {
399399
const { t } = useTranslationContext();
400400
const { message, poll } = usePollContext();
401-
const { vote_counts_by_option } = usePollState();
401+
const { voteCountsByOption } = usePollState();
402402
const ownCapabilities = useOwnCapabilitiesContext();
403403
const [showAllVotes, setShowAllVotes] = useState(false);
404404
const { onPress, option } = props;
@@ -421,8 +421,8 @@ export const ShowAllVotesButton = (props: ShowAllVotesButtonProps) => {
421421
return (
422422
<>
423423
{ownCapabilities.queryPollVotes &&
424-
vote_counts_by_option &&
425-
vote_counts_by_option?.[option.id] > 5 ? (
424+
voteCountsByOption &&
425+
voteCountsByOption?.[option.id] > 5 ? (
426426
<GenericPollButton onPress={onPressHandler} title={t<string>('Show All')} />
427427
) : null}
428428
{showAllVotes ? (

package/src/components/Poll/components/PollAnswersList.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export type PollAnswersListProps<
2626

2727
export const PollAnswerListItem = ({ answer }: { answer: PollAnswer }) => {
2828
const { t, tDateTimeParser } = useTranslationContext();
29-
const { voting_visibility } = usePollState();
29+
const { votingVisibility } = usePollState();
3030

3131
const {
3232
theme: {
@@ -49,8 +49,8 @@ export const PollAnswerListItem = ({ answer }: { answer: PollAnswer }) => {
4949
);
5050

5151
const isAnonymous = useMemo(
52-
() => voting_visibility === VotingVisibility.anonymous,
53-
[voting_visibility],
52+
() => votingVisibility === VotingVisibility.anonymous,
53+
[votingVisibility],
5454
);
5555

5656
return (

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,18 @@ export const PollAllOptions = ({
7373
);
7474

7575
export const PollOption = ({ option, showProgressBar = true }: PollOptionProps) => {
76-
const { is_closed, latest_votes_by_option, maxVotedOptionIds, vote_counts_by_option } =
77-
usePollState();
76+
const { isClosed, latestVotesByOption, maxVotedOptionIds, voteCountsByOption } = usePollState();
7877

7978
const relevantVotes = useMemo(
80-
() => latest_votes_by_option?.[option.id]?.slice(0, 2) || [],
81-
[latest_votes_by_option, option.id],
79+
() => latestVotesByOption?.[option.id]?.slice(0, 2) || [],
80+
[latestVotesByOption, option.id],
8281
);
8382
const maxVotes = useMemo(
8483
() =>
85-
maxVotedOptionIds?.[0] && vote_counts_by_option
86-
? vote_counts_by_option[maxVotedOptionIds[0]]
87-
: 0,
88-
[maxVotedOptionIds, vote_counts_by_option],
84+
maxVotedOptionIds?.[0] && voteCountsByOption ? voteCountsByOption[maxVotedOptionIds[0]] : 0,
85+
[maxVotedOptionIds, voteCountsByOption],
8986
);
90-
const votes = vote_counts_by_option[option.id] || 0;
87+
const votes = voteCountsByOption[option.id] || 0;
9188

9289
const {
9390
theme: {
@@ -118,17 +115,15 @@ export const PollOption = ({ option, showProgressBar = true }: PollOptionProps)
118115
{relevantVotes.map((vote: PollVote) => (
119116
<Avatar image={vote.user?.image as string} key={vote.id} size={20} />
120117
))}
121-
<Text style={{ color: black, marginLeft: 2 }}>
122-
{vote_counts_by_option[option.id] || 0}
123-
</Text>
118+
<Text style={{ color: black, marginLeft: 2 }}>{voteCountsByOption[option.id] || 0}</Text>
124119
</View>
125120
</View>
126121
{showProgressBar ? (
127122
<View style={[styles.progressBar, progressBar]}>
128123
<View
129124
style={{
130125
backgroundColor:
131-
is_closed && maxVotedOptionIds.length === 1 && maxVotedOptionIds[0] === option.id
126+
isClosed && maxVotedOptionIds.length === 1 && maxVotedOptionIds[0] === option.id
132127
? progressBarWinnerFill || accent_info
133128
: progressBarVotedFill || accent_dark_blue,
134129
flex: maxVotes > 0 ? votes / maxVotes : 0,

package/src/components/Poll/components/PollResults/PollOptionFullResults.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type PollOptionFullResultsProps<
2424
};
2525

2626
export const PollOptionFullResultsItem = ({ item }: { item: PollVoteClass }) => (
27-
<PollVote {...item} />
27+
<PollVote vote={item} />
2828
);
2929

3030
export const PollOptionFullResultsContent = ({
@@ -33,7 +33,7 @@ export const PollOptionFullResultsContent = ({
3333
}: Pick<PollOptionFullResultsProps, 'option' | 'additionalFlatListProps'>) => {
3434
const { t } = useTranslationContext();
3535
const { hasNextPage, loadMore, votes } = usePollOptionVotesPagination({ option });
36-
const { vote_counts_by_option } = usePollState();
36+
const { voteCountsByOption } = usePollState();
3737

3838
const {
3939
theme: {
@@ -48,11 +48,11 @@ export const PollOptionFullResultsContent = ({
4848
() => (
4949
<View style={[styles.headerContainer, headerContainer]}>
5050
<Text style={[styles.headerText, { color: black }, headerText]}>
51-
{t<string>('{{count}} votes', { count: vote_counts_by_option[option.id] ?? 0 })}
51+
{t<string>('{{count}} votes', { count: voteCountsByOption[option.id] ?? 0 })}
5252
</Text>
5353
</View>
5454
),
55-
[black, headerContainer, headerText, option.id, t, vote_counts_by_option],
55+
[black, headerContainer, headerText, option.id, t, voteCountsByOption],
5656
);
5757

5858
return (

0 commit comments

Comments
 (0)