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

324 lines
8.5 KiB
TypeScript
Raw Normal View History

2020-12-03 01:28:56 +01:00
import { Feather } from '@expo/vector-icons'
2020-12-26 23:05:17 +01:00
import React, { useCallback, useMemo, useState } from 'react'
2020-12-03 01:28:56 +01:00
import { Pressable, StyleSheet, Text, View } from 'react-native'
2020-12-18 23:58:53 +01:00
import { useMutation, useQueryClient } from 'react-query'
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'
2020-12-13 14:04:25 +01:00
import { toast } from '@components/toast'
import relativeTime from '@utils/relativeTime'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2020-10-29 14:52:28 +01:00
import Emojis from './Emojis'
2020-12-19 18:21:37 +01:00
import { TimelineData } from '../../Timeline'
import { findIndex } from 'lodash'
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 {
toast({
type: 'error',
content: '投票失败,请重试',
2020-12-03 01:28:56 +01:00
autoHide: false
})
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
}) => {
2020-12-03 01:28:56 +01:00
const { theme } = useTheme()
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-03 01:28:56 +01:00
}
})
2020-12-20 17:53:24 +01:00
const pollButton = () => {
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'
content='投票'
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 })}
{...(mutation.isLoading ? { icon: 'loader' } : { text: '刷新' })}
2020-12-26 23:05:17 +01:00
type='text'
content='刷新'
loading={mutation.isLoading}
2020-12-20 17:53:24 +01:00
/>
</View>
)
}
}
}
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 }]}>
</Text>
)
} else {
return (
<Text style={[styles.expiration, { color: theme.secondary }]}>
2020-12-18 23:58:53 +01:00
{relativeTime(poll.expires_at)}
2020-12-03 01:28:56 +01:00
</Text>
)
}
}, [])
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
2020-10-29 14:52:28 +01:00
return (
2020-12-03 01:28:56 +01:00
<View style={styles.base}>
2020-12-18 23:58:53 +01:00
{poll.options.map((option, index) =>
poll.voted ? (
2020-12-03 01:28:56 +01:00
<View key={index} style={styles.poll}>
<View style={styles.optionSelected}>
2020-12-26 23:05:17 +01:00
<Feather
style={styles.voted}
name={
`${poll.own_votes!.includes(index) ? 'check-' : ''}${
poll.multiple ? 'square' : 'circle'
}` as any
}
2020-12-26 23:05:17 +01:00
size={StyleConstants.Font.Size.M}
color={
poll.own_votes!.includes(index)
? theme.primary
: theme.disabled
2020-12-26 23:05:17 +01:00
}
/>
2020-12-03 01:28:56 +01:00
<View style={styles.contentSelected}>
<Emojis
content={option.title}
2020-12-18 23:58:53 +01:00
emojis={poll.emojis}
2020-12-03 01:28:56 +01:00
size={StyleConstants.Font.Size.M}
2020-12-26 23:05:17 +01:00
numberOfLines={2}
2020-12-03 01:28:56 +01:00
/>
</View>
<Text style={[styles.percentage, { color: theme.primary }]}>
2020-12-20 17:53:24 +01:00
{poll.votes_count
? Math.round((option.votes_count / poll.voters_count) * 100)
: 0}
%
2020-12-03 01:28:56 +01:00
</Text>
</View>
<View
style={[
styles.background,
{
width: `${Math.round(
2020-12-20 17:53:24 +01:00
(option.votes_count / poll.voters_count) * 100
2020-12-03 01:28:56 +01:00
)}%`,
2020-12-26 23:05:17 +01:00
backgroundColor: theme.disabled
2020-12-03 01:28:56 +01:00
}
]}
/>
</View>
) : (
<View key={index} style={styles.poll}>
<Pressable
style={[styles.optionUnselected]}
onPress={() => {
2020-12-18 23:58:53 +01:00
if (poll.multiple) {
2020-12-26 23:05:17 +01:00
setAllOptions(
allOptions.map((o, i) => (i === index ? !o : o))
)
2020-12-03 01:28:56 +01:00
} else {
2020-12-27 16:54:13 +01:00
{
const otherOptions =
allOptions[index] === false ? false : undefined
setAllOptions(
allOptions.map((o, i) =>
i === index
? !o
: otherOptions !== undefined
? otherOptions
: o
)
2020-12-26 23:05:17 +01:00
)
2020-12-27 16:54:13 +01:00
}
2020-12-03 01:28:56 +01:00
}
}}
>
<Feather
style={styles.votedNot}
name={isSelected(index)}
size={StyleConstants.Font.Size.L}
color={theme.primary}
/>
<View style={styles.contentUnselected}>
<Emojis
content={option.title}
2020-12-18 23:58:53 +01:00
emojis={poll.emojis}
2020-12-03 01:28:56 +01:00
size={StyleConstants.Font.Size.M}
2020-12-26 23:05:17 +01:00
numberOfLines={2}
2020-12-03 01:28:56 +01:00
/>
</View>
</Pressable>
</View>
)
)}
<View style={styles.meta}>
2020-12-20 17:53:24 +01:00
{pollButton()}
2020-12-03 01:28:56 +01:00
<Text style={[styles.votes, { color: theme.secondary }]}>
{poll.voters_count || 0}{' • '}
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
},
poll: {
2020-12-26 23:05:17 +01:00
flex: 1,
minHeight: StyleConstants.Font.LineHeight.M * 2,
paddingVertical: StyleConstants.Spacing.XS
2020-12-03 01:28:56 +01:00
},
optionSelected: {
flex: 1,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
paddingRight: StyleConstants.Spacing.M
},
optionUnselected: {
flex: 1,
flexDirection: 'row'
},
contentSelected: {
flex: 1,
2020-12-03 01:28:56 +01:00
flexDirection: 'row',
2020-12-26 23:05:17 +01:00
alignItems: 'center',
paddingRight: StyleConstants.Spacing.S
2020-12-03 01:28:56 +01:00
},
contentUnselected: {
2020-12-26 23:05:17 +01:00
flexShrink: 1
2020-12-03 01:28:56 +01:00
},
voted: {
2020-12-26 23:05:17 +01:00
marginRight: StyleConstants.Spacing.S
2020-12-03 01:28:56 +01:00
},
votedNot: {
2020-12-26 23:05:17 +01:00
paddingRight: StyleConstants.Spacing.S
2020-12-03 01:28:56 +01:00
},
percentage: {
fontSize: StyleConstants.Font.Size.M
},
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: {
marginRight: StyleConstants.Spacing.M
},
votes: {
fontSize: StyleConstants.Font.Size.S
},
expiration: {
fontSize: StyleConstants.Font.Size.S
2020-10-29 14:52:28 +01:00
}
})
2020-12-20 17:53:24 +01:00
export default TimelinePoll