import React, { useState } from 'react'; import { zodResolver } from '@hookform/resolvers/zod'; import { msg } from '@lingui/core/macro'; import { useLingui } from '@lingui/react'; import type { User } from '@prisma/client'; import { useForm } from 'react-hook-form'; import { z } from 'zod'; import profileClaimTeaserImage from '@documenso/assets/images/profile-claim-teaser.png'; import { 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 { cn } from '@documenso/ui/lib/utils'; import { Button } from '@documenso/ui/primitives/button'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } 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 { useToast } from '@documenso/ui/primitives/use-toast'; import { UserProfileSkeleton } from '../general/user-profile-skeleton'; export const ZClaimPublicProfileFormSchema = z.object({ url: z .string() .trim() .toLowerCase() .min(1, { message: 'Please enter a valid username.' }) .regex(/^[a-z0-9-]+$/, { message: 'Username can only container alphanumeric characters and dashes.', }), }); export type TClaimPublicProfileFormSchema = z.infer; export type ClaimPublicProfileDialogFormProps = { open: boolean; onOpenChange?: (open: boolean) => void; onClaimed?: () => void; user: User; }; export const ClaimPublicProfileDialogForm = ({ open, onOpenChange, onClaimed, user, }: ClaimPublicProfileDialogFormProps) => { const { _ } = useLingui(); const { toast } = useToast(); const [claimed, setClaimed] = useState(false); const baseUrl = new URL(NEXT_PUBLIC_WEBAPP_URL() ?? 'http://localhost:3000'); const form = useForm({ values: { url: user.url || '', }, resolver: zodResolver(ZClaimPublicProfileFormSchema), }); const { mutateAsync: updatePublicProfile } = trpc.profile.updatePublicProfile.useMutation(); const isSubmitting = form.formState.isSubmitting; const onFormSubmit = async ({ url }: TClaimPublicProfileFormSchema) => { try { await updatePublicProfile({ url, }); setClaimed(true); onClaimed?.(); } catch (err) { const error = AppError.parseError(err); if (error.code === 'PROFILE_URL_TAKEN') { form.setError('url', { type: 'manual', message: _(msg`This username is already taken`), }); } else if (error.code === 'PREMIUM_PROFILE_URL') { form.setError('url', { type: 'manual', message: error.message, }); } else if (error.code !== AppErrorCode.UNKNOWN_ERROR) { toast({ title: 'An error occurred', description: error.userMessage ?? error.message, variant: 'destructive', }); } else { toast({ title: _(msg`An unknown error occurred`), description: _( msg`We encountered an unknown error while attempting to save your details. Please try again later.`, ), variant: 'destructive', }); } } }; return ( {!claimed && ( <> Introducing public profiles! Reserve your Documenso public profile username profile claim teaser
( Public profile username
{baseUrl.host}/u/{field.value || ''}
)} />
)} {claimed && ( <> All set! We will let you know as soon as this features is launched
)}
); };