import { zodResolver } from '@hookform/resolvers/zod' import Link from 'next/link' import { useForm } from 'react-hook-form' import { toast } from 'sonner' import { Alert, AlertDescription, AlertTitle, Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, Input, Switch, } from 'ui' 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 { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization' import { DOCS_URL } from '@/lib/constants' const VercelIntegrationConnectionForm = ({ disabled, connection, integration, }: { disabled?: boolean connection: IntegrationProjectConnection integration: Integration }) => { // NOTE(kamil): Ignore sync targets for Vercel Marketplace as it's not synchronized using integration, // but through a separate marketplace mechanism. It's not theoretically necessary, but we might have some stale data. const { data: org } = useSelectedOrganizationQuery() const envSyncTargets = org?.managed_by === 'vercel-marketplace' ? [] : (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 as any), defaultValues: { environmentVariablesProduction: envSyncTargets.includes('production'), environmentVariablesPreview: envSyncTargets.includes('preview'), environmentVariablesDevelopment: envSyncTargets.includes('development'), publicEnvVarPrefix: connection.public_env_var_prefix, }, }) const { mutate: updateVercelConnection, isPending } = 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 (
{org?.managed_by === 'vercel-marketplace' ? ( Vercel Marketplace managed project This project is managed via Vercel Marketplace. Environment variables are automatically synchronized for your connected Vercel projects. This integration purpose is synchronizing preview deployments environment variables with our{' '} Branching {' '} feature. ) : (
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