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 { RuntimeBadge } from './RuntimeBadge' import { WORKER_DEPLOYABLE_RUNTIMES, WORKER_SIZES, WORKERS_REGION } from './Workers.constants' import type { WorkerAccess } from './Workers.types' import { formatSize, generateWorkerName } from './Workers.utils' import { WorkerSnippetTabs } from './WorkerSnippetTabs' const FORM_ID = 'deploy-worker-form' const DeployWorkerFormSchema = z.object({ name: z .string() .trim() .min(1, 'Name is required') .regex(/^[a-z0-9-]+$/, 'Lowercase letters, numbers, and hyphens only'), runtime: z.enum(WORKER_DEPLOYABLE_RUNTIMES), size: z.enum(WORKER_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 DeployWorkerFormValues = z.infer const DEFAULT_VALUES: DeployWorkerFormValues = { name: '', runtime: 'deno', size: WORKER_SIZES[0], access: 'private', instances: 1, } const ACCESS_OPTIONS: { value: WorkerAccess; label: string }[] = [ { value: 'private', label: 'Private' }, { value: 'public', label: 'Public' }, ] interface DeployWorkerDialogProps { open: boolean onOpenChange: (open: boolean) => void } export const DeployWorkerDialog = ({ open, onOpenChange }: DeployWorkerDialogProps) => { const form = useForm({ mode: 'onBlur', resolver: zodResolver(DeployWorkerFormSchema), defaultValues: { ...DEFAULT_VALUES, name: generateWorkerName() }, }) const [name, runtime, size, access, instances] = useWatch({ control: form.control, name: ['name', 'runtime', 'size', 'access', 'instances'], }) return ( Deploy a worker
( )} /> ( )} /> ( )} /> ( )} /> ( field.onChange( Number.isNaN(e.target.valueAsNumber) ? '' : e.target.valueAsNumber ) } /> )} />
) }