import { useCallback, useState } from 'react'; import { useLingui } from '@lingui/react'; import { Trans } from '@lingui/react/macro'; import type { Recipient } from '@prisma/client'; import { RecipientRole, SendStatus, SigningStatus } from '@prisma/client'; import { Check, ChevronsUpDown, Info } from 'lucide-react'; import { sortBy } from 'remeda'; import { RECIPIENT_ROLES_DESCRIPTION } from '@documenso/lib/constants/recipient-roles'; import { getRecipientColorStyles } from '../lib/recipient-colors'; import { cn } from '../lib/utils'; import { Button } from './button'; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem } from './command'; import { Popover, PopoverContent, PopoverTrigger } from './popover'; import { Tooltip, TooltipContent, TooltipTrigger } from './tooltip'; export interface RecipientSelectorProps { className?: string; selectedRecipient: Recipient | null; onSelectedRecipientChange: (recipient: Recipient) => void; recipients: Recipient[]; } export const RecipientSelector = ({ className, selectedRecipient, onSelectedRecipientChange, recipients, }: RecipientSelectorProps) => { const { _ } = useLingui(); const [showRecipientsSelector, setShowRecipientsSelector] = useState(false); const recipientsByRole = useCallback(() => { const recipientsByRole: Record = { CC: [], VIEWER: [], SIGNER: [], APPROVER: [], ASSISTANT: [], }; recipients.forEach((recipient) => { recipientsByRole[recipient.role].push(recipient); }); return recipientsByRole; }, [recipients]); const recipientsByRoleToDisplay = useCallback(() => { return Object.entries(recipientsByRole()) .filter( ([role]) => role !== RecipientRole.CC && role !== RecipientRole.VIEWER && role !== RecipientRole.ASSISTANT, ) .map( ([role, roleRecipients]) => [ role, sortBy( roleRecipients, [(r) => r.signingOrder || Number.MAX_SAFE_INTEGER, 'asc'], [(r) => r.id, 'asc'], ), ] as [RecipientRole, Recipient[]], ); }, [recipientsByRole]); return ( No recipient matching this description was found. {recipientsByRoleToDisplay().map(([role, roleRecipients], roleIndex) => (
{_(RECIPIENT_ROLES_DESCRIPTION[role].roleNamePlural)}
{roleRecipients.length === 0 && (
No recipients with this role
)} {roleRecipients.map((recipient) => ( r.id === recipient.id), 0, ), ).comboxBoxItem, { 'text-muted-foreground': recipient.sendStatus === SendStatus.SENT, }, )} onSelect={() => { onSelectedRecipientChange(recipient); setShowRecipientsSelector(false); }} disabled={recipient.signingStatus !== SigningStatus.NOT_SIGNED} > {recipient.name && ( {recipient.name} ({recipient.email}) )} {!recipient.name && {recipient.email}}
{recipient.sendStatus !== SendStatus.SENT ? ( ) : ( This document has already been sent to this recipient. You can no longer edit this recipient. )}
))}
))}
); };