tooot/src/components/Timeline/Shared/Poll.tsx

348 lines
9.8 KiB
TypeScript
Raw Normal View History

2021-01-24 02:25:43 +01:00
import analytics from '@components/analytics'
2020-12-26 23:05:17 +01:00
import Button from '@components/Button'
2021-01-01 16:48:16 +01:00
import haptics from '@components/haptics'
import Icon from '@components/Icon'
2021-02-28 22:49:55 +01:00
import { displayMessage } from '@components/Message'
2021-01-17 22:37:05 +01:00
import { ParseEmojis } from '@components/Parse'
2021-01-14 00:43:35 +01:00
import RelativeTime from '@components/RelativeTime'
2021-01-11 21:36:57 +01:00
import {
2021-01-19 01:13:45 +01:00
MutationVarsTimelineUpdateStatusProperty,
2021-01-11 21:36:57 +01:00
QueryKeyTimeline,
useTimelineMutation
} from '@utils/queryHooks/timeline'
2021-02-13 01:26:02 +01:00
import updateStatusProperty from '@utils/queryHooks/timeline/updateStatusProperty'
2020-12-13 14:04:25 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2021-01-13 01:03:46 +01:00
import { maxBy } from 'lodash'
2021-01-01 16:48:16 +01:00
import React, { useCallback, useMemo, useState } from 'react'
2021-01-13 01:03:46 +01:00
import { Trans, useTranslation } from 'react-i18next'
2021-01-01 16:48:16 +01:00
import { Pressable, StyleSheet, Text, View } from 'react-native'
2021-01-11 21:36:57 +01:00
import { useQueryClient } from 'react-query'
2020-10-29 14:52:28 +01:00
2020-10-31 21:04:46 +01:00
export interface Props {
2021-01-07 19:13:09 +01:00
queryKey: QueryKeyTimeline
2021-02-13 01:26:02 +01:00
rootQueryKey?: QueryKeyTimeline
2021-01-11 21:36:57 +01:00
statusId: Mastodon.Status['id']
2020-12-19 18:21:37 +01:00
poll: NonNullable<Mastodon.Status['poll']>
reblog: boolean
2020-12-20 17:53:24 +01:00
sameAccount: boolean
2020-10-31 21:04:46 +01:00
}
2020-12-03 01:28:56 +01:00
2020-12-20 17:53:24 +01:00
const TimelinePoll: React.FC<Props> = ({
queryKey,
2021-02-13 01:26:02 +01:00
rootQueryKey,
2021-01-11 21:36:57 +01:00
statusId,
2020-12-20 17:53:24 +01:00
poll,
reblog,
sameAccount
}) => {
2021-01-01 16:48:16 +01:00
const { mode, theme } = useTheme()
2021-03-22 00:04:47 +01:00
const { t, i18n } = useTranslation('componentTimeline')
2020-12-26 23:05:17 +01:00
const [allOptions, setAllOptions] = useState(
new Array(poll.options.length).fill(false)
)
const queryClient = useQueryClient()
2021-01-11 21:36:57 +01:00
const mutation = useTimelineMutation({
queryClient,
2021-02-13 01:26:02 +01:00
onSuccess: ({ body }, params) => {
const theParams = params as MutationVarsTimelineUpdateStatusProperty
queryClient.cancelQueries(queryKey)
rootQueryKey && queryClient.cancelQueries(rootQueryKey)
haptics('Success')
switch (theParams.payload.property) {
case 'poll':
theParams.payload.data = (body as unknown) as Mastodon.Poll
updateStatusProperty({ queryClient, ...theParams })
break
}
},
2021-01-19 01:13:45 +01:00
onError: (err: any, params) => {
const theParams = params as MutationVarsTimelineUpdateStatusProperty
2021-02-28 22:49:55 +01:00
displayMessage({
mode,
type: 'error',
2021-01-19 01:13:45 +01:00
message: t('common:toastMessage.error.message', {
2021-01-24 02:25:43 +01:00
// @ts-ignore
2021-01-19 01:13:45 +01:00
function: t(`shared.poll.meta.button.${theParams.payload.type}`)
}),
...(err.status &&
typeof err.status === 'number' &&
err.data &&
err.data.error &&
typeof err.data.error === 'string' && {
description: err.data.error
2021-01-19 01:13:45 +01:00
})
})
queryClient.invalidateQueries(queryKey)
2020-12-03 01:28:56 +01:00
}
})
2021-01-01 16:48:16 +01:00
const pollButton = useMemo(() => {
2020-12-20 17:53:24 +01:00
if (!poll.expired) {
if (!sameAccount && !poll.voted) {
return (
<View style={styles.button}>
2020-12-26 23:05:17 +01:00
<Button
2021-01-24 02:25:43 +01:00
onPress={() => {
analytics('timeline_shared_vote_vote_press')
2021-01-11 21:36:57 +01:00
mutation.mutate({
type: 'updateStatusProperty',
queryKey,
2021-02-13 01:26:02 +01:00
rootQueryKey,
2021-01-11 21:36:57 +01:00
id: statusId,
reblog,
payload: {
property: 'poll',
id: poll.id,
type: 'vote',
options: allOptions
}
})
2021-01-24 02:25:43 +01:00
}}
2020-12-26 23:05:17 +01:00
type='text'
2021-01-01 23:10:47 +01:00
content={t('shared.poll.meta.button.vote')}
2020-12-26 23:05:17 +01:00
loading={mutation.isLoading}
disabled={allOptions.filter(o => o !== false).length === 0}
2020-12-20 17:53:24 +01:00
/>
</View>
)
} else {
return (
<View style={styles.button}>
2020-12-26 23:05:17 +01:00
<Button
2021-01-24 02:25:43 +01:00
onPress={() => {
analytics('timeline_shared_vote_refresh_press')
2021-01-11 21:36:57 +01:00
mutation.mutate({
type: 'updateStatusProperty',
queryKey,
2021-02-13 01:26:02 +01:00
rootQueryKey,
2021-01-11 21:36:57 +01:00
id: statusId,
reblog,
payload: {
property: 'poll',
id: poll.id,
type: 'refresh'
}
})
2021-01-24 02:25:43 +01:00
}}
2020-12-26 23:05:17 +01:00
type='text'
2021-01-01 23:10:47 +01:00
content={t('shared.poll.meta.button.refresh')}
2020-12-26 23:05:17 +01:00
loading={mutation.isLoading}
2020-12-20 17:53:24 +01:00
/>
</View>
)
}
}
2021-03-22 00:04:47 +01:00
}, [
mode,
i18n.language,
poll.expired,
poll.voted,
allOptions,
mutation.isLoading
])
2020-12-20 17:53:24 +01:00
2020-12-03 01:28:56 +01:00
const pollExpiration = useMemo(() => {
2020-12-18 23:58:53 +01:00
if (poll.expired) {
2020-12-03 01:28:56 +01:00
return (
<Text style={[styles.expiration, { color: theme.secondary }]}>
2021-01-01 23:10:47 +01:00
{t('shared.poll.meta.expiration.expired')}
2020-12-03 01:28:56 +01:00
</Text>
)
} else {
return (
<Text style={[styles.expiration, { color: theme.secondary }]}>
2021-01-13 01:03:46 +01:00
<Trans
2021-01-20 00:39:39 +01:00
i18nKey='componentTimeline:shared.poll.meta.expiration.until'
2021-01-14 00:43:35 +01:00
components={[<RelativeTime date={poll.expires_at} />]}
2021-01-13 01:03:46 +01:00
/>
2020-12-03 01:28:56 +01:00
</Text>
)
}
2021-03-22 00:04:47 +01:00
}, [mode, i18n.language, poll.expired, poll.expires_at])
2020-12-03 01:28:56 +01:00
2020-12-26 23:05:17 +01:00
const isSelected = useCallback(
2021-01-13 01:03:46 +01:00
(index: number): string =>
2020-12-26 23:05:17 +01:00
allOptions[index]
? `Check${poll.multiple ? 'Square' : 'Circle'}`
: `${poll.multiple ? 'Square' : 'Circle'}`,
2020-12-26 23:05:17 +01:00
[allOptions]
)
2020-12-03 01:28:56 +01:00
2021-01-01 16:48:16 +01:00
const pollBodyDisallow = useMemo(() => {
2021-01-13 01:03:46 +01:00
const maxValue = maxBy(poll.options, option => option.votes_count)
?.votes_count
2021-01-01 16:48:16 +01:00
return poll.options.map((option, index) => (
<View key={index} style={styles.optionContainer}>
<View style={styles.optionContent}>
<Icon
2021-01-01 16:48:16 +01:00
style={styles.optionSelection}
name={
`${poll.own_votes?.includes(index) ? 'Check' : ''}${
poll.multiple ? 'Square' : 'Circle'
2021-01-01 16:48:16 +01:00
}` as any
}
size={StyleConstants.Font.Size.M}
color={
2021-01-13 01:03:46 +01:00
poll.own_votes?.includes(index) ? theme.blue : theme.disabled
2021-01-01 16:48:16 +01:00
}
/>
<Text style={styles.optionText}>
<ParseEmojis content={option.title} emojis={poll.emojis} />
</Text>
2021-03-22 00:04:47 +01:00
<Text
style={[styles.optionPercentage, { color: theme.primaryDefault }]}
>
2021-01-01 16:48:16 +01:00
{poll.votes_count
2021-01-13 01:03:46 +01:00
? Math.round(
(option.votes_count /
(poll.voters_count || poll.votes_count)) *
100
)
2021-01-01 16:48:16 +01:00
: 0}
%
</Text>
</View>
<View
style={[
styles.background,
{
width: `${Math.round(
2021-01-13 01:03:46 +01:00
(option.votes_count / (poll.voters_count || poll.votes_count)) *
100
2021-01-01 16:48:16 +01:00
)}%`,
2021-01-13 01:03:46 +01:00
backgroundColor:
option.votes_count === maxValue ? theme.blue : theme.disabled
2021-01-01 16:48:16 +01:00
}
]}
/>
</View>
))
}, [mode, poll.options])
const pollBodyAllow = useMemo(() => {
return poll.options.map((option, index) => (
<Pressable
key={index}
style={styles.optionContainer}
onPress={() => {
2021-01-24 02:25:43 +01:00
analytics('timeline_shared_vote_option_press')
2021-01-01 16:48:16 +01:00
haptics('Light')
if (poll.multiple) {
setAllOptions(allOptions.map((o, i) => (i === index ? !o : o)))
} else {
{
const otherOptions =
allOptions[index] === false ? false : undefined
setAllOptions(
allOptions.map((o, i) =>
i === index
? !o
: otherOptions !== undefined
? otherOptions
: o
)
)
}
}
}}
>
<View style={[styles.optionContent]}>
<Icon
2021-01-01 16:48:16 +01:00
style={styles.optionSelection}
name={isSelected(index)}
2021-01-17 22:37:05 +01:00
size={StyleConstants.Font.Size.M}
color={theme.primaryDefault}
2021-01-01 16:48:16 +01:00
/>
<Text style={styles.optionText}>
<ParseEmojis content={option.title} emojis={poll.emojis} />
</Text>
</View>
</Pressable>
))
}, [mode, allOptions])
2021-01-13 01:03:46 +01:00
const pollVoteCounts = useMemo(() => {
if (poll.voters_count !== null) {
return (
<Text style={[styles.votes, { color: theme.secondary }]}>
{t('shared.poll.meta.count.voters', { count: poll.voters_count })}
</Text>
)
} else if (poll.votes_count !== null) {
return (
<Text style={[styles.votes, { color: theme.secondary }]}>
{t('shared.poll.meta.count.votes', { count: poll.votes_count })}
</Text>
)
}
}, [poll.voters_count, poll.votes_count])
2020-10-29 14:52:28 +01:00
return (
2020-12-03 01:28:56 +01:00
<View style={styles.base}>
2021-01-01 16:48:16 +01:00
{poll.expired || poll.voted ? pollBodyDisallow : pollBodyAllow}
2020-12-03 01:28:56 +01:00
<View style={styles.meta}>
2021-01-01 16:48:16 +01:00
{pollButton}
2021-01-13 01:03:46 +01:00
{pollVoteCounts}
2020-12-03 01:28:56 +01:00
{pollExpiration}
</View>
2020-10-29 14:52:28 +01:00
</View>
)
}
const styles = StyleSheet.create({
2020-12-03 01:28:56 +01:00
base: {
marginTop: StyleConstants.Spacing.M
},
2021-01-01 16:48:16 +01:00
optionContainer: {
2020-12-26 23:05:17 +01:00
flex: 1,
2021-01-01 16:48:16 +01:00
paddingVertical: StyleConstants.Spacing.S
2020-12-03 01:28:56 +01:00
},
2021-01-01 16:48:16 +01:00
optionContent: {
2020-12-03 01:28:56 +01:00
flex: 1,
flexDirection: 'row'
},
2021-01-01 16:48:16 +01:00
optionText: {
flex: 1
2020-12-03 01:28:56 +01:00
},
2021-01-01 16:48:16 +01:00
optionSelection: {
2021-01-17 22:37:05 +01:00
paddingTop: StyleConstants.Font.LineHeight.M - StyleConstants.Font.Size.M,
2020-12-26 23:05:17 +01:00
marginRight: StyleConstants.Spacing.S
2020-12-03 01:28:56 +01:00
},
2021-01-01 16:48:16 +01:00
optionPercentage: {
...StyleConstants.FontStyle.M,
alignSelf: 'center',
2021-01-01 23:10:47 +01:00
marginLeft: StyleConstants.Spacing.S,
flexBasis: '20%',
textAlign: 'center'
2020-12-03 01:28:56 +01:00
},
background: {
2020-12-26 23:05:17 +01:00
height: StyleConstants.Spacing.XS,
2020-12-20 17:53:24 +01:00
minWidth: 2,
2020-12-26 23:05:17 +01:00
borderTopRightRadius: 10,
borderBottomRightRadius: 10,
marginTop: StyleConstants.Spacing.XS,
marginBottom: StyleConstants.Spacing.S
2020-12-03 01:28:56 +01:00
},
meta: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
marginTop: StyleConstants.Spacing.XS
},
button: {
2021-01-01 16:48:16 +01:00
marginRight: StyleConstants.Spacing.S
2020-12-03 01:28:56 +01:00
},
votes: {
...StyleConstants.FontStyle.S
2020-12-03 01:28:56 +01:00
},
expiration: {
...StyleConstants.FontStyle.S
2020-10-29 14:52:28 +01:00
}
})
2020-12-20 17:53:24 +01:00
export default TimelinePoll