Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
"use client";
import type { VoteType } from "@rallly/database";
import { cn } from "@rallly/ui";
import { Button } from "@rallly/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
useDialog,
} from "@rallly/ui/dialog";
import * as m from "motion/react-m";
import type * as React from "react";
import { IfScoresVisible } from "@/features/poll/components/visibility";
import { PollOptionVoteSummary } from "@/features/poll/components/vote-breakdown-dialog";
import { Trans, useTranslation } from "@/i18n/client";
import { ConnectedScoreSummary } from "../score-summary";
import VoteIcon from "../vote-icon";
import { VoteSegmentedControl } from "../vote-segmented-control";
export interface PollOptionProps {
children?: React.ReactNode;
yesScore: number;
ifNeedBeScore: number;
editable?: boolean;
vote?: VoteType;
onChange: (vote: VoteType) => void;
selectedParticipantId?: string;
optionId: string;
optionLabel: string;
/** When false the segmented control offers yes and no only. */
allowTentativeVotes?: boolean;
}
const PollOption: React.FunctionComponent<PollOptionProps> = ({
children,
selectedParticipantId,
vote,
onChange,
editable = false,
optionId,
optionLabel,
yesScore,
ifNeedBeScore,
allowTentativeVotes = true,
}) => {
const { t } = useTranslation();
const dialog = useDialog();
const showVote = !editable && !!selectedParticipantId;
return (
<div
className={cn(
"grid h-14 items-center px-2 text-left transition-[grid-template-columns] duration-300",
editable &&
(allowTentativeVotes
? "grid-cols-[0rem_1fr_3.5rem_9.375rem]"
: "grid-cols-[0rem_1fr_3.5rem_6.625rem]"),
showVote && "grid-cols-[1.875rem_1fr_3.5rem_0rem]",
!editable && !showVote && "grid-cols-[0rem_1fr_3.5rem_0rem]",
)}
data-testid="poll-option"
>
<span
aria-hidden={showVote ? undefined : true}
className="overflow-hidden"
>
{showVote ? <VoteIcon type={vote} /> : null}
</span>
{children}
<IfScoresVisible>
<Button
{...dialog.triggerProps}
aria-label={`${
allowTentativeVotes
? t("optionVoteBreakdown", {
defaultValue: "{yesScore} yes, {ifNeedBeScore} if need be",
yesScore,
ifNeedBeScore,
})
: t("optionVoteBreakdownYesOnly", {
defaultValue: "{yesScore} yes",
yesScore,
})
}. ${t("showParticipantVotes", {
defaultValue: "Show participant votes",
})}`}
variant="ghost"
size="lg"
className="col-start-3 justify-self-end"
>
<ConnectedScoreSummary optionId={optionId} />
</Button>
<Dialog {...dialog.dialogProps}>
<DialogContent size="sm">
<DialogHeader>
<DialogTitle>
<Trans i18nKey="participants" defaults="Participants" />
</DialogTitle>
<DialogDescription>{optionLabel}</DialogDescription>
</DialogHeader>
<PollOptionVoteSummary optionId={optionId} />
</DialogContent>
</Dialog>
</IfScoresVisible>
{editable ? (
<m.div
className="col-start-4 justify-self-end"
initial={{ opacity: 0, x: 12 }}
animate={{ opacity: 1, x: 0 }}
transition={{ duration: 0.3, ease: [0.4, 0, 0.2, 1] }}
>
<VoteSegmentedControl
value={vote}
onChange={onChange}
optionLabel={optionLabel}
allowTentativeVotes={allowTentativeVotes}
/>
</m.div>
) : null}
</div>
);
};
export default PollOption;