import { useEffect, useMemo, useState } from 'react'; import { zodResolver } from '@hookform/resolvers/zod'; import { Trans, useLingui } from '@lingui/react/macro'; import { OrganisationGroupType, TeamMemberRole } from '@prisma/client'; import type * as DialogPrimitive from '@radix-ui/react-dialog'; import { useForm } from 'react-hook-form'; import { match } from 'ts-pattern'; import { z } from 'zod'; import { TEAM_MEMBER_ROLE_HIERARCHY } from '@documenso/lib/constants/teams'; import { TEAM_MEMBER_ROLE_MAP } from '@documenso/lib/constants/teams-translations'; import { trpc } from '@documenso/trpc/react'; import { Button } from '@documenso/ui/primitives/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from '@documenso/ui/primitives/dialog'; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@documenso/ui/primitives/form/form'; import { Input } from '@documenso/ui/primitives/input'; import { MultiSelectCombobox } from '@documenso/ui/primitives/multi-select-combobox'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@documenso/ui/primitives/select'; import { useToast } from '@documenso/ui/primitives/use-toast'; import { useCurrentTeam } from '~/providers/team'; export type TeamGroupCreateDialogProps = Omit; const ZAddTeamMembersFormSchema = z.object({ groups: z.array( z.object({ organisationGroupId: z.string(), teamRole: z.nativeEnum(TeamMemberRole), }), ), }); type TAddTeamMembersFormSchema = z.infer; export const TeamGroupCreateDialog = ({ ...props }: TeamGroupCreateDialogProps) => { const [open, setOpen] = useState(false); const [step, setStep] = useState<'SELECT' | 'ROLES'>('SELECT'); const { t } = useLingui(); const { toast } = useToast(); const team = useCurrentTeam(); const form = useForm({ resolver: zodResolver(ZAddTeamMembersFormSchema), defaultValues: { groups: [], }, }); const { mutateAsync: createTeamGroups } = trpc.team.group.createMany.useMutation(); const organisationGroupQuery = trpc.organisation.group.find.useQuery({ organisationId: team.organisationId, perPage: 100, // Won't really work if they somehow have more than 100 groups. types: [OrganisationGroupType.CUSTOM], }); const teamGroupQuery = trpc.team.group.find.useQuery({ teamId: team.id, perPage: 100, // Won't really work if they somehow have more than 100 groups. }); const avaliableOrganisationGroups = useMemo(() => { const organisationGroups = organisationGroupQuery.data?.data ?? []; const teamGroups = teamGroupQuery.data?.data ?? []; return organisationGroups.filter( (group) => !teamGroups.some((teamGroup) => teamGroup.organisationGroupId === group.id), ); }, [organisationGroupQuery, teamGroupQuery]); const onFormSubmit = async ({ groups }: TAddTeamMembersFormSchema) => { try { await createTeamGroups({ teamId: team.id, groups, }); toast({ title: t`Success`, description: t`Team members have been added.`, duration: 5000, }); setOpen(false); } catch { toast({ title: t`An unknown error occurred`, description: t`We encountered an unknown error while attempting to add team members. Please try again later.`, variant: 'destructive', }); } }; useEffect(() => { if (!open) { form.reset(); setStep('SELECT'); } }, [open, form]); return ( e.stopPropagation()} asChild> {match(step) .with('SELECT', () => ( Add members Select members or groups of members to add to the team. )) .with('ROLES', () => ( Add group roles Configure the team roles for each group )) .exhaustive()}
{step === 'SELECT' && ( <> ( Groups ({ label: group.name ?? group.organisationRole, value: group.id, }))} loading={organisationGroupQuery.isLoading || teamGroupQuery.isLoading} selectedValues={field.value.map( ({ organisationGroupId }) => organisationGroupId, )} onChange={(value) => { field.onChange( value.map((organisationGroupId) => ({ organisationGroupId, teamRole: field.value.find( (value) => value.organisationGroupId === organisationGroupId, )?.teamRole || TeamMemberRole.MEMBER, })), ); }} className="bg-background w-full" emptySelectionPlaceholder={t`Select groups`} /> Select groups to add to this team )} /> )} {step === 'ROLES' && ( <>
{form.getValues('groups').map((group, index) => (
{index === 0 && ( Group )} id === group.organisationGroupId, )?.name || t`Untitled Group` } />
( {index === 0 && ( Team Role )} )} />
))}
)}
); };