Skip to content

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

4.8 KB
Raw Blame
"use client";

import type { VoteType } from "@rallly/database";
import { cn } from "@rallly/ui";
import { Badge } from "@rallly/ui/badge";
import { Button } from "@rallly/ui/button";
import { MoreHorizontalIcon } from "lucide-react";
import type * as React from "react";

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 { usePoll } from "@/features/poll/components/poll-context";
import type { Vote } from "@/features/poll/types";
import { useUser } from "@/features/user/client";
import { Trans, useTranslation } from "@/i18n/client";

import VoteIcon from "../vote-icon";
import ParticipantRowForm from "./participant-row-form";

export interface ParticipantRowProps {
  participant: {
    id: string;
    name: string;
    userId?: string;
    email?: string;
    editUrl?: string | null;
    note?: string | null;
    createdAt: Date;
    image?: string | null;
    votes: Vote[];
  };
  className?: string;
  editMode?: boolean;
  onChangeEditMode?: (editMode: boolean) => void;
}

export const ParticipantRowView: React.FunctionComponent<{
  name: string;
  image?: string | null;
  action?: React.ReactNode;
  note?: React.ReactNode;
  votes: Array<VoteType | undefined>;
  className?: string;
  isYou?: boolean;
  participantId: string;
}> = ({
  name,
  image,
  action,
  note,
  votes,
  className,
  isYou,
  participantId,
}) => {
  return (
    <tr
      data-testid="participant-row"
      data-participantid={participantId}
      className={cn("group", className)}
    >
      <td
        style={{ minWidth: 235, maxWidth: 235 }}
        className="sticky left-0 z-10 h-12 border-border-muted border-b bg-card px-3 group-[.last-row]:border-b-0"
      >
        <div className="flex max-w-full items-center justify-between gap-x-1">
          <Participant>
            <OptimizedAvatarImage
              size="sm"
              name={name}
              src={image ?? undefined}
            />
            <ParticipantName>{name}</ParticipantName>
          </Participant>
          <div className="flex items-center gap-x-2">
            {isYou ? (
              <Badge variant="secondary" className="shrink-0">
                <Trans i18nKey="you" defaults="You" />
              </Badge>
            ) : null}
            {note}
            {action}
          </div>
        </div>
      </td>
      {votes.map((vote, i) => {
        return (
          <td
            // biome-ignore lint/suspicious/noArrayIndexKey: Fix this later
            key={i}
            className={cn(
              "h-12 border-b border-b-border-muted border-l bg-card group-[.last-row]:border-b-0",
            )}
          >
            <div className={cn("flex items-center justify-center")}>
              <VoteIcon type={vote} />
            </div>
          </td>
        );
      })}
    </tr>
  );
};

const ParticipantRow: React.FunctionComponent<ParticipantRowProps> = ({
  participant,
  editMode,
  className,
  onChangeEditMode,
}) => {
  const { t } = useTranslation();
  const { ownsObject } = useUser();
  const { getVote, optionIds } = usePoll();

  const isYou = ownsObject(participant);

  const { canEditParticipant } = usePermissions();
  const canEdit = canEditParticipant(participant.id);

  if (editMode) {
    return (
      <ParticipantRowForm
        className={className}
        name={participant.name}
        image={participant.image ?? undefined}
        isYou={isYou}
        onCancel={() => onChangeEditMode?.(false)}
      />
    );
  }

  return (
    <ParticipantRowView
      className={className}
      name={participant.name}
      image={participant.image}
      votes={optionIds.map((optionId) => {
        return getVote(participant.id, optionId);
      })}
      participantId={participant.id}
      note={
        // The server only includes notes the viewer may see (host or author).
        participant.note ? (
          <ParticipantNote
            note={participant.note}
            participantName={participant.name}
            createdAt={participant.createdAt}
          />
        ) : null
      }
      action={
        canEdit ? (
          <ParticipantDropdown
            participant={participant}
            align="start"
            onEdit={() => onChangeEditMode?.(true)}
          >
            <Button
              aria-label={t("moreOptions", { defaultValue: "More options" })}
              size="icon-xs"
              variant="ghost"
            >
              <MoreHorizontalIcon />
            </Button>
          </ParticipantDropdown>
        ) : null
      }
      isYou={isYou}
    />
  );
};

export default ParticipantRow;