import { trpc } from '@documenso/trpc/react'; import { Alert, AlertDescription } from '@documenso/ui/primitives/alert'; import { AvatarWithText } from '@documenso/ui/primitives/avatar'; import { Button } from '@documenso/ui/primitives/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from '@documenso/ui/primitives/dialog'; import { useToast } from '@documenso/ui/primitives/use-toast'; import { msg } from '@lingui/core/macro'; import { useLingui } from '@lingui/react'; import { Trans } from '@lingui/react/macro'; import { useState } from 'react'; import { match } from 'ts-pattern'; /** * The reason a team member cannot be removed from the team. When set, the delete * dialog explains the reason instead of offering a confirm button. */ export type TeamMemberDeleteDisableReason = | 'TEAM_OWNER' | 'HIGHER_ROLE' | 'INHERIT_MEMBER_ENABLED' | 'INHERITED_MEMBER'; export type TeamMemberDeleteDialogProps = { teamId: number; teamName: string; memberId: string; memberName: string; memberEmail: string; disableReason?: TeamMemberDeleteDisableReason | null; trigger?: React.ReactNode; }; export const TeamMemberDeleteDialog = ({ trigger, teamId, teamName, memberId, memberName, memberEmail, disableReason, }: TeamMemberDeleteDialogProps) => { const [open, setOpen] = useState(false); const { _ } = useLingui(); const { toast } = useToast(); const { mutateAsync: deleteTeamMember, isPending: isDeletingTeamMember } = trpc.team.member.delete.useMutation({ onSuccess: () => { toast({ title: _(msg`Success`), description: _(msg`You have successfully removed this user from the team.`), duration: 5000, }); setOpen(false); }, onError: () => { toast({ title: _(msg`An unknown error occurred`), description: _( msg`We encountered an unknown error while attempting to remove this user. Please try again later.`, ), variant: 'destructive', duration: 10000, }); }, }); return ( !isDeletingTeamMember && setOpen(value)}> {trigger ?? ( )} Are you sure? You are about to remove the following user from {teamName}. {disableReason ? ( {match(disableReason) .with('TEAM_OWNER', () => You cannot remove the organisation owner from the team.) .with('HIGHER_ROLE', () => You cannot remove a member with a role higher than your own.) .with('INHERIT_MEMBER_ENABLED', () => ( You cannot remove members from this team while the inherit member feature is enabled. )) .with('INHERITED_MEMBER', () => ( This member is inherited from a group and cannot be removed from the team directly. )) .exhaustive()} ) : ( {memberName}} secondaryText={memberEmail} /> )}
{!disableReason && ( )}
); };