import React, { useEffect, useRef, useState } from 'react'; import { selectLocalPeer, selectPeerNameByID, selectPermissions, selectPollByID, useHMSActions, useHMSStore, } from '@100mslive/react-sdk'; import { ChevronLeftIcon, CrossIcon } from '@100mslive/react-icons'; import { Box, Button, Flex, Text } from '../../../..'; // @ts-ignore import { Container } from '../../Streaming/Common'; import { StandardView } from './StandardVoting'; import { TimedView } from './TimedVoting'; // @ts-ignore import { usePollViewState } from '../../AppData/useUISettings'; // @ts-ignore import { getPeerResponses } from '../../../common/utils'; import { StatusIndicator } from '../common/StatusIndicator'; import { POLL_VIEWS } from '../../../common/constants'; export const Voting = ({ id, toggleVoting }: { id: string; toggleVoting: () => void }) => { const actions = useHMSActions(); const poll = useHMSStore(selectPollByID(id)); const pollCreatorName = useHMSStore(selectPeerNameByID(poll?.createdBy)); const permissions = useHMSStore(selectPermissions); const canEndActivity = !!permissions?.pollWrite; const { setPollView } = usePollViewState(); // Sets view - linear or vertical, toggles timer indicator const showSingleView = poll?.type === 'quiz' && poll.state === 'started'; const fetchedInitialResponses = useRef(false); const [savedResponses, setSavedResponses] = useState>({}); const localPeer = useHMSStore(selectLocalPeer); const localPeerId = localPeer?.id; const customerUserId = localPeer?.customerUserId; // To reset whenever a different poll is opened useEffect(() => { fetchedInitialResponses.current = false; setSavedResponses({}); }, [id, setSavedResponses]); useEffect(() => { const getResponses = async () => { if (poll && actions.interactivityCenter && !fetchedInitialResponses.current) { await actions.interactivityCenter.getPollResponses(poll, true); fetchedInitialResponses.current = true; } }; getResponses(); }, [poll, actions.interactivityCenter]); useEffect(() => { if (poll?.questions) { const localPeerResponses = getPeerResponses(poll.questions, localPeerId, customerUserId); // @ts-ignore localPeerResponses?.forEach(response => { if (response) { setSavedResponses(prev => { const prevCopy = { ...prev }; prevCopy[response[0]?.questionIndex] = { option: response[0]?.option, options: response[0]?.options }; return prevCopy; }); } }); } }, [localPeerId, poll?.questions, id, customerUserId]); if (!poll) { return null; } const canViewLeaderboard = poll.type === 'quiz' && poll.state === 'stopped' && !poll.anonymous; return ( setPollView(POLL_VIEWS.CREATE_POLL_QUIZ)} css={{ cursor: 'pointer', c: '$on_surface_medium', '&:hover': { color: '$on_surface_high' } }} > {poll.title} {poll.state === 'started' ? ( {pollCreatorName || 'Participant'} started a {poll.type} ) : null} {showSingleView ? ( ) : ( )} {poll.state === 'started' && canEndActivity && ( )} {canViewLeaderboard ? ( ) : null} ); };