Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
import { cn } from "@rallly/ui";
import { Badge } from "@rallly/ui/badge";
import { Button } from "@rallly/ui/button";
import { Card } from "@rallly/ui/card";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@rallly/ui/select";
import { MoreHorizontalIcon, PlusIcon, UsersIcon } from "lucide-react";
import { AnimatePresence } from "motion/react";
import * as m from "motion/react-m";
import * as React from "react";
import { TimesShownIn } from "@/components/clock";
import { OptimizedAvatarImage } from "@/components/optimized-avatar-image";
import { usePermissions } from "@/features/poll/client";
import {
Participant,
ParticipantName,
} from "@/features/poll/components/participant";
import { ParticipantDropdown } from "@/features/poll/components/participant-dropdown";
import { ParticipantNote } from "@/features/poll/components/participant-note";
import { useOptions, usePoll } from "@/features/poll/components/poll-context";
import { useVisibleParticipants } from "@/features/poll/components/visibility";
import { VotingFooter } from "@/features/poll/components/voting-footer";
import { useVotingForm } from "@/features/poll/components/voting-form";
import { YouAvatar } from "@/features/poll/components/you-avatar";
import { useUser } from "@/features/user/client";
import { Trans, useTranslation } from "@/i18n/client";
import GroupedOptions from "./mobile-poll/grouped-options";
const MobilePoll: React.FunctionComponent = () => {
const pollContext = usePoll();
const { poll } = pollContext;
const { options } = useOptions();
const session = useUser();
const votingForm = useVotingForm();
const selectedParticipantId = votingForm.watch("participantId");
const visibleParticipants = useVisibleParticipants();
const selectedParticipant = selectedParticipantId
? visibleParticipants.find(
(participant) => participant.id === selectedParticipantId,
)
: undefined;
const { canEditParticipant, canAddNewParticipant } = usePermissions();
const isEditing = votingForm.watch("mode") !== "view";
// True while the sticky footer is pinned to the viewport (floating over
// the option list) rather than resting at the card's end. Tracked via a
// sentinel after the footer: while the card's end is off-screen, the
// footer is stuck.
const footerSentinelRef = React.useRef<HTMLDivElement>(null);
const [isFooterFloating, setIsFooterFloating] = React.useState(true);
React.useEffect(() => {
const sentinel = footerSentinelRef.current;
if (!sentinel) {
return;
}
const observer = new IntersectionObserver(([entry]) => {
setIsFooterFloating(!entry.isIntersecting);
});
observer.observe(sentinel);
return () => observer.disconnect();
}, []);
const { t } = useTranslation();
const participantOptions = [
{
value: "all",
label: (
<div className="flex items-center gap-x-2.5">
<div>
<UsersIcon className="size-4 shrink-0 text-muted-foreground" />
</div>
<span>
{t("allParticipants", {
defaultValue: "All participants",
})}
</span>
</div>
),
},
...visibleParticipants.map((participant) => ({
value: participant.id,
label: (
<Participant>
<OptimizedAvatarImage
name={participant.name}
src={participant.image ?? undefined}
size="sm"
/>
<ParticipantName>{participant.name}</ParticipantName>
{session.ownsObject(participant) && (
<Badge>
<Trans i18nKey="you" defaults="You" />
</Badge>
)}
</Participant>
),
})),
];
return (
<Card className="overflow-visible">
<div className="flex flex-col space-y-2 border-b p-2">
<div className="flex gap-x-2">
{selectedParticipantId || !isEditing ? (
<Select
items={participantOptions}
value={selectedParticipantId ?? "all"}
onValueChange={(participantId) => {
if (participantId) {
votingForm.setValue("participantId", participantId);
}
}}
disabled={isEditing}
>
<SelectTrigger
className="flex-1"
data-testid="participant-selector"
>
<SelectValue />
</SelectTrigger>
<SelectContent>
{participantOptions.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
) : (
<div className="flex h-9 grow items-center px-1">
<Participant>
<YouAvatar />
<ParticipantName>
{t("you", { defaultValue: "You" })}
</ParticipantName>
</Participant>
</div>
)}
{isEditing ? (
<Button
onClick={() => {
if (votingForm.watch("mode") === "new") {
votingForm.cancel();
} else {
votingForm.setValue("mode", "view");
}
}}
>
{t("cancel")}
</Button>
) : selectedParticipant ? (
<>
{selectedParticipant.note ? (
<ParticipantNote
note={selectedParticipant.note}
participantName={selectedParticipant.name}
createdAt={selectedParticipant.createdAt}
size="icon"
/>
) : null}
<ParticipantDropdown
align="end"
disabled={!canEditParticipant(selectedParticipant.id)}
participant={{
name: selectedParticipant.name,
userId: selectedParticipant.userId ?? undefined,
email: selectedParticipant.email ?? undefined,
editUrl: selectedParticipant.editUrl,
id: selectedParticipant.id,
}}
onEdit={() => {
votingForm.setEditingParticipantId(selectedParticipant.id);
}}
onDelete={() => {
votingForm.setValue("participantId", "all");
}}
>
<Button
aria-label={t("moreOptions", {
defaultValue: "More options",
})}
variant="ghost"
size="icon"
>
<MoreHorizontalIcon />
</Button>
</ParticipantDropdown>
</>
) : canAddNewParticipant ? (
<Button
aria-label={t("addParticipant", {
defaultValue: "Add participant",
})}
size="icon"
onClick={() => {
votingForm.newParticipant();
}}
>
<PlusIcon />
</Button>
) : null}
</div>
</div>
{poll.options[0]?.duration !== 0 && poll.timeZone ? (
<div className="border-b p-2">
<TimesShownIn />
</div>
) : null}
<GroupedOptions
selectedParticipantId={selectedParticipantId}
options={options}
editable={isEditing}
group={(option) => {
if (option.type === "timeSlot") {
return `${option.dow} ${option.day} ${option.month} ${option.year}`;
}
return `${option.month} ${option.year}`;
}}
/>
<AnimatePresence>
{isEditing ? (
<m.div
className="sticky bottom-0 z-20"
variants={{
hidden: { opacity: 0, y: -20, height: 0 },
visible: { opacity: 1, y: 0, height: "auto" },
}}
initial="hidden"
animate="visible"
exit={{
opacity: 0,
y: -10,
height: 0,
transition: { duration: 0.2 },
}}
>
{/* While floating, 3px + the page's p-3 and the card's 1px
border puts the footer's edges at 16px from the viewport,
flush with the floating comments button (right-4). At rest
it returns to the card's normal inset. */}
<VotingFooter
className={cn(
"pb-3 transition-[padding] duration-200 ease-out",
isFooterFloating ? "px-[3px]" : "px-3",
)}
/>
</m.div>
) : null}
</AnimatePresence>
<div ref={footerSentinelRef} aria-hidden="true" />
</Card>
);
};
export default MobilePoll;