Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

✨ Use user avatar when available for participant (#2284)

Luke Vellabf95757998a8
diff --git a/apps/web/src/app/[locale]/(app)/(optional-space)/poll/[urlId]/edit-options/page.tsx b/apps/web/src/app/[locale]/(app)/(optional-space)/poll/[urlId]/edit-options/page.tsx
index 6b1757cae..90afa0a05 100644
--- a/apps/web/src/app/[locale]/(app)/(optional-space)/poll/[urlId]/edit-options/page.tsx
+++ b/apps/web/src/app/[locale]/(app)/(optional-space)/poll/[urlId]/edit-options/page.tsx
@@ -8,6 +8,10 @@ import { useRouter } from "next/navigation";
import { useForm } from "react-hook-form";
import PollOptionsForm from "@/components/forms/poll-options-form";
import { useModalContext } from "@/components/modal/modal-provider";
+import {
+ filterParticipantsByVote,
+ useParticipants,
+} from "@/components/participants-provider";
import { useUpdatePollMutation } from "@/components/poll/mutations";
import { usePoll } from "@/components/poll-context";
import { Trans, useTranslation } from "@/i18n/client";
@@ -32,7 +36,8 @@ const convertOptionToString = (
};
const Page = () => {
- const { poll, getParticipantsWhoVotedForOption } = usePoll();
+ const { poll } = usePoll();
+ const { participants } = useParticipants();
const { mutate: updatePollMutation, isPending: isUpdating } =
useUpdatePollMutation();
const { t } = useTranslation();
@@ -119,7 +124,9 @@ const Page = () => {
};
const optionsToDeleteThatHaveVotes = optionsToDelete.filter(
- (option) => getParticipantsWhoVotedForOption(option.id).length > 0,
+ (option) =>
+ filterParticipantsByVote(participants, option.id, "yes").length >
+ 0,
);
if (optionsToDeleteThatHaveVotes.length > 0) {
diff --git a/apps/web/src/components/participants-provider.tsx b/apps/web/src/components/participants-provider.tsx
index 83389aa86..d91fbd373 100644
--- a/apps/web/src/components/participants-provider.tsx
+++ b/apps/web/src/components/participants-provider.tsx
@@ -1,52 +1,27 @@
-import type { Participant, VoteType } from "@rallly/database";
+import type { VoteType } from "@rallly/database";
+import { useParams } from "next/navigation";
import * as React from "react";
-
import { useVisibility } from "@/components/visibility";
import { usePermissions } from "@/contexts/permissions";
import { trpc } from "@/trpc/client";
-import type { Vote } from "@/trpc/client/types";
-
-import { useRequiredContext } from "./use-required-context";
-const ParticipantsContext = React.createContext<{
- participants: Array<Participant & { votes: Vote[] }>;
- getParticipants: (optionId: string, voteType: VoteType) => Participant[];
-} | null>(null);
+export function filterParticipantsByVote<
+ T extends { votes: { optionId: string; type: VoteType }[] },
+>(participants: T[], optionId: string, voteType: VoteType): T[] {
+ return participants.filter((participant) => {
+ return participant.votes.some((vote) => {
+ return vote.optionId === optionId && vote.type === voteType;
+ });
+ });
+}
export const useParticipants = () => {
- return useRequiredContext(ParticipantsContext);
-};
-
-export const ParticipantsProvider: React.FunctionComponent<{
- children?: React.ReactNode;
- pollId: string;
-}> = ({ children, pollId }) => {
- const { data: participants } = trpc.polls.participants.list.useQuery({
- pollId,
+ const urlId = useParams<{ urlId: string }>().urlId;
+ const [participants] = trpc.polls.participants.list.useSuspenseQuery({
+ pollId: urlId,
});
- const getParticipants = (
- optionId: string,
- voteType: VoteType,
- ): Participant[] => {
- if (!participants) {
- return [];
- }
- return participants.filter((participant) => {
- return participant.votes.some((vote) => {
- return vote.optionId === optionId && vote.type === voteType;
- });
- });
- };
- if (!participants) {
- return null;
- }
-
- return (
- <ParticipantsContext.Provider value={{ participants, getParticipants }}>
- {children}
- </ParticipantsContext.Provider>
- );
+ return { participants };
};
export const useVisibleParticipants = () => {
diff --git a/apps/web/src/components/poll-context.tsx b/apps/web/src/components/poll-context.tsx
index 798f50745..b1408c2f2 100644
--- a/apps/web/src/components/poll-context.tsx
+++ b/apps/web/src/components/poll-context.tsx
@@ -1,10 +1,9 @@
-import type { Participant, VoteType } from "@rallly/database";
-import { keyBy } from "lodash";
+import type { VoteType } from "@rallly/database";
import { TrashIcon } from "lucide-react";
import React from "react";
import { useTranslation } from "@/i18n/client";
import { dayjs } from "@/lib/dayjs";
-import type { GetPollApiResponse, Vote } from "@/trpc/client/types";
+import type { GetPollApiResponse } from "@/trpc/client/types";
import type {
ParsedDateOption,
ParsedTimeSlotOption,
@@ -21,17 +20,12 @@ type PollContextValue = {
admin: boolean;
highScore: number;
optionIds: string[];
- // TODO (Luke Vella) [2022-05-18]: Move this stuff to participants provider
- getParticipantsWhoVotedForOption: (optionId: string) => Participant[]; // maybe just attach votes to parsed options
getScore: (optionId: string) => {
yes: number;
ifNeedBe: number;
no: number;
skip: number;
};
- getParticipantById: (
- participantId: string,
- ) => (Participant & { votes: Vote[] }) | undefined;
getVote: (participantId: string, optionId: string) => VoteType | undefined;
};
@@ -86,45 +80,18 @@ export const PollContextProvider: React.FunctionComponent<{
return score > acc ? score : acc;
}, 1);
- const getParticipantById = (participantId: string) => {
- // TODO (Luke Vella) [2022-04-16]: Build an index instead
- const participant = participants?.find(({ id }) => id === participantId);
-
- return participant;
- };
-
const optionIds = poll.options.map(({ id }) => id);
- const participantById = keyBy(
- participants,
- (participant) => participant.id,
- );
-
- const participantsByOptionId: Record<string, Participant[]> = {};
- poll.options.forEach((option) => {
- participantsByOptionId[option.id] = (participants ?? []).filter(
- (participant) =>
- participant.votes.some(
- ({ type, optionId }) => optionId === option.id && type === "yes",
- ),
- );
- });
-
return {
optionIds,
poll,
urlId,
admin,
- getParticipantById: (participantId) => {
- return participantById[participantId];
- },
highScore,
- getParticipantsWhoVotedForOption: (optionId: string) =>
- participantsByOptionId[optionId],
getVote: (participantId, optionId) => {
- const vote = getParticipantById(participantId)?.votes.find(
- (vote) => vote.optionId === optionId,
- );
+ const vote = participants
+ .find((participant) => participant.id === participantId)
+ ?.votes.find((vote) => vote.optionId === optionId);
return vote?.type;
},
getScore,
diff --git a/apps/web/src/components/poll/desktop-poll.tsx b/apps/web/src/components/poll/desktop-poll.tsx
index 13f4c420c..94c8e5c02 100644
--- a/apps/web/src/components/poll/desktop-poll.tsx
+++ b/apps/web/src/components/poll/desktop-poll.tsx
@@ -332,6 +332,7 @@ const DesktopPoll: React.FunctionComponent = () => {
name: participant.name,
userId: participant.userId ?? undefined,
email: participant.email ?? undefined,
+ image: participant.image,
votes: participant.votes,
}}
editMode={
diff --git a/apps/web/src/components/poll/desktop-poll/participant-row.tsx b/apps/web/src/components/poll/desktop-poll/participant-row.tsx
index afbd4df95..48dc008d6 100644
--- a/apps/web/src/components/poll/desktop-poll/participant-row.tsx
+++ b/apps/web/src/components/poll/desktop-poll/participant-row.tsx
@@ -24,6 +24,7 @@ export interface ParticipantRowProps {
name: string;
userId?: string;
email?: string;
+ image?: string | null;
votes: Vote[];
};
className?: string;
@@ -34,12 +35,22 @@ export interface ParticipantRowProps {
export const ParticipantRowView: React.FunctionComponent<{
name: string;
email?: string;
+ image?: string | null;
action?: React.ReactNode;
votes: Array<VoteType | undefined>;
className?: string;
isYou?: boolean;
participantId: string;
-}> = ({ name, email, action, votes, className, isYou, participantId }) => {
+}> = ({
+ name,
+ email,
+ image,
+ action,
+ votes,
+ className,
+ isYou,
+ participantId,
+}) => {
return (
<tr
data-testid="participant-row"
@@ -52,7 +63,12 @@ export const ParticipantRowView: React.FunctionComponent<{
>
<div className="flex max-w-full items-center justify-between gap-x-1">
<Participant>
- <OptimizedAvatarImage size="sm" name={name} email={email} />
+ <OptimizedAvatarImage
+ size="sm"
+ name={name}
+ email={email}
+ src={image ?? undefined}
+ />
<ParticipantName>{name}</ParticipantName>
</Participant>
<div className="flex items-center gap-x-2">
@@ -116,6 +132,7 @@ const ParticipantRow: React.FunctionComponent<ParticipantRowProps> = ({
className={className}
name={participant.name}
email={participant.email}
+ image={participant.image}
votes={optionIds.map((optionId) => {
return getVote(participant.id, optionId);
})}
diff --git a/apps/web/src/components/poll/mobile-poll.tsx b/apps/web/src/components/poll/mobile-poll.tsx
index 678d03471..d3ccffb50 100644
--- a/apps/web/src/components/poll/mobile-poll.tsx
+++ b/apps/web/src/components/poll/mobile-poll.tsx
@@ -42,7 +42,7 @@ if (typeof window !== "undefined") {
const MobilePoll: React.FunctionComponent = () => {
const pollContext = usePoll();
- const { poll, getParticipantById } = pollContext;
+ const { poll } = pollContext;
const { options } = useOptions();
@@ -55,7 +55,9 @@ const MobilePoll: React.FunctionComponent = () => {
const visibleParticipants = useVisibleParticipants();
const selectedParticipant = selectedParticipantId
- ? getParticipantById(selectedParticipantId)
+ ? visibleParticipants.find(
+ (participant) => participant.id === selectedParticipantId,
+ )
: undefined;
const { canEditParticipant, canAddNewParticipant } = usePermissions();
@@ -110,6 +112,7 @@ const MobilePoll: React.FunctionComponent = () => {
<OptimizedAvatarImage
name={participant.name}
email={participant.email ?? undefined}
+ src={participant.image ?? undefined}
size="sm"
/>
<ParticipantName>{participant.name}</ParticipantName>
diff --git a/apps/web/src/components/poll/mobile-poll/poll-option.tsx b/apps/web/src/components/poll/mobile-poll/poll-option.tsx
index d6b6079fa..522b9ec6f 100644
--- a/apps/web/src/components/poll/mobile-poll/poll-option.tsx
+++ b/apps/web/src/components/poll/mobile-poll/poll-option.tsx
@@ -2,7 +2,7 @@
/** biome-ignore-all lint/a11y/useKeyWithClickEvents: fix later */
/** biome-ignore-all lint/a11y/useFocusableInteractive: fix later */
"use client";
-import type { Participant, VoteType } from "@rallly/database";
+import type { VoteType } from "@rallly/database";
import { cn } from "@rallly/ui";
import { Button } from "@rallly/ui/button";
import { Icon } from "@rallly/ui/icon";
@@ -11,7 +11,10 @@ import * as React from "react";
import { useToggle } from "react-use";
import { OptimizedAvatarImage } from "@/components/optimized-avatar-image";
-import { useParticipants } from "@/components/participants-provider";
+import {
+ filterParticipantsByVote,
+ useParticipants,
+} from "@/components/participants-provider";
import { usePoll } from "@/contexts/poll";
import { useRole } from "@/contexts/role";
import { useTranslation } from "@/i18n/client";
@@ -27,7 +30,6 @@ export interface PollOptionProps {
editable?: boolean;
vote?: VoteType;
onChange: (vote: VoteType) => void;
- participants: Participant[];
selectedParticipantId?: string;
optionId: string;
}
@@ -36,10 +38,22 @@ const PollOptionVoteSummary: React.FunctionComponent<{ optionId: string }> = ({
optionId,
}) => {
const { t } = useTranslation();
- const { getParticipants } = useParticipants();
- const participantsWhoVotedYes = getParticipants(optionId, "yes");
- const participantsWhoVotedIfNeedBe = getParticipants(optionId, "ifNeedBe");
- const participantsWhoVotedNo = getParticipants(optionId, "no");
+ const { participants } = useParticipants();
+ const participantsWhoVotedYes = filterParticipantsByVote(
+ participants,
+ optionId,
+ "yes",
+ );
+ const participantsWhoVotedIfNeedBe = filterParticipantsByVote(
+ participants,
+ optionId,
+ "ifNeedBe",
+ );
+ const participantsWhoVotedNo = filterParticipantsByVote(
+ participants,
+ optionId,
+ "no",
+ );
const noVotes =
participantsWhoVotedYes.length + participantsWhoVotedIfNeedBe.length === 0;
return (
@@ -51,7 +65,7 @@ const PollOptionVoteSummary: React.FunctionComponent<{ optionId: string }> = ({
) : (
<div className="grid grid-cols-2 gap-2">
<div className="col-span-1 space-y-2.5">
- {participantsWhoVotedYes.map(({ name, email }, i) => (
+ {participantsWhoVotedYes.map(({ name, email, image }, i) => (
// biome-ignore lint/suspicious/noArrayIndexKey: Fix this later
<div key={i} className="flex">
<div className="relative mr-2.5 flex size-5 items-center justify-center">
@@ -59,6 +73,7 @@ const PollOptionVoteSummary: React.FunctionComponent<{ optionId: string }> = ({
size="sm"
name={name}
email={email ?? undefined}
+ src={image ?? undefined}
/>
<VoteIcon
type="yes"
@@ -69,7 +84,7 @@ const PollOptionVoteSummary: React.FunctionComponent<{ optionId: string }> = ({
<div className="truncate text-sm">{name}</div>
</div>
))}
- {participantsWhoVotedIfNeedBe.map(({ name, email }, i) => (
+ {participantsWhoVotedIfNeedBe.map(({ name, email, image }, i) => (
// biome-ignore lint/suspicious/noArrayIndexKey: Fix this later
<div key={i} className="flex">
<div className="relative mr-2.5 flex size-5 items-center justify-center">
@@ -77,6 +92,7 @@ const PollOptionVoteSummary: React.FunctionComponent<{ optionId: string }> = ({
size="sm"
name={name}
email={email ?? undefined}
+ src={image ?? undefined}
/>
<VoteIcon
type="ifNeedBe"
@@ -89,7 +105,7 @@ const PollOptionVoteSummary: React.FunctionComponent<{ optionId: string }> = ({
))}
</div>
<div className="col-span-1 space-y-2.5">
- {participantsWhoVotedNo.map(({ name, email }, i) => (
+ {participantsWhoVotedNo.map(({ name, email, image }, i) => (
// biome-ignore lint/suspicious/noArrayIndexKey: Fix this later
<div key={i} className="flex">
<div className="relative mr-2.5 flex size-5 items-center justify-center">
@@ -97,6 +113,7 @@ const PollOptionVoteSummary: React.FunctionComponent<{ optionId: string }> = ({
size="sm"
name={name}
email={email ?? undefined}
+ src={image ?? undefined}
/>
<VoteIcon
type="no"
diff --git a/apps/web/src/components/poll/mobile-poll/poll-options.tsx b/apps/web/src/components/poll/mobile-poll/poll-options.tsx
index 73264ed55..7b8b974b6 100644
--- a/apps/web/src/components/poll/mobile-poll/poll-options.tsx
+++ b/apps/web/src/components/poll/mobile-poll/poll-options.tsx
@@ -1,10 +1,10 @@
import type { VoteType } from "@rallly/database";
import type * as React from "react";
import { Controller } from "react-hook-form";
+import { useParticipants } from "@/components/participants-provider";
import { useVotingForm } from "@/components/poll/voting-form";
import { usePoll } from "@/components/poll-context";
import type { ParsedDateTimeOpton } from "@/utils/date-time-utils";
-
import DateOption from "./date-option";
import TimeSlotOption from "./time-slot-option";
@@ -20,21 +20,17 @@ const PollOptions: React.FunctionComponent<PollOptions> = ({
selectedParticipantId,
}) => {
const { control } = useVotingForm();
- const {
- getParticipantsWhoVotedForOption,
- getParticipantById,
- getScore,
- getVote,
- optionIds,
- } = usePoll();
+ const { getScore, getVote, optionIds } = usePoll();
+ const { participants: allParticipants } = useParticipants();
const selectedParticipant = selectedParticipantId
- ? getParticipantById(selectedParticipantId)
+ ? allParticipants.find(
+ (participant) => participant.id === selectedParticipantId,
+ )
: undefined;
return (
<div className="divide-y">
{options.map((option) => {
- const participants = getParticipantsWhoVotedForOption(option.optionId);
const score = getScore(option.optionId);
const index = optionIds.indexOf(option.optionId);
return (
@@ -65,7 +61,6 @@ const PollOptions: React.FunctionComponent<PollOptions> = ({
optionId={option.optionId}
yesScore={score.yes}
ifNeedBeScore={score.ifNeedBe}
- participants={participants}
vote={vote}
startTime={option.startTime}
endTime={option.endTime}
@@ -81,7 +76,6 @@ const PollOptions: React.FunctionComponent<PollOptions> = ({
optionId={option.optionId}
yesScore={score.yes}
ifNeedBeScore={score.ifNeedBe}
- participants={participants}
vote={vote}
dow={option.dow}
day={option.day}
diff --git a/apps/web/src/components/poll/poll-context-provider.tsx b/apps/web/src/components/poll/poll-context-provider.tsx
index 16ad2529b..899168991 100644
--- a/apps/web/src/components/poll/poll-context-provider.tsx
+++ b/apps/web/src/components/poll/poll-context-provider.tsx
@@ -1,5 +1,4 @@
import ModalProvider from "@/components/modal/modal-provider";
-import { ParticipantsProvider } from "@/components/participants-provider";
import {
OptionsProvider,
PollContextProvider,
@@ -14,12 +13,10 @@ export const LegacyPollContextProvider = (props: React.PropsWithChildren) => {
}
return (
- <ParticipantsProvider pollId={poll.id}>
- <PollContextProvider poll={poll} urlId={poll.id} admin={false}>
- <ModalProvider>
- <OptionsProvider>{props.children}</OptionsProvider>
- </ModalProvider>
- </PollContextProvider>
- </ParticipantsProvider>
+ <PollContextProvider poll={poll} urlId={poll.id} admin={false}>
+ <ModalProvider>
+ <OptionsProvider>{props.children}</OptionsProvider>
+ </ModalProvider>
+ </PollContextProvider>
);
};
diff --git a/apps/web/src/trpc/routers/polls/participants.ts b/apps/web/src/trpc/routers/polls/participants.ts
index ba8a6fe54..4b796bd4a 100644
--- a/apps/web/src/trpc/routers/polls/participants.ts
+++ b/apps/web/src/trpc/routers/polls/participants.ts
@@ -1,3 +1,4 @@
+import type { Participant, VoteType } from "@rallly/database";
import { prisma } from "@rallly/database";
import { createLogger } from "@rallly/logger";
import { absoluteUrl } from "@rallly/utils/absolute-url";
@@ -20,6 +21,19 @@ const logger = createLogger("participants");
const MAX_PARTICIPANTS = 1000;
+function createParticipantFullDTO(
+ participant: Participant & { user: { image: string | null } | null } & {
+ votes: { optionId: string; type: VoteType }[];
+ },
+) {
+ const { votes, user, ...rest } = participant;
+ return {
+ ...rest,
+ image: user?.image ?? null,
+ votes,
+ };
+}
+
async function canModifyParticipant(participantId: string, userId: string) {
const participant = await prisma.participant.findUnique({
where: { id: participantId },
@@ -104,7 +118,7 @@ export const participants = router({
throw new TRPCError({ code: "NOT_FOUND", message: "Poll not found" });
}
- const participants = await prisma.participant.findMany({
+ const rawParticipants = await prisma.participant.findMany({
where: {
pollId,
deleted: false,
@@ -116,6 +130,11 @@ export const participants = router({
type: true,
},
},
+ user: {
+ select: {
+ image: true,
+ },
+ },
},
orderBy: [
{
@@ -125,6 +144,8 @@ export const participants = router({
],
});
+ const participants = rawParticipants.map(createParticipantFullDTO);
+
// Hide participants if the poll has hideParticipants enabled
// and the current user is not an admin
if (poll.hideParticipants) {
@@ -141,6 +162,7 @@ export const participants = router({
...participant,
name: "Anonymous",
email: null,
+ image: null,
};
});
}
@@ -248,11 +270,21 @@ export const participants = router({
},
},
include: {
+ votes: {
+ select: {
+ optionId: true,
+ type: true,
+ },
+ },
+ user: {
+ select: {
+ image: true,
+ },
+ },
poll: {
select: {
id: true,
title: true,
- userId: true,
},
},
},
@@ -312,7 +344,7 @@ export const participants = router({
},
});
- return participant;
+ return createParticipantFullDTO(participant);
},
),
rename: publicProcedure
@@ -401,7 +433,17 @@ export const participants = router({
id: participantId,
},
include: {
- votes: true,
+ votes: {
+ select: {
+ optionId: true,
+ type: true,
+ },
+ },
+ user: {
+ select: {
+ image: true,
+ },
+ },
},
});
});
@@ -414,6 +456,6 @@ export const participants = router({
},
});
- return participant;
+ return createParticipantFullDTO(participant);
}),
});