import { zodResolver } from '@hookform/resolvers/zod' import { useForm, useWatch } from 'react-hook-form' import { Button, Dialog, DialogContent, DialogFooter, DialogHeader, DialogSection, DialogSectionSeparator, DialogTitle, Form, FormControl, FormField, Input, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from 'ui' import { Admonition } from 'ui-patterns/Admonition' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import * as z from 'zod' import { COMPUTE_INSTANCE_DEPLOYABLE_RUNTIMES, COMPUTE_INSTANCE_SIZES, COMPUTE_REGION, } from './Compute.constants' import type { ComputeInstanceAccess } from './Compute.types' import { formatSize, generateComputeInstanceName } from './Compute.utils' import { ComputeInstanceSnippetTabs } from './ComputeInstanceSnippetTabs' import { RuntimeBadge } from './RuntimeBadge' const FORM_ID = 'deploy-instance-form' const DeployComputeInstanceFormSchema = z.object({ name: z .string() .trim() .min(1, 'Name is required') .regex(/^[a-z0-9-]+$/, 'Lowercase letters, numbers, and hyphens only'), runtime: z.enum(COMPUTE_INSTANCE_DEPLOYABLE_RUNTIMES), size: z.enum(COMPUTE_INSTANCE_SIZES), access: z.enum(['private', 'public']), instances: z .union([z.literal(''), z.coerce.number().int().gte(1).lte(10)]) .refine((value) => value !== '', 'Instances is required'), }) type DeployComputeInstanceFormValues = z.infer const DEFAULT_VALUES: DeployComputeInstanceFormValues = { name: '', runtime: 'deno', size: COMPUTE_INSTANCE_SIZES[0], access: 'private', instances: 1, } const ACCESS_OPTIONS: { value: ComputeInstanceAccess; label: string }[] = [ { value: 'private', label: 'Private' }, { value: 'public', label: 'Public' }, ] interface DeployComputeInstanceDialogProps { open: boolean onOpenChange: (open: boolean) => void } export const DeployComputeInstanceDialog = ({ open, onOpenChange, }: DeployComputeInstanceDialogProps) => { const form = useForm({ mode: 'onBlur', resolver: zodResolver(DeployComputeInstanceFormSchema), defaultValues: { ...DEFAULT_VALUES, name: generateComputeInstanceName() }, }) const [name, runtime, size, access, instances] = useWatch({ control: form.control, name: ['name', 'runtime', 'size', 'access', 'instances'], }) return ( Deploy
( )} /> ( )} /> ( )} /> ( )} /> ( field.onChange( Number.isNaN(e.target.valueAsNumber) ? '' : e.target.valueAsNumber ) } /> )} />
) }