// Dialog form for creating and editing vercel_deploy documents import { useState, useCallback } from 'react' import { useClient } from 'sanity' import { Dialog, Box, Stack, Flex, Text, TextInput, Button, Switch, Label, Card, } from '@sanity/ui' import { CheckmarkCircleIcon } from '@sanity/icons' import type { DeployTarget } from '../types' const VERCEL_HOOK_RE = /^https:\/\/api\.vercel\.com\/v1\/integrations\/deploy\// interface DeployTargetFormProps { /** When provided, form is in edit mode; otherwise create mode */ initial?: DeployTarget onSaved: () => void onClose: () => void } export function DeployTargetForm({ initial, onSaved, onClose }: DeployTargetFormProps) { const client = useClient({ apiVersion: '2025-01-01' }) const isEdit = Boolean(initial) const [name, setName] = useState(initial?.name ?? '') const [url, setUrl] = useState(initial?.url ?? '') const [teamId, setTeamId] = useState(initial?.teamId ?? '') const [disableDelete, setDisableDelete] = useState(initial?.disableDeleteAction ?? false) const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const urlValid = !url || VERCEL_HOOK_RE.test(url.trim()) const canSave = name.trim() && url.trim() && urlValid const save = useCallback(async () => { if (!canSave) return setSaving(true) setError(null) const fields = { name: name.trim(), url: url.trim(), ...(teamId.trim() ? { teamId: teamId.trim() } : { teamId: null }), disableDeleteAction: disableDelete, } try { if (isEdit && initial) { await client.patch(initial._id).set(fields).commit() } else { await client.create({ _type: 'vercel_deploy', ...fields }) } onSaved() } catch (err) { setError(err instanceof Error ? err.message : 'Save failed') } finally { setSaving(false) } }, [canSave, isEdit, initial, client, name, url, teamId, disableDelete, onSaved]) return ( ) }