import Button from '@components/Button' import haptics from '@components/haptics' import Icon from '@components/Icon' import { displayMessage } from '@components/Message' import { ParseEmojis } from '@components/Parse' import RelativeTime from '@components/RelativeTime' import CustomText from '@components/Text' import { useQueryClient } from '@tanstack/react-query' import { useNavState } from '@utils/navigation/navigators' import { MutationVarsTimelineUpdateStatusProperty, useTimelineMutation } from '@utils/queryHooks/timeline' import updateStatusProperty from '@utils/queryHooks/timeline/updateStatusProperty' import { StyleConstants } from '@utils/styles/constants' import { useTheme } from '@utils/styles/ThemeManager' import { maxBy } from 'lodash' import React, { useContext, useState } from 'react' import { Trans, useTranslation } from 'react-i18next' import { Pressable, View } from 'react-native' import StatusContext from './Context' const TimelinePoll: React.FC = () => { const { queryKey, status, ownAccount, spoilerHidden, disableDetails, highlighted } = useContext(StatusContext) if (!queryKey || !status || !status.poll) return null const poll = status.poll const { colors, theme } = useTheme() const { t } = useTranslation(['common', 'componentTimeline']) const [allOptions, setAllOptions] = useState(new Array(status.poll.options.length).fill(false)) const navigationState = useNavState() const queryClient = useQueryClient() const mutation = useTimelineMutation({ onSuccess: ({ body }, params) => { const theParams = params as MutationVarsTimelineUpdateStatusProperty queryClient.cancelQueries(queryKey) haptics('Success') switch (theParams.payload.type) { case 'poll': updateStatusProperty( { ...theParams, poll: body as unknown as Mastodon.Poll }, navigationState ) break } }, onError: (err: any, params) => { const theParams = params as MutationVarsTimelineUpdateStatusProperty displayMessage({ theme, type: 'error', message: t('common:message.error.message', { // @ts-ignore function: t(`componentTimeline:shared.poll.meta.button.${theParams.payload.type}` as any) }), ...(err.status && typeof err.status === 'number' && err.data && err.data.error && typeof err.data.error === 'string' && { description: err.data.error }) }) queryClient.invalidateQueries(queryKey) } }) const pollButton = () => { if (!poll.expired) { if (!ownAccount && !poll.voted) { return (