import { useCallback, useRef, useEffect } from 'react'; import { AnimatePresence, motion, useAnimationControls } from 'framer-motion'; import PropTypes from 'prop-types'; import { clsm } from '../../../../utils'; import { PROFILE_COLORS_WITH_WHITE_TEXT, STREAM_ACTION_NAME, CHAT_MESSAGE_EVENT_TYPES } from '../../../../constants'; import { createAnimationProps } from '../../../../helpers/animationPropsHelper'; import { streamManager as $streamManagerContent } from '../../../../content'; import { useChannel } from '../../../../contexts/Channel'; import { useChat } from '../../../../contexts/Chat'; import { useResponsiveDevice } from '../../../../contexts/ResponsiveDevice'; import { usePoll } from '../../../../contexts/StreamManagerActions/Poll'; import { useUser } from '../../../../contexts/User'; import AnimatedVoteItems from './AnimatedVoteItems'; import Button from '../../../../components/Button/Button'; import ProgressBar from '../../ViewerStreamActions/ProgressBar'; import Spinner from '../../../../components/Spinner'; import PollContainer from './PollContainer'; const $content = $streamManagerContent.stream_manager_actions[STREAM_ACTION_NAME.POLL]; const ViewerPoll = ({ shouldRenderInTab }) => { const { SUBMIT_VOTE } = CHAT_MESSAGE_EVENT_TYPES; const { actions: { sendMessage } } = useChat(); const { isTouchscreenDevice } = useResponsiveDevice(); const { isSubmitting, selectedOption, startTime, duration, setPollRef, dispatchPollState, question, showFinalResults, hasScrollbar, hasPollEnded, shouldRenderVoteButton, shouldRenderProgressbar, isActive } = usePoll(); const { channelData } = useChannel(); const { color } = channelData || {}; const { userData = undefined } = useUser(); const { trackingId = undefined } = userData || {}; const buttonDivControls = useAnimationControls(); const radioBoxControls = useAnimationControls(); const textColor = PROFILE_COLORS_WITH_WHITE_TEXT.includes(color) ? 'white' : 'black'; const submitVote = useCallback(async () => { dispatchPollState({ isSubmitting: true }); const result = await sendMessage(SUBMIT_VOTE, { voter: trackingId, eventType: SUBMIT_VOTE, option: selectedOption, duration: JSON.stringify(duration), startTime: JSON.stringify(startTime) }); await Promise.all([ radioBoxControls.start({ left: '-300px', opacity: 0, transition: { duration: 0.1 } }), buttonDivControls.start({ height: 0, padding: 0, opacity: 0, transition: { duration: 0.1 } }) ]); if (result) { dispatchPollState({ isSubmitting: false, isVoting: false }); } }, [ SUBMIT_VOTE, buttonDivControls, duration, radioBoxControls, selectedOption, sendMessage, startTime, trackingId, dispatchPollState ]); const pollRef = useRef(); useEffect(() => { if (pollRef?.current) { dispatchPollState({ pollRef: pollRef.current }); } }, [dispatchPollState, pollRef, setPollRef]); const showVoteAndProgress = !hasPollEnded && !showFinalResults; const showVoteAndProgressAsFooter = hasScrollbar && !shouldRenderInTab; const renderProgressBar = (
); const renderVoteButton = ( <> {shouldRenderVoteButton && ( )} ); return ( <>

{question}

{!showVoteAndProgressAsFooter && ( <> {renderVoteButton} {renderProgressBar} )}
{showVoteAndProgress && showVoteAndProgressAsFooter && isActive && ( <>
)} ); }; ViewerPoll.propTypes = { shouldRenderInTab: PropTypes.bool.isRequired }; export default ViewerPoll;