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

306 lines
8.1 KiB
TypeScript
Raw Normal View History

2020-12-13 14:04:25 +01:00
import client from '@api/client'
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 relativeTime from '@components/relativeTime'
import { TimelineData } from '@components/Timelines/Timeline'
import { Feather } from '@expo/vector-icons'
import { ParseEmojis } from '@root/components/Parse'
2020-12-13 14:04:25 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2020-12-19 18:21:37 +01:00
import { findIndex } from 'lodash'
2021-01-01 16:48:16 +01:00
import React, { useCallback, useMemo, useState } from 'react'
2021-01-01 23:10:47 +01:00
import { useTranslation } from 'react-i18next'
2021-01-01 16:48:16 +01:00
import { Pressable, StyleSheet, Text, View } from 'react-native'
import { useMutation, useQueryClient } from 'react-query'
2020-10-29 14:52:28 +01:00
2020-12-03 01:28:56 +01:00
const fireMutation = async ({
id,
options
}: {
id: string
2020-12-26 23:05:17 +01:00
options?: boolean[]
2020-12-03 01:28:56 +01:00
}) => {
const formData = new FormData()
2020-12-20 17:53:24 +01:00
options &&
2020-12-26 23:05:17 +01:00
options.forEach((o, i) => {
if (options[i]) {
formData.append('choices[]', i.toString())
2020-12-20 17:53:24 +01:00
}
})
2020-12-03 01:28:56 +01:00
const res = await client({
2020-12-20 17:53:24 +01:00
method: options ? 'post' : 'get',
2020-12-03 01:28:56 +01:00
instance: 'local',
2020-12-20 17:53:24 +01:00
url: options ? `polls/${id}/votes` : `polls/${id}`,
...(options && { body: formData })
2020-12-03 01:28:56 +01:00
})
if (res.body.id === id) {
2020-12-20 17:53:24 +01:00
return Promise.resolve(res.body as Mastodon.Poll)
2020-12-03 01:28:56 +01:00
} else {
return Promise.reject()
}
}
2020-10-31 21:04:46 +01:00
export interface Props {
2020-12-18 23:58:53 +01:00
queryKey: QueryKey.Timeline
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,
poll,
reblog,
sameAccount
}) => {
2021-01-01 16:48:16 +01:00
const { mode, theme } = useTheme()
2021-01-01 23:10:47 +01:00
const { t, i18n } = useTranslation('timeline')
2020-12-18 23:58:53 +01:00
const queryClient = useQueryClient()
2020-12-26 23:05:17 +01:00
const [allOptions, setAllOptions] = useState(
new Array(poll.options.length).fill(false)
)
2020-12-20 17:53:24 +01:00
const mutation = useMutation(fireMutation, {
onSuccess: (data, { id }) => {
2020-12-18 23:58:53 +01:00
queryClient.cancelQueries(queryKey)
2020-12-19 18:21:37 +01:00
queryClient.setQueryData<TimelineData>(queryKey, old => {
let tootIndex = -1
const pageIndex = findIndex(old?.pages, page => {
const tempIndex = findIndex(page.toots, [
reblog ? 'reblog.poll.id' : 'poll.id',
id
])
if (tempIndex >= 0) {
tootIndex = tempIndex
return true
} else {
return false
}
})
if (pageIndex >= 0 && tootIndex >= 0) {
if (reblog) {
2020-12-20 17:53:24 +01:00
old!.pages[pageIndex].toots[tootIndex].reblog!.poll = data
2020-12-19 18:21:37 +01:00
} else {
2020-12-20 17:53:24 +01:00
old!.pages[pageIndex].toots[tootIndex].poll = data
2020-12-19 18:21:37 +01:00
}
}
return old
})
2020-12-30 14:33:33 +01:00
haptics('Success')
2021-01-01 16:48:16 +01:00
},
onError: () => {
haptics('Error')
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
onPress={() =>
mutation.mutate({ id: poll.id, options: allOptions })
}
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
2020-12-20 17:53:24 +01:00
onPress={() => mutation.mutate({ id: poll.id })}
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-01-01 16:48:16 +01:00
}, [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-01 23:10:47 +01:00
{t('shared.poll.meta.expiration.until', {
at: relativeTime(poll.expires_at, i18n.language)
})}
2020-12-03 01:28:56 +01:00
</Text>
)
}
2021-01-01 23:10:47 +01:00
}, [mode, poll.expired, poll.expires_at])
2020-12-03 01:28:56 +01:00
2020-12-26 23:05:17 +01:00
const isSelected = useCallback(
(index: number): any =>
allOptions[index]
? `check-${poll.multiple ? 'square' : 'circle'}`
: `${poll.multiple ? 'square' : 'circle'}`,
[allOptions]
)
2020-12-03 01:28:56 +01:00
2021-01-01 16:48:16 +01:00
const pollBodyDisallow = useMemo(() => {
return poll.options.map((option, index) => (
<View key={index} style={styles.optionContainer}>
<View style={styles.optionContent}>
<Feather
style={styles.optionSelection}
name={
`${poll.own_votes?.includes(index) ? 'check-' : ''}${
poll.multiple ? 'square' : 'circle'
}` as any
}
size={StyleConstants.Font.Size.M}
color={
poll.own_votes?.includes(index) ? theme.primary : theme.disabled
}
/>
<Text style={styles.optionText}>
<ParseEmojis content={option.title} emojis={poll.emojis} />
</Text>
<Text style={[styles.optionPercentage, { color: theme.primary }]}>
{poll.votes_count
? Math.round((option.votes_count / poll.voters_count) * 100)
: 0}
%
</Text>
</View>
<View
style={[
styles.background,
{
width: `${Math.round(
(option.votes_count / poll.voters_count) * 100
)}%`,
backgroundColor: theme.disabled
}
]}
/>
</View>
))
}, [mode, poll.options])
const pollBodyAllow = useMemo(() => {
return poll.options.map((option, index) => (
<Pressable
key={index}
style={styles.optionContainer}
onPress={() => {
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]}>
<Feather
style={styles.optionSelection}
name={isSelected(index)}
size={StyleConstants.Font.Size.L}
color={theme.primary}
/>
<Text style={styles.optionText}>
<ParseEmojis content={option.title} emojis={poll.emojis} />
</Text>
</View>
</Pressable>
))
}, [mode, allOptions])
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}
2020-12-03 01:28:56 +01:00
<Text style={[styles.votes, { color: theme.secondary }]}>
2021-01-01 23:10:47 +01:00
{t('shared.poll.meta.voted', { count: poll.voters_count })}
2020-12-03 01:28:56 +01:00
</Text>
{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: {
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