import { useEffect, useMemo, useState } from 'react'; import { zodResolver } from '@hookform/resolvers/zod'; import { msg } from '@lingui/core/macro'; import { useLingui } from '@lingui/react'; import { Trans } from '@lingui/react/macro'; import type * as DialogPrimitive from '@radix-ui/react-dialog'; import { useForm } from 'react-hook-form'; import { useSearchParams } from 'react-router'; import type { z } from 'zod'; import { useUpdateSearchParams } from '@documenso/lib/client-only/hooks/use-update-search-params'; import { useCurrentOrganisation } from '@documenso/lib/client-only/providers/organisation'; import { useSession } from '@documenso/lib/client-only/providers/session'; import { IS_BILLING_ENABLED, NEXT_PUBLIC_WEBAPP_URL } from '@documenso/lib/constants/app'; import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error'; import { trpc } from '@documenso/trpc/react'; import { ZCreateTeamRequestSchema } from '@documenso/trpc/server/team-router/create-team.types'; import { Alert, AlertDescription } from '@documenso/ui/primitives/alert'; import { Button } from '@documenso/ui/primitives/button'; import { Checkbox } from '@documenso/ui/primitives/checkbox'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from '@documenso/ui/primitives/dialog'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@documenso/ui/primitives/form/form'; import { Input } from '@documenso/ui/primitives/input'; import { SpinnerBox } from '@documenso/ui/primitives/spinner'; import { useToast } from '@documenso/ui/primitives/use-toast'; export type TeamCreateDialogProps = { trigger?: React.ReactNode; onCreated?: () => Promise; } & Omit; const ZCreateTeamFormSchema = ZCreateTeamRequestSchema.pick({ teamName: true, teamUrl: true, inheritMembers: true, }); type TCreateTeamFormSchema = z.infer; export const TeamCreateDialog = ({ trigger, onCreated, ...props }: TeamCreateDialogProps) => { const { _ } = useLingui(); const { toast } = useToast(); const { refreshSession } = useSession(); const [searchParams] = useSearchParams(); const updateSearchParams = useUpdateSearchParams(); const organisation = useCurrentOrganisation(); const [open, setOpen] = useState(false); const { data: fullOrganisation } = trpc.organisation.get.useQuery({ organisationReference: organisation.id, }); const actionSearchParam = searchParams?.get('action'); const form = useForm({ resolver: zodResolver(ZCreateTeamFormSchema), defaultValues: { teamName: '', teamUrl: '', inheritMembers: true, }, }); const { mutateAsync: createTeam } = trpc.team.create.useMutation(); const onFormSubmit = async ({ teamName, teamUrl, inheritMembers }: TCreateTeamFormSchema) => { try { await createTeam({ organisationId: organisation.id, teamName, teamUrl, inheritMembers, }); setOpen(false); await onCreated?.(); await refreshSession(); toast({ title: _(msg`Success`), description: _(msg`Your team has been created.`), duration: 5000, }); } catch (err) { const error = AppError.parseError(err); if (error.code === AppErrorCode.ALREADY_EXISTS) { form.setError('teamUrl', { type: 'manual', message: _(msg`This URL is already in use.`), }); return; } toast({ title: _(msg`An unknown error occurred`), description: _( msg`We encountered an unknown error while attempting to create a team. Please try again later.`, ), variant: 'destructive', }); } }; const mapTextToUrl = (text: string) => { return text.toLowerCase().replace(/\s+/g, '-'); }; const dialogState = useMemo(() => { if (!fullOrganisation) { return 'loading'; } if (!IS_BILLING_ENABLED()) { return 'form'; } if (fullOrganisation.organisationClaim.teamCount === 0) { return 'form'; } if (fullOrganisation.organisationClaim.teamCount <= fullOrganisation.teams.length) { return 'alert'; } return 'form'; }, [fullOrganisation]); useEffect(() => { if (actionSearchParam === 'add-team') { setOpen(true); updateSearchParams({ action: null }); } }, [actionSearchParam, open]); useEffect(() => { form.reset(); }, [open, form]); return ( !form.formState.isSubmitting && setOpen(value)} > e.stopPropagation()} asChild={true}> {trigger ?? ( )} Create team Create a team to collaborate with your team members. {dialogState === 'loading' && } {dialogState === 'alert' && ( <> You have reached the maximum number of teams for your plan. Please contact sales at support@documenso.com if you would like to adjust your plan. )} {dialogState === 'form' && (
( Team Name { const oldGeneratedUrl = mapTextToUrl(field.value); const newGeneratedUrl = mapTextToUrl(event.target.value); const urlField = form.getValues('teamUrl'); if (urlField === oldGeneratedUrl) { form.setValue('teamUrl', newGeneratedUrl); } field.onChange(event); }} /> )} /> ( Team URL {!form.formState.errors.teamUrl && ( {field.value ? ( `${NEXT_PUBLIC_WEBAPP_URL()}/t/${field.value}` ) : ( A unique URL to identify your team )} )} )} /> (
)} />
)}
); };