UNPKG

stream-chat-react

Version:

React components to create chat conversations or livestream style chat

35 lines (34 loc) 2.28 kB
import clsx from 'clsx'; import React, { useCallback, useState } from 'react'; import { PollOptionVotesList } from './PollOptionVotesList'; import { PollOptionWithLatestVotes } from './PollOptionWithLatestVotes'; import { ModalHeader } from '../../../Modal/ModalHeader'; import { useStateStore } from '../../../../store'; import { usePollContext, useTranslationContext } from '../../../../context'; const pollStateSelector = (nextValue) => ({ name: nextValue.name, options: [...nextValue.options], vote_counts_by_option: nextValue.vote_counts_by_option, }); export const PollResults = ({ close }) => { const { t } = useTranslationContext(); const { poll } = usePollContext(); const { name, options, vote_counts_by_option } = useStateStore(poll.state, pollStateSelector); const [optionToView, setOptionToView] = useState(); const goBack = useCallback(() => setOptionToView(undefined), []); return (React.createElement("div", { className: clsx('str-chat__modal__poll-results', { 'str-chat__modal__poll-results--option-detail': optionToView, }) }, optionToView ? (React.createElement(React.Fragment, null, React.createElement(ModalHeader, { close: close, goBack: goBack, title: optionToView.text }), React.createElement("div", { className: 'str-chat__modal__poll-results__body' }, React.createElement(PollOptionVotesList, { key: `poll-option-detail-${optionToView.id}`, option: optionToView })))) : (React.createElement(React.Fragment, null, React.createElement(ModalHeader, { close: close, title: t('Poll results') }), React.createElement("div", { className: 'str-chat__modal__poll-results__body' }, React.createElement("div", { className: 'str-chat__modal__poll-results__title' }, name), React.createElement("div", { className: 'str-chat__modal__poll-results__option-list' }, options .sort((next, current) => (vote_counts_by_option[current.id] ?? 0) >= (vote_counts_by_option[next.id] ?? 0) ? 1 : -1) .map((option) => (React.createElement(PollOptionWithLatestVotes, { key: `poll-option-${option.id}`, option: option, showAllVotes: () => setOptionToView(option) }))))))))); };