import type { ContentPollDetailPayload } from "../queries/content-queries"; import type { ContentPollListState } from "./poll-workflow"; export interface ContentPollResultsProps { readonly pollId: string; readonly detail: ContentPollDetailPayload; readonly list: Pick< ContentPollListState, "responseQuestionId" | "responsePageSize" | "responseOffset" >; } function displayDate(value: string): string { return new Intl.DateTimeFormat("en-GB", { dateStyle: "medium", timeStyle: "short", timeZone: "UTC", }).format(new Date(value)); } function responseHref( pollId: string, questionId: string, pageSize: number, offset: number, ): string { const params = new URLSearchParams(); params.set("responseQuestionId", questionId); params.set("responsePageSize", String(pageSize)); params.set("responseOffset", String(Math.max(0, offset))); return `/ase-next/content/engagement/polls/${encodeURIComponent(pollId)}?${params.toString()}`; } function voteLabel(count: number): string { return `${count} ${count === 1 ? "vote" : "votes"}`; } export function ContentPollResults({ pollId, detail, list, }: ContentPollResultsProps) { const choiceQuestions = detail.questions.filter( (question) => question.type !== "text", ); const textQuestions = detail.questions.filter( (question) => question.type === "text", ); const selectedQuestionId = detail.textResponses.questionId; const previousOffset = Math.max( 0, list.responseOffset - list.responsePageSize, ); const nextOffset = list.responseOffset + list.responsePageSize; return (

Results

{choiceQuestions.length === 0 ? (

No choice results are available.

) : (
{choiceQuestions.map((question) => (

{question.question}

{voteLabel(question.answerCount)} received

    {question.choiceResults.map((result) => { const percentage = Math.round( (result.count / Math.max(1, question.answerCount)) * 100, ); return (
  • {result.option} {voteLabel(result.count)} ยท {percentage}%
  • ); })}
))}
)}

Free-text responses

{textQuestions.length === 0 ? (

No free-text questions are configured.

) : ( <> {detail.textResponses.items.length === 0 ? (

No responses for this question.

) : (
{detail.textResponses.items.map((response) => ( ))}
Free-text Poll responses
User Answer Submitted
{response.username ?? "Unavailable user"} (user{" "} {response.userId}) {response.answer}
)} {selectedQuestionId && (list.responseOffset > 0 || nextOffset < detail.textResponses.total) ? ( ) : null} )}
); }