import clsx from 'clsx';
import React from 'react';
import { defineMessages, useIntl } from 'react-intl';
import { Motion, presets, spring } from 'react-motion';
import { HStack, Icon, Text } from '../ui';
import type {
Poll as PollEntity,
PollOption as PollOptionEntity,
} from 'soapbox/types/entities';
const messages = defineMessages({
voted: { id: 'poll.voted', defaultMessage: 'You voted for this answer' },
votes: { id: 'poll.votes', defaultMessage: '{votes, plural, one {# vote} other {# votes}}' },
});
const PollPercentageBar: React.FC<{ percent: number, leading: boolean }> = ({ percent, leading }): JSX.Element => {
return (
{({ width }) => (
)}
);
};
interface IPollOptionText extends IPollOption {
percent: number
}
const PollOptionText: React.FC = ({ poll, option, index, active, onToggle }) => {
const handleOptionChange: React.EventHandler = () => onToggle(index);
const handleOptionKeyPress: React.EventHandler = e => {
if (e.key === 'Enter' || e.key === ' ') {
onToggle(index);
e.stopPropagation();
e.preventDefault();
}
};
return (
);
};
interface IPollOption {
poll: PollEntity
option: PollOptionEntity
index: number
showResults?: boolean
active: boolean
onToggle: (value: number) => void
}
const PollOption: React.FC = (props): JSX.Element | null => {
const { index, poll, option, showResults } = props;
const intl = useIntl();
if (!poll) return null;
const pollVotesCount = poll.voters_count || poll.votes_count;
const percent = pollVotesCount === 0 ? 0 : (option.votes_count / pollVotesCount) * 100;
const leading = poll.options.filterNot(other => other.title === option.title).every(other => option.votes_count >= other.votes_count);
const voted = poll.own_votes?.includes(index);
const message = intl.formatMessage(messages.votes, { votes: option.votes_count });
return (
{showResults ? (
{voted ? (
) : (
)}
{Math.round(percent)}%
) : (
)}
);
};
export default PollOption;