'use client'; import { useState } from 'react'; import AnimateHeight from '@/components/animate-height'; import { CreateClientSuccess } from '@/components/clients/create-client-success'; import { Button, buttonVariants } from '@/components/ui/button'; import { Combobox } from '@/components/ui/combobox'; import { DialogFooter } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Switch } from '@/components/ui/switch'; import { useAppParams } from '@/hooks/useAppParams'; import { api, handleError } from '@/trpc/client'; import { cn } from '@/utils/cn'; import { zodResolver } from '@hookform/resolvers/zod'; import { SaveIcon } from 'lucide-react'; import { useRouter } from 'next/navigation'; import type { SubmitHandler } from 'react-hook-form'; import { Controller, useForm } from 'react-hook-form'; import { toast } from 'sonner'; import { z } from 'zod'; import { popModal } from '.'; import { ModalContent, ModalHeader } from './Modal/Container'; const validation = z.object({ name: z.string().min(1), cors: z.string().url().or(z.literal('')), projectId: z.string(), type: z.enum(['read', 'write', 'root']), }); type IForm = z.infer; interface Props { projectId: string; } export default function AddClient(props: Props) { const { organizationSlug, projectId } = useAppParams(); const router = useRouter(); const form = useForm({ resolver: zodResolver(validation), defaultValues: { name: '', cors: '', projectId: props.projectId ?? projectId, type: 'write', }, }); const [hasDomain, setHasDomain] = useState(true); const mutation = api.client.create.useMutation({ onError: handleError, onSuccess() { toast.success('Client created'); router.refresh(); }, }); const query = api.project.list.useQuery({ organizationSlug, }); const onSubmit: SubmitHandler = (values) => { mutation.mutate({ name: values.name, cors: hasDomain ? values.cors : null, projectId: values.projectId, organizationSlug, type: values.type, }); }; return ( {mutation.isSuccess ? ( <>
Read docs
) : ( <>
{ return (
{ field.onChange(value); }} items={ query.data?.map((item) => ({ value: item.id, label: item.name, })) ?? [] } placeholder="Select a project" />
); }} />
{ return (
{ field.onChange(value); }} items={[ { value: 'write', label: 'Write (for ingestion)', }, { value: 'read', label: 'Read (access export API)', }, { value: 'root', label: 'Root (access export API)', }, ]} placeholder="Select a project" />

{field.value === 'write' && 'Write: Is the default client type and is used for ingestion of data'} {field.value === 'read' && 'Read: You can access the current projects data from the export API'} {field.value === 'root' && 'Root: You can access any projects data from the export API'}

); }} />
)}
); } { /*
Select your framework and we'll generate a client for you.
*/ }