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'
|
2021-01-03 02:00:26 +01:00
|
|
|
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'
|
2022-05-07 00:52:32 +02:00
|
|
|
import CustomText from '@components/Text'
|
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'
|
2022-06-10 17:02:04 +02:00
|
|
|
import { Pressable, 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
|
|
|
|
}) => {
|
2022-02-12 14:51:01 +01:00
|
|
|
const { colors, 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)
|
|
|
|
)
|
|
|
|
|
2021-01-04 10:50:24 +01:00
|
|
|
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)
|
|
|
|
rootQueryKey && queryClient.cancelQueries(rootQueryKey)
|
|
|
|
|
|
|
|
haptics('Success')
|
|
|
|
switch (theParams.payload.property) {
|
|
|
|
case 'poll':
|
2022-02-12 14:51:01 +01:00
|
|
|
theParams.payload.data = body as unknown as Mastodon.Poll
|
2021-03-25 00:25:37 +01:00
|
|
|
updateStatusProperty(theParams)
|
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,
|
2021-01-04 10:50:24 +01:00
|
|
|
type: 'error',
|
2021-03-28 23:31:10 +02:00
|
|
|
message: t('common:message.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}`)
|
|
|
|
}),
|
2021-01-04 10:50:24 +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
|
|
|
})
|
2021-01-04 10:50:24 +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 (
|
2022-05-07 00:52:32 +02:00
|
|
|
<View style={{ marginRight: StyleConstants.Spacing.S }}>
|
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 (
|
2022-05-07 00:52:32 +02:00
|
|
|
<View style={{ marginRight: StyleConstants.Spacing.S }}>
|
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
|
|
|
}, [
|
2022-02-12 14:51:01 +01:00
|
|
|
theme,
|
2021-03-22 00:04:47 +01:00
|
|
|
i18n.language,
|
|
|
|
poll.expired,
|
|
|
|
poll.voted,
|
|
|
|
allOptions,
|
|
|
|
mutation.isLoading
|
|
|
|
])
|
2020-12-20 17:53:24 +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]
|
2021-01-03 02:00:26 +01:00
|
|
|
? `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(() => {
|
2022-02-12 14:51:01 +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-05-07 00:52:32 +02:00
|
|
|
<View
|
|
|
|
key={index}
|
|
|
|
style={{ flex: 1, paddingVertical: StyleConstants.Spacing.S }}
|
|
|
|
>
|
|
|
|
<View style={{ flex: 1, flexDirection: 'row' }}>
|
2021-01-03 02:00:26 +01:00
|
|
|
<Icon
|
2022-05-07 00:52:32 +02:00
|
|
|
style={{
|
|
|
|
paddingTop:
|
|
|
|
StyleConstants.Font.LineHeight.M - StyleConstants.Font.Size.M,
|
|
|
|
marginRight: StyleConstants.Spacing.S
|
|
|
|
}}
|
2021-01-01 16:48:16 +01:00
|
|
|
name={
|
2021-01-03 02:00:26 +01:00
|
|
|
`${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={
|
2022-02-12 14:51:01 +01:00
|
|
|
poll.own_votes?.includes(index) ? colors.blue : colors.disabled
|
2021-01-01 16:48:16 +01:00
|
|
|
}
|
|
|
|
/>
|
2022-05-07 00:52:32 +02:00
|
|
|
<CustomText style={{ flex: 1 }}>
|
2021-01-01 16:48:16 +01:00
|
|
|
<ParseEmojis content={option.title} emojis={poll.emojis} />
|
2022-05-07 00:52:32 +02:00
|
|
|
</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
|
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}
|
|
|
|
%
|
2022-05-07 00:52:32 +02:00
|
|
|
</CustomText>
|
2021-01-01 16:48:16 +01:00
|
|
|
</View>
|
|
|
|
|
|
|
|
<View
|
2022-05-07 00:52:32 +02:00
|
|
|
style={{
|
|
|
|
height: StyleConstants.Spacing.XS,
|
|
|
|
minWidth: 2,
|
|
|
|
borderTopRightRadius: 10,
|
|
|
|
borderBottomRightRadius: 10,
|
|
|
|
marginTop: StyleConstants.Spacing.XS,
|
|
|
|
marginBottom: StyleConstants.Spacing.S,
|
|
|
|
width: `${Math.round(
|
|
|
|
(option.votes_count / (poll.voters_count || poll.votes_count)) *
|
|
|
|
100
|
|
|
|
)}%`,
|
|
|
|
backgroundColor:
|
|
|
|
option.votes_count === maxValue ? colors.blue : colors.disabled
|
|
|
|
}}
|
2021-01-01 16:48:16 +01:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
))
|
2022-02-12 14:51:01 +01:00
|
|
|
}, [theme, poll.options])
|
2021-01-01 16:48:16 +01:00
|
|
|
const pollBodyAllow = useMemo(() => {
|
|
|
|
return poll.options.map((option, index) => (
|
|
|
|
<Pressable
|
|
|
|
key={index}
|
2022-05-07 00:52:32 +02:00
|
|
|
style={{ flex: 1, paddingVertical: StyleConstants.Spacing.S }}
|
2021-01-01 16:48:16 +01:00
|
|
|
onPress={() => {
|
2021-01-24 02:25:43 +01:00
|
|
|
analytics('timeline_shared_vote_option_press')
|
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 {
|
|
|
|
{
|
|
|
|
const otherOptions =
|
|
|
|
allOptions[index] === false ? false : undefined
|
|
|
|
setAllOptions(
|
|
|
|
allOptions.map((o, i) =>
|
|
|
|
i === index
|
|
|
|
? !o
|
|
|
|
: otherOptions !== undefined
|
|
|
|
? otherOptions
|
|
|
|
: o
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2022-05-07 00:52:32 +02:00
|
|
|
<View style={{ flex: 1, flexDirection: 'row' }}>
|
2021-01-03 02:00:26 +01:00
|
|
|
<Icon
|
2022-05-07 00:52:32 +02:00
|
|
|
style={{
|
|
|
|
paddingTop:
|
|
|
|
StyleConstants.Font.LineHeight.M - StyleConstants.Font.Size.M,
|
|
|
|
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
|
|
|
/>
|
2022-05-07 00:52:32 +02:00
|
|
|
<CustomText style={{ flex: 1 }}>
|
2021-01-01 16:48:16 +01:00
|
|
|
<ParseEmojis content={option.title} emojis={poll.emojis} />
|
2022-05-07 00:52:32 +02:00
|
|
|
</CustomText>
|
2021-01-01 16:48:16 +01:00
|
|
|
</View>
|
|
|
|
</Pressable>
|
|
|
|
))
|
2022-02-12 14:51:01 +01:00
|
|
|
}, [theme, allOptions])
|
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) {
|
|
|
|
return (
|
2022-05-29 17:56:35 +02:00
|
|
|
t('shared.poll.meta.count.voters', { count: poll.voters_count }) + ' • '
|
2021-01-13 01:03:46 +01:00
|
|
|
)
|
|
|
|
} else if (poll.votes_count !== null) {
|
|
|
|
return (
|
2022-05-29 17:56:35 +02:00
|
|
|
t('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) {
|
|
|
|
return t('shared.poll.meta.expiration.expired')
|
|
|
|
} else {
|
|
|
|
if (poll.expires_at) {
|
|
|
|
return (
|
|
|
|
<Trans
|
|
|
|
i18nKey='componentTimeline:shared.poll.meta.expiration.until'
|
2022-06-14 23:43:11 +02:00
|
|
|
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
|
|
|
|
2020-10-29 14:52:28 +01:00
|
|
|
return (
|
2022-05-07 00:52:32 +02:00
|
|
|
<View style={{ marginTop: StyleConstants.Spacing.M }}>
|
2021-01-01 16:48:16 +01:00
|
|
|
{poll.expired || poll.voted ? pollBodyDisallow : pollBodyAllow}
|
2022-05-07 00:52:32 +02:00
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
marginTop: StyleConstants.Spacing.XS
|
|
|
|
}}
|
|
|
|
>
|
2021-01-01 16:48:16 +01:00
|
|
|
{pollButton}
|
2022-05-29 17:56:35 +02: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
|