Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
✨ Add dropdown menu with action in poll list (#2166)
Luke Vella6edd5557b273
diff --git a/apps/web/src/features/poll/components/poll-list.tsx b/apps/web/src/features/poll/components/poll-list.tsxdeleted file mode 100644index fb53abf74..000000000--- a/apps/web/src/features/poll/components/poll-list.tsx+++ /dev/null@@ -1,93 +0,0 @@-import { Tooltip, TooltipContent, TooltipTrigger } from "@rallly/ui/tooltip";-import Link from "next/link";-import { CopyLinkButton } from "@/components/copy-link-button";-import { OptimizedAvatarImage } from "@/components/optimized-avatar-image";-import { StackedList, StackedListItem } from "@/components/stacked-list";-import { Trans } from "@/components/trans";-import { PollStatusIcon } from "@/features/poll/components/poll-status-icon";-import type { PollStatus } from "../schema";--export const PollList = StackedList;--export function PollListItem({- title,- status,- participants,- inviteLink,- pollLink,- createdBy,-}: {- title: string;- status: PollStatus;- participants: { id: string; name: string; image?: string }[];- inviteLink: string;- pollLink: string;- createdBy?: { name: string; image?: string };-}) {- return (- <StackedListItem>- <div className="relative -m-4 flex min-w-0 flex-1 items-center gap-2 p-4">- <PollStatusIcon status={status} showTooltip={false} />- <Link- className="min-w-0 text-sm hover:underline focus:ring-ring focus-visible:ring-2"- href={pollLink}- >- <span className="absolute inset-0" />- <span className="block truncate">{title}</span>- </Link>- </div>- <div className="hidden items-center justify-end gap-4 sm:flex">- {participants.length > 0 ? (- <Tooltip>- <TooltipTrigger asChild>- <span className="cursor-help text-muted-foreground text-sm">- <Trans- i18nKey="participantCount"- defaults="{count, plural, =0 {No participants} =1 {1 participant} other {# participants}}"- values={{ count: participants.length }}- />- </span>- </TooltipTrigger>- <TooltipContent>- <ul>- {participants.slice(0, 10).map((participant) => (- <li key={participant.id}>{participant.name}</li>- ))}- {participants.length > 10 && (- <li>- <Trans- i18nKey="moreParticipants"- values={{ count: participants.length - 10 }}- defaults="{count} more…"- />- </li>- )}- </ul>- </TooltipContent>- </Tooltip>- ) : (- <span className="text-muted-foreground text-sm">- <Trans- i18nKey="participantCount"- defaults="{count, plural, =0 {No participants} =1 {1 participant} other {# participants}}"- values={{ count: participants.length }}- />- </span>- )}- {createdBy && (- <Tooltip>- <TooltipTrigger>- <OptimizedAvatarImage- size="sm"- name={createdBy.name}- src={createdBy.image}- />- </TooltipTrigger>- <TooltipContent>{createdBy.name}</TooltipContent>- </Tooltip>- )}- <CopyLinkButton href={inviteLink} />- </div>- </StackedListItem>- );-}diff --git a/apps/web/src/features/poll/components/polls-infinite-list.tsx b/apps/web/src/features/poll/components/polls-infinite-list.tsxindex b0315d404..ac808e1cd 100644--- a/apps/web/src/features/poll/components/polls-infinite-list.tsx+++ b/apps/web/src/features/poll/components/polls-infinite-list.tsx@@ -1,11 +1,41 @@"use client";+import { Button } from "@rallly/ui/button";+import {+ Dialog,+ DialogClose,+ DialogContent,+ DialogDescription,+ DialogFooter,+ DialogHeader,+ DialogTitle,+ useDialog,+} from "@rallly/ui/dialog";+import {+ DropdownMenu,+ DropdownMenuContent,+ DropdownMenuItem,+ DropdownMenuTrigger,+} from "@rallly/ui/dropdown-menu";import { Icon } from "@rallly/ui/icon";-import { StickerIcon } from "lucide-react";+import { toast } from "@rallly/ui/sonner";+import { Tooltip, TooltipContent, TooltipTrigger } from "@rallly/ui/tooltip";+import { absoluteUrl, shortUrl } from "@rallly/utils/absolute-url";+import {+ CircleStopIcon,+ MoreVerticalIcon,+ PlayIcon,+ StickerIcon,+ TrashIcon,+} from "lucide-react";+import Link from "next/link";import React from "react";+import { CopyLinkButton } from "@/components/copy-link-button";+import { OptimizedAvatarImage } from "@/components/optimized-avatar-image";import { Spinner } from "@/components/spinner";+import { StackedList, StackedListItem } from "@/components/stacked-list";import { Trans } from "@/components/trans";-import { PollList, PollListItem } from "@/features/poll/components/poll-list";+import { PollStatusIcon } from "@/features/poll/components/poll-status-icon";import type { PollStatus } from "@/features/poll/schema";import { trpc } from "@/trpc/client";@@ -16,6 +46,200 @@ interface PollsInfiniteListProps {emptyState: React.ReactNode;}+function PollListItem({+ id,+ title,+ status,+ participants,+ user,+}: {+ id: string;+ title: string;+ status: PollStatus;+ participants: { id: string; name: string }[];+ user: { name: string; image: string | null } | null;+}) {+ const deletePollDialog = useDialog();+ const deletePoll = trpc.polls.markAsDeleted.useMutation();+ const closePoll = trpc.polls.close.useMutation();+ const reopenPoll = trpc.polls.reopen.useMutation();+ return (+ <>+ <div className="grid w-full grid-cols-[1fr_auto] gap-2">+ <div className="relative -m-4 flex min-w-0 flex-1 items-center gap-2 p-4">+ <PollStatusIcon status={status} showTooltip={false} />+ <Link+ className="min-w-0 text-sm hover:underline focus:ring-ring focus-visible:ring-2"+ href={absoluteUrl(`/poll/${id}`)}+ >+ <span className="absolute inset-0" />+ <span className="block truncate">{title}</span>+ </Link>+ </div>+ <div className="hidden items-center justify-end gap-4 sm:flex">+ {participants.length > 0 ? (+ <Tooltip>+ <TooltipTrigger asChild>+ <span className="cursor-help text-muted-foreground text-sm">+ <Trans+ i18nKey="participantCount"+ defaults="{count, plural, =0 {No participants} =1 {1 participant} other {# participants}}"+ values={{ count: participants.length }}+ />+ </span>+ </TooltipTrigger>+ <TooltipContent>+ <ul>+ {participants.slice(0, 10).map((participant) => (+ <li key={participant.id}>{participant.name}</li>+ ))}+ {participants.length > 10 && (+ <li>+ <Trans+ i18nKey="moreParticipants"+ values={{ count: participants.length - 10 }}+ defaults="{count} more…"+ />+ </li>+ )}+ </ul>+ </TooltipContent>+ </Tooltip>+ ) : (+ <span className="text-muted-foreground text-sm">+ <Trans+ i18nKey="participantCount"+ defaults="{count, plural, =0 {No participants} =1 {1 participant} other {# participants}}"+ values={{ count: participants.length }}+ />+ </span>+ )}+ {user && (+ <Tooltip>+ <TooltipTrigger>+ <OptimizedAvatarImage+ size="sm"+ name={user.name}+ src={user.image ?? undefined}+ />+ </TooltipTrigger>+ <TooltipContent>{user.name}</TooltipContent>+ </Tooltip>+ )}+ <CopyLinkButton href={shortUrl(`/invite/${id}`)} />+ <DropdownMenu>+ <DropdownMenuTrigger asChild>+ <Button variant="ghost" size="icon">+ <MoreVerticalIcon />+ </Button>+ </DropdownMenuTrigger>+ <DropdownMenuContent align="end">+ {status === "open" && (+ <DropdownMenuItem+ onSelect={() => {+ toast.promise(closePoll.mutateAsync({ pollId: id }), {+ loading: (+ <Trans+ i18nKey="closingPoll"+ defaults="Closing poll..."+ />+ ),+ success: (+ <Trans i18nKey="pollClosed" defaults="Poll closed" />+ ),+ });+ }}+ >+ <Icon>+ <CircleStopIcon />+ </Icon>+ <Trans i18nKey="closePoll" defaults="Close" />+ </DropdownMenuItem>+ )}+ {status === "closed" && (+ <DropdownMenuItem+ onSelect={() => {+ toast.promise(reopenPoll.mutateAsync({ pollId: id }), {+ loading: (+ <Trans+ i18nKey="reopeningPoll"+ defaults="Reopening poll..."+ />+ ),+ });+ }}+ >+ <Icon>+ <PlayIcon />+ </Icon>+ <Trans i18nKey="reopenPoll" defaults="Reopen" />+ </DropdownMenuItem>+ )}+ <DropdownMenuItem onSelect={() => deletePollDialog.trigger()}>+ <Icon>+ <TrashIcon />+ </Icon>+ <span>+ <Trans i18nKey="deleteMenuItem" defaults="Delete" />+ </span>+ </DropdownMenuItem>+ </DropdownMenuContent>+ </DropdownMenu>+ </div>+ </div>+ <Dialog {...deletePollDialog.dialogProps}>+ <DialogContent>+ <DialogHeader>+ <DialogTitle>+ <Trans i18nKey="deletePoll" />+ </DialogTitle>+ <DialogDescription>+ <Trans+ i18nKey="deletePollPrompt"+ defaults="Are you sure you want to delete <b>{title}</b>?"+ values={{ title }}+ components={{+ b: <b className="font-bold" />,+ }}+ />+ </DialogDescription>+ </DialogHeader>+ <DialogFooter>+ <DialogClose asChild>+ <Button>+ <Trans i18nKey="cancel" />+ </Button>+ </DialogClose>+ <Button+ variant="destructive"+ onClick={() => {+ deletePollDialog.dismiss();+ toast.promise(deletePoll.mutateAsync({ pollId: id }), {+ loading: (+ <Trans i18nKey="deletingPoll" defaults="Deleting poll..." />+ ),+ success: (+ <Trans i18nKey="pollDeleted" defaults="Poll deleted" />+ ),+ error: (+ <Trans+ i18nKey="pollDeleteError"+ defaults="Failed to delete poll"+ />+ ),+ });+ }}+ loading={deletePoll.isPending}+ >+ <Trans i18nKey="delete" />+ </Button>+ </DialogFooter>+ </DialogContent>+ </Dialog>+ </>+ );+}+export function PollsInfiniteList({status,search,@@ -90,28 +314,17 @@ export function PollsInfiniteList({}return (- <PollList>- {polls.map((poll) => (- <PollListItem- key={poll.id}- title={poll.title}- status={poll.status}- participants={poll.participants.map((participant) => ({- id: participant.id,- name: participant.name,- image: participant.image,- }))}- inviteLink={poll.inviteLink}- pollLink={`/poll/${poll.id}`}- createdBy={- poll.user- ? {- name: poll.user.name,- image: poll.user.image ?? undefined,- }- : undefined- }- />+ <StackedList>+ {polls.map(({ id, title, status, participants, user }) => (+ <StackedListItem key={id}>+ <PollListItem+ id={id}+ title={title}+ status={status}+ participants={participants}+ user={user}+ />+ </StackedListItem>))}{hasNextPage && (@@ -138,6 +351,6 @@ export function PollsInfiniteList({/></div>)}- </PollList>+ </StackedList>);}diff --git a/apps/web/src/trpc/routers/polls.ts b/apps/web/src/trpc/routers/polls.tsindex 4b6dc0d64..3cd6a618a 100644--- a/apps/web/src/trpc/routers/polls.ts+++ b/apps/web/src/trpc/routers/polls.ts@@ -536,6 +536,40 @@ export const polls = router({revalidatePath("/", "layout");}),+ markAsDeleted: possiblyPublicProcedure+ .input(+ z.object({+ pollId: z.string(),+ }),+ )+ .use(requireUserMiddleware)+ .mutation(async ({ input: { pollId }, ctx }) => {+ const hasAccess = await hasPollAdminAccess(pollId, ctx.user.id);++ if (!hasAccess) {+ throw new TRPCError({+ code: "FORBIDDEN",+ message: "You are not allowed to mark this poll as deleted",+ });+ }++ await prisma.poll.update({+ where: { id: pollId },+ data: { deleted: true, deletedAt: new Date() },+ });++ // Track poll deletion analytics+ ctx.posthog?.capture({+ event: "poll_delete",+ distinctId: ctx.user.id,+ groups: {+ poll: pollId,+ },+ });+ }),+ /**+ * @deprecated Use markAsDeleted instead+ */delete: possiblyPublicProcedure.input(z.object({