import { zodResolver } from '@hookform/resolvers/zod' import { useForm } from 'react-hook-form' import { toast } from 'sonner' import * as z from 'zod' import { FormActions } from 'components/ui/Forms/FormActions' import type { EnvironmentTargets, Integration, IntegrationProjectConnection, } from 'data/integrations/integrations.types' import { useVercelConnectionUpdateMutation } from 'data/integrations/vercel-connection-update-mutate' import { FormControl_Shadcn_, FormDescription_Shadcn_, FormField_Shadcn_, FormItem_Shadcn_, FormLabel_Shadcn_, FormMessage_Shadcn_, Form_Shadcn_, Input_Shadcn_, Switch, } from 'ui' const VercelIntegrationConnectionForm = ({ disabled, connection, integration, }: { disabled?: boolean connection: IntegrationProjectConnection integration: Integration }) => { const envSyncTargets = connection.env_sync_targets ?? [] const FormSchema = z.object({ environmentVariablesProduction: z.boolean().default(envSyncTargets.includes('production')), environmentVariablesPreview: z.boolean().default(envSyncTargets.includes('preview')), environmentVariablesDevelopment: z.boolean().default(envSyncTargets.includes('development')), publicEnvVarPrefix: z.string().optional(), }) const form = useForm>({ resolver: zodResolver(FormSchema), defaultValues: { environmentVariablesProduction: envSyncTargets.includes('production'), environmentVariablesPreview: envSyncTargets.includes('preview'), environmentVariablesDevelopment: envSyncTargets.includes('development'), publicEnvVarPrefix: connection.public_env_var_prefix, }, }) const { mutate: updateVercelConnection, isLoading } = useVercelConnectionUpdateMutation({ onSuccess: () => { form.reset(form.getValues()) toast.success(`Updated Vercel connection`) }, }) function onSubmit(data: z.infer) { const { environmentVariablesProduction, environmentVariablesPreview, environmentVariablesDevelopment, } = data const envSyncTargets: string[] = [] if (environmentVariablesProduction) envSyncTargets.push('production') if (environmentVariablesPreview) envSyncTargets.push('preview') if (environmentVariablesDevelopment) envSyncTargets.push('development') updateVercelConnection({ id: connection.id, envSyncTargets: envSyncTargets as EnvironmentTargets[], publicEnvVarPrefix: data.publicEnvVarPrefix?.trim(), organizationIntegrationId: integration.id, }) } const vercelConnectionFormId = `vercel-connection-form-${connection.id}` return (
Sync environment variables for selected target environments
(
Production Sync environment variables for production environment.
)} /> (
Preview Sync environment variables for preview environment.
)} /> (
Development Sync environment variables for development environment.
)} />
Customize public environment variable prefix
( Prefix e.g.{' '} { field.onChange('NEXT_PUBLIC_') }} > NEXT_PUBLIC_ ,{' '} { field.onChange('VITE_PUBLIC_') }} > VITE_PUBLIC_ ,{' '} { field.onChange('PUBLIC_') }} > PUBLIC_ , etc. )} />
{form.formState.isDirty ? (

Note: Changing these settings will not trigger a resync of environment variables.

) : (
)} form.reset()} />
) } export default VercelIntegrationConnectionForm