-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathPollOption.tsx
More file actions
297 lines (268 loc) · 8.83 KB
/
PollOption.tsx
File metadata and controls
297 lines (268 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import React, { useCallback, useMemo } from 'react';
import { Pressable, ScrollViewProps, StyleSheet, Text, View } from 'react-native';
import { ScrollView } from 'react-native-gesture-handler';
import { PollOption as PollOptionClass, PollVote, UserResponse } from 'stream-chat';
import { PollVoteButtonProps } from './Button';
import {
PollContextProvider,
PollContextValue,
useOwnCapabilitiesContext,
usePollContext,
useTheme,
useTranslationContext,
} from '../../../contexts';
import { Check } from '../../../icons';
import { primitives } from '../../../theme';
import { ProgressBar } from '../../ProgressControl/ProgressBar';
import { UserAvatarStack } from '../../ui/Avatar/AvatarStack';
import { useIsPollCreatedByCurrentUser } from '../hook/useIsPollCreatedByCurrentUser';
import { usePollState } from '../hooks/usePollState';
export type PollOptionProps = {
option: PollOptionClass;
showProgressBar?: boolean;
forceIncoming?: boolean;
};
export type PollAllOptionsContentProps = PollContextValue & {
additionalScrollViewProps?: Partial<ScrollViewProps>;
PollAllOptionsContent?: React.ComponentType;
};
export const PollAllOptionsContent = ({
additionalScrollViewProps,
}: Pick<PollAllOptionsContentProps, 'additionalScrollViewProps'>) => {
const { t } = useTranslationContext();
const { name, options } = usePollState();
const {
theme: {
poll: {
allOptions: { listContainer, titleContainer, titleText, wrapper },
},
},
} = useTheme();
const styles = useAllOptionStyles();
return (
<ScrollView style={[styles.allOptionsWrapper, wrapper]} {...additionalScrollViewProps}>
<View style={[styles.allOptionsTitleContainer, titleContainer]}>
<Text style={styles.allOptionsTitleMeta}>{t('Question')}</Text>
<Text style={[styles.allOptionsTitleText, titleText]}>{name}</Text>
</View>
<View style={[styles.allOptionsListContainer, listContainer]}>
{options?.map((option: PollOptionClass) => (
<View key={`full_poll_options_${option.id}`} style={styles.optionWrapper}>
<PollOption key={option.id} option={option} forceIncoming />
</View>
))}
</View>
</ScrollView>
);
};
export const PollAllOptions = ({
additionalScrollViewProps,
message,
poll,
PollAllOptionsContent: PollAllOptionsContentOverride,
}: PollAllOptionsContentProps) => (
<PollContextProvider value={{ message, poll }}>
{PollAllOptionsContentOverride ? (
<PollAllOptionsContentOverride />
) : (
<PollAllOptionsContent additionalScrollViewProps={additionalScrollViewProps} />
)}
</PollContextProvider>
);
export const PollOption = ({ option, showProgressBar = true, forceIncoming }: PollOptionProps) => {
const { latestVotesByOption, voteCountsByOption, voteCount } = usePollState();
const styles = useStyles();
const relevantVotes = useMemo(
() => latestVotesByOption?.[option.id] || [],
[latestVotesByOption, option.id],
);
const votes = voteCountsByOption[option.id] || 0;
const {
theme: {
poll: {
message: {
option: { text, votesContainer, container, info, header, votesText },
},
},
semantics,
},
} = useTheme();
const isPollCreatedByClient = useIsPollCreatedByCurrentUser();
const unFilledColor =
isPollCreatedByClient && !forceIncoming
? semantics.chatPollProgressTrackOutgoing
: semantics.chatPollProgressTrackIncoming;
const filledColor =
isPollCreatedByClient && !forceIncoming
? semantics.chatPollProgressFillOutgoing
: semantics.chatPollProgressFillIncoming;
return (
<View style={[styles.container, container]}>
<VoteButton option={option} />
<View style={[styles.info, info]}>
<View style={[styles.header, header]}>
<Text style={[styles.text, text]}>{option.text}</Text>
<View style={[styles.votesContainer, votesContainer]}>
<UserAvatarStack
users={relevantVotes.map((vote: PollVote) => vote.user as UserResponse)}
overlap={0.2}
maxVisible={3}
avatarSize='xs'
/>
<Text style={[styles.votesText, votesText]}>{voteCountsByOption[option.id] || 0}</Text>
</View>
</View>
{showProgressBar ? (
<View style={styles.progressBarContainer}>
<ProgressBar
progress={votes / voteCount}
filledColor={filledColor}
emptyColor={unFilledColor}
/>
</View>
) : null}
</View>
</View>
);
};
export const VoteButton = ({ onPress, option }: PollVoteButtonProps) => {
const { message, poll } = usePollContext();
const { isClosed, ownVotesByOptionId } = usePollState();
const ownCapabilities = useOwnCapabilitiesContext();
const {
theme: { semantics },
} = useTheme();
const isPollCreatedByClient = useIsPollCreatedByCurrentUser();
const styles = useStyles();
const {
theme: {
poll: {
message: {
option: { voteButtonContainer },
},
},
},
} = useTheme();
const toggleVote = useCallback(async () => {
if (ownVotesByOptionId[option.id]) {
await poll.removeVote(ownVotesByOptionId[option.id]?.id, message.id);
} else {
await poll.castVote(option.id, message.id);
}
}, [message.id, option.id, ownVotesByOptionId, poll]);
const onPressHandler = useCallback(() => {
if (onPress) {
onPress({ message, poll });
return;
}
toggleVote();
}, [message, onPress, poll, toggleVote]);
const hasVote = !!ownVotesByOptionId[option.id];
return ownCapabilities.castPollVote && !isClosed ? (
<Pressable
onPress={onPressHandler}
style={({ pressed }) => [
{ opacity: pressed ? 0.5 : 1 },
styles.voteContainer,
{
borderWidth: hasVote ? 0 : 1,
backgroundColor: hasVote ? semantics.accentPrimary : 'transparent',
borderColor: isPollCreatedByClient
? semantics.chatBorderOnChatOutgoing
: semantics.chatBorderOnChatIncoming,
},
voteButtonContainer,
]}
>
{hasVote ? <Check height={20} stroke={semantics.textOnAccent} width={20} /> : null}
</Pressable>
) : null;
};
const useStyles = () => {
const {
theme: { semantics },
} = useTheme();
return useMemo(() => {
return StyleSheet.create({
container: {
flexDirection: 'row',
gap: primitives.spacingXs,
alignItems: 'center',
},
header: {
flexDirection: 'row',
justifyContent: 'space-between',
},
text: {
color: semantics.chatTextIncoming,
fontSize: primitives.typographyFontSizeSm,
fontWeight: primitives.typographyFontWeightRegular,
lineHeight: primitives.typographyLineHeightTight,
},
info: {
flexGrow: 1,
gap: primitives.spacingXs,
},
votesContainer: { flexDirection: 'row', gap: primitives.spacingXs, alignItems: 'center' },
votesText: {
color: semantics.chatTextIncoming,
fontSize: primitives.typographyFontSizeXs,
fontWeight: primitives.typographyFontWeightRegular,
lineHeight: primitives.typographyLineHeightTight,
},
progressBarContainer: {
flex: 1,
},
voteContainer: {
borderRadius: primitives.radiusMax,
height: 24,
justifyContent: 'center',
alignItems: 'center',
width: 24,
},
});
}, [semantics]);
};
const useAllOptionStyles = () => {
const {
theme: { semantics },
} = useTheme();
return useMemo(
() =>
StyleSheet.create({
allOptionsListContainer: {
borderRadius: primitives.radiusLg,
padding: primitives.spacingMd,
backgroundColor: semantics.backgroundCoreSurfaceCard,
marginTop: primitives.spacing2xl,
},
allOptionsTitleContainer: {
borderRadius: primitives.radiusLg,
padding: primitives.spacingMd,
backgroundColor: semantics.backgroundCoreSurfaceCard,
},
allOptionsTitleText: {
fontSize: primitives.typographyFontSizeLg,
lineHeight: primitives.typographyLineHeightRelaxed,
fontWeight: primitives.typographyFontWeightSemiBold,
color: semantics.textPrimary,
paddingTop: primitives.spacingXs,
},
allOptionsTitleMeta: {
fontSize: primitives.typographyFontSizeSm,
color: semantics.textTertiary,
lineHeight: primitives.typographyLineHeightNormal,
fontWeight: primitives.typographyFontWeightMedium,
},
allOptionsWrapper: {
flex: 1,
padding: primitives.spacingMd,
backgroundColor: semantics.backgroundCoreElevation1,
},
optionWrapper: {
paddingVertical: primitives.spacingMd,
},
}),
[semantics],
);
};