/* Copyright 2023 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React, { ReactNode } from "react"; import classNames from "classnames"; import { PollAnswerSubevent } from "matrix-js-sdk/src/extensible_events_v1/PollStartEvent"; import { _t } from "../../../languageHandler"; import { Icon as TrophyIcon } from "../../../../res/img/element-icons/trophy.svg"; import StyledRadioButton from "../elements/StyledRadioButton"; type PollOptionContentProps = { answer: PollAnswerSubevent; voteCount: number; displayVoteCount?: boolean; isWinner?: boolean; }; const PollOptionContent: React.FC = ({ isWinner, answer, voteCount, displayVoteCount }) => { const votesText = displayVoteCount ? _t("%(count)s votes", { count: voteCount }) : ""; return (
{answer.text}
{isWinner && } {votesText}
); }; interface PollOptionProps extends PollOptionContentProps { pollId: string; totalVoteCount: number; isEnded?: boolean; isChecked?: boolean; onOptionSelected?: (id: string) => void; children?: ReactNode; } const EndedPollOption: React.FC> = ({ isChecked, children, answer, }) => (
{children}
); const ActivePollOption: React.FC> = ({ pollId, isChecked, children, answer, onOptionSelected, }) => ( onOptionSelected?.(answer.id)} > {children} ); export const PollOption: React.FC = ({ pollId, answer, voteCount, totalVoteCount, displayVoteCount, isEnded, isChecked, onOptionSelected, }) => { const cls = classNames({ mx_PollOption: true, mx_PollOption_checked: isChecked, mx_PollOption_ended: isEnded, }); const isWinner = isEnded && isChecked; const answerPercent = totalVoteCount === 0 ? 0 : Math.round((100.0 * voteCount) / totalVoteCount); const PollOptionWrapper = isEnded ? EndedPollOption : ActivePollOption; return (
onOptionSelected?.(answer.id)}>
); };