'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { zodResolver } from '@hookform/resolvers/zod'; import { useForm } from 'react-hook-form'; import { z } from 'zod'; import { useCopyToClipboard } from '@documenso/lib/client-only/hooks/use-copy-to-clipboard'; import { TRPCClientError } from '@documenso/trpc/client'; import { trpc } from '@documenso/trpc/react'; import type { TCreateTokenMutationSchema } from '@documenso/trpc/server/api-token-router/schema'; import { ZCreateTokenMutationSchema } from '@documenso/trpc/server/api-token-router/schema'; import { cn } from '@documenso/ui/lib/utils'; import { Button } from '@documenso/ui/primitives/button'; import { Card, CardContent } from '@documenso/ui/primitives/card'; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@documenso/ui/primitives/form/form'; import { Input } from '@documenso/ui/primitives/input'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@documenso/ui/primitives/select'; import { Switch } from '@documenso/ui/primitives/switch'; import { useToast } from '@documenso/ui/primitives/use-toast'; import { EXPIRATION_DATES } from '../(dashboard)/settings/token/contants'; const ZCreateTokenFormSchema = ZCreateTokenMutationSchema.extend({ enabled: z.boolean(), }); type TCreateTokenFormSchema = z.infer; export type ApiTokenFormProps = { className?: string; teamId?: number; }; export const ApiTokenForm = ({ className, teamId }: ApiTokenFormProps) => { const router = useRouter(); const [, copy] = useCopyToClipboard(); const { toast } = useToast(); const [newlyCreatedToken, setNewlyCreatedToken] = useState(''); const [noExpirationDate, setNoExpirationDate] = useState(false); const { mutateAsync: createTokenMutation } = trpc.apiToken.createToken.useMutation({ onSuccess(data) { setNewlyCreatedToken(data.token); }, }); const form = useForm({ resolver: zodResolver(ZCreateTokenFormSchema), defaultValues: { tokenName: '', expirationDate: '', enabled: false, }, }); const copyToken = async (token: string) => { try { const copied = await copy(token); if (!copied) { throw new Error('Unable to copy the token'); } toast({ title: 'Token copied to clipboard', description: 'The token was copied to your clipboard.', }); } catch (error) { toast({ title: 'Unable to copy token', description: 'We were unable to copy the token to your clipboard. Please try again.', variant: 'destructive', }); } }; const onSubmit = async ({ tokenName, expirationDate }: TCreateTokenMutationSchema) => { try { await createTokenMutation({ teamId, tokenName, expirationDate: noExpirationDate ? null : expirationDate, }); toast({ title: 'Token created', description: 'A new token was created successfully.', duration: 5000, }); form.reset(); router.refresh(); } catch (error) { if (error instanceof TRPCClientError && error.data?.code === 'BAD_REQUEST') { toast({ title: 'An error occurred', description: error.message, variant: 'destructive', }); } else { toast({ title: 'An unknown error occurred', variant: 'destructive', duration: 5000, description: 'We encountered an unknown error while attempting create the new token. Please try again later.', }); } } }; return (
( Token name
Please enter a meaningful name for your token. This will help you identify it later.
)} />
( Token expiration date
)} /> ( Never expire
{ setNoExpirationDate((prev) => !prev); field.onChange(val); }} />
)} />
{newlyCreatedToken && (

Your token was created successfully! Make sure to copy it because you won't be able to see it again!

{newlyCreatedToken}

)}
); };