|
| 1 | +/* |
| 2 | +Copyright 2023 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React, { useEffect, useState } from "react"; |
| 18 | +import { PollAnswerSubevent } from "matrix-js-sdk/src/extensible_events_v1/PollStartEvent"; |
| 19 | +import { MatrixEvent, Poll, PollEvent } from "matrix-js-sdk/src/matrix"; |
| 20 | +import { Relations } from "matrix-js-sdk/src/models/relations"; |
| 21 | + |
| 22 | +import { Icon as PollIcon } from "../../../../../res/img/element-icons/room/composer/poll.svg"; |
| 23 | +import { _t } from "../../../../languageHandler"; |
| 24 | +import { formatLocalDateShort } from "../../../../DateUtils"; |
| 25 | +import { allVotes, collectUserVotes, countVotes } from "../../messages/MPollBody"; |
| 26 | +import { PollOption } from "../../polls/PollOption"; |
| 27 | +import { Caption } from "../../typography/Caption"; |
| 28 | + |
| 29 | +interface Props { |
| 30 | + event: MatrixEvent; |
| 31 | + poll: Poll; |
| 32 | +} |
| 33 | + |
| 34 | +type EndedPollState = { |
| 35 | + winningAnswers: { |
| 36 | + answer: PollAnswerSubevent; |
| 37 | + voteCount: number; |
| 38 | + }[]; |
| 39 | + totalVoteCount: number; |
| 40 | +}; |
| 41 | +const getWinningAnswers = (poll: Poll, responseRelations: Relations): EndedPollState => { |
| 42 | + const userVotes = collectUserVotes(allVotes(responseRelations)); |
| 43 | + const votes = countVotes(userVotes, poll.pollEvent); |
| 44 | + const totalVoteCount = [...votes.values()].reduce((sum, vote) => sum + vote, 0); |
| 45 | + const winCount = Math.max(...votes.values()); |
| 46 | + |
| 47 | + return { |
| 48 | + totalVoteCount, |
| 49 | + winningAnswers: poll.pollEvent.answers |
| 50 | + .filter((answer) => votes.get(answer.id) === winCount) |
| 51 | + .map((answer) => ({ |
| 52 | + answer, |
| 53 | + voteCount: votes.get(answer.id) || 0, |
| 54 | + })), |
| 55 | + }; |
| 56 | +}; |
| 57 | + |
| 58 | +/** |
| 59 | + * Get deduplicated and validated poll responses |
| 60 | + * Will use cached responses from Poll instance when existing |
| 61 | + * Updates on changes to Poll responses (paging relations or from sync) |
| 62 | + * Returns winning answers and total vote count |
| 63 | + */ |
| 64 | +const usePollVotes = (poll: Poll): Partial<EndedPollState> => { |
| 65 | + const [results, setResults] = useState({ totalVoteCount: 0 }); |
| 66 | + |
| 67 | + useEffect(() => { |
| 68 | + const getResponses = async (): Promise<void> => { |
| 69 | + const responseRelations = await poll.getResponses(); |
| 70 | + setResults(getWinningAnswers(poll, responseRelations)); |
| 71 | + }; |
| 72 | + const onPollResponses = (responseRelations: Relations): void => |
| 73 | + setResults(getWinningAnswers(poll, responseRelations)); |
| 74 | + poll.on(PollEvent.Responses, onPollResponses); |
| 75 | + |
| 76 | + getResponses(); |
| 77 | + |
| 78 | + return () => { |
| 79 | + poll.off(PollEvent.Responses, onPollResponses); |
| 80 | + }; |
| 81 | + }, [poll]); |
| 82 | + |
| 83 | + return results; |
| 84 | +}; |
| 85 | + |
| 86 | +/** |
| 87 | + * Render an ended poll with the winning answer and vote count |
| 88 | + * @param event - the poll start MatrixEvent |
| 89 | + * @param poll - Poll instance |
| 90 | + */ |
| 91 | +export const PollListItemEnded: React.FC<Props> = ({ event, poll }) => { |
| 92 | + const pollEvent = poll.pollEvent; |
| 93 | + const { winningAnswers, totalVoteCount } = usePollVotes(poll); |
| 94 | + if (!pollEvent) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + const formattedDate = formatLocalDateShort(event.getTs()); |
| 98 | + |
| 99 | + return ( |
| 100 | + <li data-testid={`pollListItem-${event.getId()!}`} className="mx_PollListItemEnded"> |
| 101 | + <div className="mx_PollListItemEnded_title"> |
| 102 | + <PollIcon className="mx_PollListItemEnded_icon" /> |
| 103 | + <span className="mx_PollListItemEnded_question">{pollEvent.question.text}</span> |
| 104 | + <Caption>{formattedDate}</Caption> |
| 105 | + </div> |
| 106 | + {!!winningAnswers?.length && ( |
| 107 | + <div className="mx_PollListItemEnded_answers"> |
| 108 | + {winningAnswers?.map(({ answer, voteCount }) => ( |
| 109 | + <PollOption |
| 110 | + key={answer.id} |
| 111 | + answer={answer} |
| 112 | + voteCount={voteCount} |
| 113 | + totalVoteCount={totalVoteCount!} |
| 114 | + pollId={poll.pollId} |
| 115 | + displayVoteCount |
| 116 | + isChecked |
| 117 | + isEnded |
| 118 | + /> |
| 119 | + ))} |
| 120 | + </div> |
| 121 | + )} |
| 122 | + <div className="mx_PollListItemEnded_voteCount"> |
| 123 | + <Caption>{_t("Final result based on %(count)s votes", { count: totalVoteCount })}</Caption> |
| 124 | + </div> |
| 125 | + </li> |
| 126 | + ); |
| 127 | +}; |
0 commit comments