import { ButtonContainer } from '@/components/ButtonContainer'; import { InputWithLabel } from '@/components/forms/InputWithLabel'; import { Button } from '@/components/ui/button'; import { Checkbox } from '@/components/ui/checkbox'; import { Combobox } from '@/components/ui/combobox'; import { Label } from '@/components/ui/label'; import { toast } from '@/components/ui/use-toast'; import { useOrganizationParams } from '@/hooks/useOrganizationParams'; import { useRefetchActive } from '@/hooks/useRefetchActive'; import { api, handleError } from '@/utils/api'; import { clipboard } from '@/utils/clipboard'; import { zodResolver } from '@hookform/resolvers/zod'; import { Copy } from 'lucide-react'; import dynamic from 'next/dynamic'; import { Controller, useForm, useWatch } from 'react-hook-form'; import { z } from 'zod'; import { popModal } from '.'; import { ModalContent, ModalHeader } from './Modal/Container'; const Syntax = dynamic(import('@/components/Syntax')); const validator = z.object({ name: z.string().min(1, 'Required'), cors: z.string().min(1, 'Required'), withCors: z.boolean(), projectId: z.string().min(1, 'Required'), }); type IForm = z.infer; export default function CreateProject() { const params = useOrganizationParams(); const refetch = useRefetchActive(); const query = api.project.list.useQuery({ organizationSlug: params.organization, }); const mutation = api.client.create.useMutation({ onError: handleError, onSuccess() { toast({ title: 'Success', description: 'Client created!', }); refetch(); }, }); const { register, handleSubmit, formState, control } = useForm({ resolver: zodResolver(validator), defaultValues: { name: '', cors: '*', projectId: '', withCors: true, }, }); const withCors = useWatch({ control, name: 'withCors', }); if (mutation.isSuccess && mutation.data) { const { clientId, clientSecret, cors } = mutation.data; const snippet = clientSecret ? `const mixan = new Mixan({ clientId: "${clientId}", // Avoid using this on web, rely on cors settings instead // Mostly for react-native and node/backend clientSecret: "${clientSecret}", })` : `const mixan = new Mixan({ clientId: "${clientId}", })`; return (

Your client has been created! You will only see the client secret once so keep it safe 🫣

{clientSecret && ( )}
); } return (
{ mutation.mutate({ ...values, organizationSlug: params.organization, }); })} > ( )} /> {withCors && ( <>
Restrict access by domain names (include https://)
)} { return (
{ field.onChange(value); }} items={ query.data?.map((item) => ({ value: item.id, label: item.name, })) ?? [] } placeholder="Select a project" />
); }} />
); }