import { useEffect, forwardRef } from 'react'; import PropTypes from 'prop-types'; import { motion } from 'framer-motion'; import { CheckCircle } from '../../../../assets/icons'; import { clsm, convertConcurrentViews } from '../../../../utils'; import { createAnimationProps } from '../../../../helpers/animationPropsHelper'; import { STREAM_ACTION_NAME } from '../../../../constants'; import { streamManager as $streamManagerContent } from '../../../../content'; import Tooltip from '../../../../components/Tooltip/Tooltip'; import { useLocation } from 'react-router-dom'; import { usePoll } from '../../../../contexts/StreamManagerActions/Poll'; import { useUser } from '../../../../contexts/User'; const $content = $streamManagerContent.stream_manager_actions[STREAM_ACTION_NAME.POLL]; const opacityAnimation = createAnimationProps({ customVariants: { hidden: { opacity: 0 }, visible: { opacity: 1 } }, transition: { duration: 0.3 } }); const VoteItem = forwardRef( ( { count, isHighestCount, option, percentage, showVotePercentage, color, textColor, inputAndLabelId, radioBoxControls }, ref ) => { const { selectedOption, setSelectedOption, isVoting, showFinalResults, noVotesCaptured, shouldRenderRadioInput } = usePoll(); const hasWon = isHighestCount && showFinalResults; const countFormatted = convertConcurrentViews(count); const { pathname } = useLocation(); const { isSessionValid } = useUser(); const voteContent = count === 1 ? $content.vote.toLowerCase() : $content.votes; const isStreamManagerPage = pathname === '/manager'; const isPollPercentageVisible = !isSessionValid || !isVoting || showFinalResults || isStreamManagerPage; const showCurrentVotes = isPollPercentageVisible || noVotesCaptured; useEffect(() => { /** * This code dynamically adjusts the height of certain containers based on the height * of their child elements, specifically '.vote-option-container' and * '.vote-option-parent-container'. If any '.vote-option-container' * has a height between 24 and 44 pixels, it sets shouldResizeAllContainers to true, * and the parent containers are given a height of either 58 pixels or 44 pixels * based on the value of shouldResizeAllContainers. */ let shouldResizeAllContainers = false; const listItems = document.querySelectorAll('.vote-option-container'); const parentDivs = document.querySelectorAll( '.vote-option-parent-container' ); for (const item of listItems) { if (item.offsetHeight > 24 && item.offsetHeight < 50) { shouldResizeAllContainers = true; break; } } parentDivs.forEach((item) => { if (hasWon) return; item.classList.add(shouldResizeAllContainers ? 'h-14' : 'h-11'); }); }, [option, color, hasWon]); return ( // eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
{ if (!isVoting) return; setSelectedOption(option); }} className={clsm([ 'overflow-hidden', 'vote-option-parent-container', 'relative', 'w-full', `bg-poll-${color}-pollVoteBg`, 'rounded-[100px]', 'h-full', hasWon && [ 'h-20', `bg-poll-${color}-pollVoteBg`, 'border-2', 'border-white', 'mb-1.5' ] ])} >
{hasWon && (

{$content.winner}

)}
input.radio]:top-[0px]', 'flex', 'relative', 'items-center' ])} > {shouldRenderRadioInput && ( { setSelectedOption(option); }} type="radio" value={selectedOption} /> )}
{isSessionValid && selectedOption === option && !isVoting && ( )}
{showCurrentVotes && (
{showVotePercentage ? ( {`${percentage}%`} ) : (

{`${percentage}% (${countFormatted} ${voteContent})`}

)}
)}
); } ); VoteItem.defaultProps = { count: 0, isHighestCount: false, showVotePercentage: false, percentage: 0, radioBoxControls: {}, inputAndLabelId: undefined, noVotesCaptured: false }; VoteItem.propTypes = { count: PropTypes.number, color: PropTypes.string.isRequired, textColor: PropTypes.string.isRequired, isHighestCount: PropTypes.bool, option: PropTypes.string.isRequired, percentage: PropTypes.number, showVotePercentage: PropTypes.bool, inputAndLabelId: PropTypes.string, radioBoxControls: PropTypes.object, noVotesCaptured: PropTypes.bool }; export default VoteItem;