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

274 lines
8.7 KiB
TypeScript
Raw Normal View History

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'
2022-06-10 17:02:04 +02:00
import RelativeTime from '@components/RelativeTime'
import CustomText from '@components/Text'
import { useQueryClient } from '@tanstack/react-query'
2023-01-04 22:39:29 +01:00
import { useNavState } from '@utils/navigation/navigators'
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
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'
import React, { useContext, useState } from 'react'
2021-01-13 01:03:46 +01:00
import { Trans, useTranslation } from 'react-i18next'
2022-06-10 17:02:04 +02:00
import { Pressable, View } from 'react-native'
2022-12-03 20:47:11 +01:00
import StatusContext from './Context'
2020-10-29 14:52:28 +01:00
2022-12-03 20:47:11 +01:00
const TimelinePoll: React.FC = () => {
const { queryKey, status, ownAccount, spoilerHidden, disableDetails, highlighted } =
useContext(StatusContext)
2022-12-03 20:47:11 +01:00
if (!queryKey || !status || !status.poll) return null
const poll = status.poll
2020-12-03 01:28:56 +01:00
2022-02-12 14:51:01 +01:00
const { colors, theme } = useTheme()
2022-12-23 15:53:40 +01:00
const { t } = useTranslation(['common', 'componentTimeline'])
2020-12-26 23:05:17 +01:00
2022-12-03 20:47:11 +01:00
const [allOptions, setAllOptions] = useState(new Array(status.poll.options.length).fill(false))
2020-12-26 23:05:17 +01:00
2023-01-04 22:39:29 +01:00
const navigationState = useNavState()
const queryClient = useQueryClient()
2021-01-11 21:36:57 +01:00
const mutation = useTimelineMutation({
2021-02-13 01:26:02 +01:00
onSuccess: ({ body }, params) => {
const theParams = params as MutationVarsTimelineUpdateStatusProperty
queryClient.cancelQueries(queryKey)
haptics('Success')
2023-01-01 18:37:05 +01:00
switch (theParams.payload.type) {
2021-02-13 01:26:02 +01:00
case 'poll':
2023-01-04 22:39:29 +01:00
updateStatusProperty(
{ ...theParams, poll: body as unknown as Mastodon.Poll },
navigationState
)
2021-02-13 01:26:02 +01:00
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({
2022-02-12 14:51:01 +01:00
theme,
type: 'error',
2021-03-28 23:31:10 +02:00
message: t('common:message.error.message', {
// @ts-ignore
2022-12-23 15:53:40 +01:00
function: t(`componentTimeline:shared.poll.meta.button.${theParams.payload.type}` as any)
2021-01-19 01:13:45 +01:00
}),
...(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
}
})
const pollButton = () => {
2020-12-20 17:53:24 +01:00
if (!poll.expired) {
2022-12-03 20:47:11 +01:00
if (!ownAccount && !poll.voted) {
2020-12-20 17:53:24 +01:00
return (
<View style={{ marginRight: StyleConstants.Spacing.S }}>
2020-12-26 23:05:17 +01:00
<Button
2022-11-29 23:44:11 +01:00
onPress={() =>
2021-01-11 21:36:57 +01:00
mutation.mutate({
type: 'updateStatusProperty',
2023-01-01 18:37:05 +01:00
status,
2021-01-11 21:36:57 +01:00
payload: {
2023-01-01 18:37:05 +01:00
type: 'poll',
action: 'vote',
2021-01-11 21:36:57 +01:00
options: allOptions
}
})
2022-11-29 23:44:11 +01:00
}
2020-12-26 23:05:17 +01:00
type='text'
2022-12-23 15:53:40 +01:00
content={t('componentTimeline: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 if (highlighted) {
2020-12-20 17:53:24 +01:00
return (
<View style={{ marginRight: StyleConstants.Spacing.S }}>
2020-12-26 23:05:17 +01:00
<Button
2022-11-29 23:44:11 +01:00
onPress={() =>
2021-01-11 21:36:57 +01:00
mutation.mutate({
type: 'updateStatusProperty',
2023-01-01 18:37:05 +01:00
status,
2021-01-11 21:36:57 +01:00
payload: {
2023-01-01 18:37:05 +01:00
type: 'poll',
action: 'refresh'
2021-01-11 21:36:57 +01:00
}
})
2022-11-29 23:44:11 +01:00
}
2020-12-26 23:05:17 +01:00
type='text'
2022-12-23 15:53:40 +01:00
content={t('componentTimeline: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>
)
}
}
}
2020-12-20 17:53:24 +01:00
const isSelected = (index: number) =>
allOptions[index]
? poll.multiple
? 'check-square'
: 'check-circle'
: poll.multiple
? 'square'
: 'circle'
2020-12-03 01:28:56 +01:00
const pollBodyDisallow = () => {
2022-11-29 23:44:11 +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) => (
2022-11-29 23:44:11 +01:00
<View key={index} style={{ flex: 1, paddingVertical: StyleConstants.Spacing.S }}>
<View style={{ flex: 1, flexDirection: 'row' }}>
<Icon
style={{
marginTop: (StyleConstants.Font.LineHeight.M - StyleConstants.Font.Size.M) / 2,
marginRight: StyleConstants.Spacing.S
}}
2021-01-01 16:48:16 +01:00
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}
2022-11-29 23:44:11 +01:00
color={poll.own_votes?.includes(index) ? colors.blue : colors.disabled}
2021-01-01 16:48:16 +01:00
/>
<CustomText style={{ flex: 1 }}>
2021-01-01 16:48:16 +01:00
<ParseEmojis content={option.title} emojis={poll.emojis} />
</CustomText>
<CustomText
fontStyle='M'
style={{
alignSelf: 'center',
marginLeft: StyleConstants.Spacing.S,
flexBasis: '20%',
textAlign: 'center',
color: colors.primaryDefault
}}
2021-03-22 00:04:47 +01:00
>
2021-01-01 16:48:16 +01:00
{poll.votes_count
2022-11-29 23:44:11 +01:00
? Math.round((option.votes_count / (poll.voters_count || poll.votes_count)) * 100)
2021-01-01 16:48:16 +01:00
: 0}
%
</CustomText>
2021-01-01 16:48:16 +01:00
</View>
<View
style={{
height: StyleConstants.Spacing.XS,
minWidth: 2,
borderTopRightRadius: 10,
borderBottomRightRadius: 10,
marginTop: StyleConstants.Spacing.XS,
marginBottom: StyleConstants.Spacing.XS,
width: `${Math.round(
2022-11-29 23:44:11 +01:00
(option.votes_count / (poll.voters_count || poll.votes_count)) * 100
)}%`,
2022-11-29 23:44:11 +01:00
backgroundColor: option.votes_count === maxValue ? colors.blue : colors.disabled
}}
2021-01-01 16:48:16 +01:00
/>
</View>
))
}
const pollBodyAllow = () => {
2021-01-01 16:48:16 +01:00
return poll.options.map((option, index) => (
<Pressable
key={index}
style={{ flex: 1, paddingVertical: StyleConstants.Spacing.S }}
2021-01-01 16:48:16 +01:00
onPress={() => {
2021-03-27 00:47:14 +01:00
!allOptions[index] && haptics('Light')
2021-01-01 16:48:16 +01:00
if (poll.multiple) {
setAllOptions(allOptions.map((o, i) => (i === index ? !o : o)))
} else {
{
2022-11-29 23:44:11 +01:00
const otherOptions = allOptions[index] === false ? false : undefined
2021-01-01 16:48:16 +01:00
setAllOptions(
allOptions.map((o, i) =>
2022-11-29 23:44:11 +01:00
i === index ? !o : otherOptions !== undefined ? otherOptions : o
2021-01-01 16:48:16 +01:00
)
)
}
}
}}
>
<View style={{ flex: 1, flexDirection: 'row' }}>
<Icon
style={{
marginTop: (StyleConstants.Font.LineHeight.M - StyleConstants.Font.Size.M) / 2,
marginRight: StyleConstants.Spacing.S
}}
2021-01-01 16:48:16 +01:00
name={isSelected(index)}
2021-01-17 22:37:05 +01:00
size={StyleConstants.Font.Size.M}
2022-02-12 14:51:01 +01:00
color={colors.primaryDefault}
2021-01-01 16:48:16 +01:00
/>
<CustomText style={{ flex: 1 }}>
2021-01-01 16:48:16 +01:00
<ParseEmojis content={option.title} emojis={poll.emojis} />
</CustomText>
2021-01-01 16:48:16 +01:00
</View>
</Pressable>
))
}
2021-01-01 16:48:16 +01:00
2022-06-14 23:43:11 +02:00
const pollVoteCounts = () => {
2021-01-13 01:03:46 +01:00
if (poll.voters_count !== null) {
2023-01-01 18:37:05 +01:00
return t('componentTimeline:shared.poll.meta.count.voters', { count: poll.voters_count })
2021-01-13 01:03:46 +01:00
} else if (poll.votes_count !== null) {
2023-01-01 18:37:05 +01:00
return t('componentTimeline:shared.poll.meta.count.votes', { count: poll.votes_count })
2021-01-13 01:03:46 +01:00
}
2022-06-14 23:43:11 +02:00
}
2021-01-13 01:03:46 +01:00
2022-06-14 23:43:11 +02:00
const pollExpiration = () => {
2022-05-29 17:56:35 +02:00
if (poll.expired) {
2022-12-23 15:53:40 +01:00
return t('componentTimeline:shared.poll.meta.expiration.expired')
2022-05-29 17:56:35 +02:00
} else {
if (poll.expires_at) {
return (
2023-01-01 18:37:05 +01:00
<>
{' • '}
<Trans
ns='componentTimeline'
i18nKey='shared.poll.meta.expiration.until'
components={[<RelativeTime time={poll.expires_at} />]}
/>
</>
2022-05-29 17:56:35 +02:00
)
}
}
2022-06-14 23:43:11 +02:00
}
2022-05-29 17:56:35 +02:00
2022-12-03 20:47:11 +01:00
if (spoilerHidden || disableDetails) return null
2020-10-29 14:52:28 +01:00
return (
<View style={{ marginTop: StyleConstants.Spacing.M }}>
{poll.expired || poll.voted ? pollBodyDisallow() : pollBodyAllow()}
<View
style={{
flex: 1,
flexDirection: 'row',
alignItems: 'center',
marginTop: StyleConstants.Spacing.XS
}}
>
{pollButton()}
2022-11-29 23:44:11 +01:00
<CustomText fontStyle='S' style={{ flexShrink: 1, color: colors.secondary }}>
2022-06-14 23:43:11 +02:00
{pollVoteCounts()}
{pollExpiration()}
2022-05-29 17:56:35 +02:00
</CustomText>
2020-12-03 01:28:56 +01:00
</View>
2020-10-29 14:52:28 +01:00
</View>
)
}
2020-12-20 17:53:24 +01:00
export default TimelinePoll