VercelIntegrationConnectionForm.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import { zodResolver } from '@hookform/resolvers/zod'
  2. import Link from 'next/link'
  3. import { useForm } from 'react-hook-form'
  4. import { toast } from 'sonner'
  5. import {
  6. Alert,
  7. AlertDescription,
  8. AlertTitle,
  9. Form,
  10. FormControl,
  11. FormDescription,
  12. FormField,
  13. FormItem,
  14. FormLabel,
  15. FormMessage,
  16. Input,
  17. Switch,
  18. } from 'ui'
  19. import * as z from 'zod'
  20. import { FormActions } from '@/components/ui/Forms/FormActions'
  21. import type {
  22. EnvironmentTargets,
  23. Integration,
  24. IntegrationProjectConnection,
  25. } from '@/data/integrations/integrations.types'
  26. import { useVercelConnectionUpdateMutation } from '@/data/integrations/vercel-connection-update-mutate'
  27. import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
  28. import { DOCS_URL } from '@/lib/constants'
  29. const VercelIntegrationConnectionForm = ({
  30. disabled,
  31. connection,
  32. integration,
  33. }: {
  34. disabled?: boolean
  35. connection: IntegrationProjectConnection
  36. integration: Integration
  37. }) => {
  38. // NOTE(kamil): Ignore sync targets for Vercel Marketplace as it's not synchronized using integration,
  39. // but through a separate marketplace mechanism. It's not theoretically necessary, but we might have some stale data.
  40. const { data: org } = useSelectedOrganizationQuery()
  41. const envSyncTargets =
  42. org?.managed_by === 'vercel-marketplace' ? [] : (connection.env_sync_targets ?? [])
  43. const FormSchema = z.object({
  44. environmentVariablesProduction: z.boolean().default(envSyncTargets.includes('production')),
  45. environmentVariablesPreview: z.boolean().default(envSyncTargets.includes('preview')),
  46. environmentVariablesDevelopment: z.boolean().default(envSyncTargets.includes('development')),
  47. publicEnvVarPrefix: z.string().optional(),
  48. })
  49. const form = useForm<z.infer<typeof FormSchema>>({
  50. resolver: zodResolver(FormSchema as any),
  51. defaultValues: {
  52. environmentVariablesProduction: envSyncTargets.includes('production'),
  53. environmentVariablesPreview: envSyncTargets.includes('preview'),
  54. environmentVariablesDevelopment: envSyncTargets.includes('development'),
  55. publicEnvVarPrefix: connection.public_env_var_prefix,
  56. },
  57. })
  58. const { mutate: updateVercelConnection, isPending } = useVercelConnectionUpdateMutation({
  59. onSuccess: () => {
  60. form.reset(form.getValues())
  61. toast.success(`Updated Vercel connection`)
  62. },
  63. })
  64. function onSubmit(data: z.infer<typeof FormSchema>) {
  65. const {
  66. environmentVariablesProduction,
  67. environmentVariablesPreview,
  68. environmentVariablesDevelopment,
  69. } = data
  70. const envSyncTargets: string[] = []
  71. if (environmentVariablesProduction) envSyncTargets.push('production')
  72. if (environmentVariablesPreview) envSyncTargets.push('preview')
  73. if (environmentVariablesDevelopment) envSyncTargets.push('development')
  74. updateVercelConnection({
  75. id: connection.id,
  76. envSyncTargets: envSyncTargets as EnvironmentTargets[],
  77. publicEnvVarPrefix: data.publicEnvVarPrefix?.trim(),
  78. organizationIntegrationId: integration.id,
  79. })
  80. }
  81. const vercelConnectionFormId = `vercel-connection-form-${connection.id}`
  82. return (
  83. <Form {...form}>
  84. <form
  85. id={vercelConnectionFormId}
  86. onSubmit={form.handleSubmit(onSubmit)}
  87. className={'w-full space-y-6'}
  88. >
  89. <div className="px-6 py-4 flex flex-col gap-y-4">
  90. <div className="flex flex-col gap-4">
  91. {org?.managed_by === 'vercel-marketplace' ? (
  92. <Alert>
  93. <AlertTitle className="text-sm">Vercel Marketplace managed project</AlertTitle>
  94. <AlertDescription className="text-xs">
  95. This project is managed via Vercel Marketplace. Environment variables are
  96. automatically synchronized for your connected Vercel projects. This integration
  97. purpose is synchronizing preview deployments environment variables with our{' '}
  98. <Link
  99. target="_blank"
  100. rel="noreferrer"
  101. href={`${DOCS_URL}/guides/platform/branching`}
  102. className="underline"
  103. >
  104. Branching
  105. </Link>{' '}
  106. feature.
  107. </AlertDescription>
  108. </Alert>
  109. ) : (
  110. <div>
  111. <h5 className="text-foreground ">
  112. Sync environment variables for selected target environments
  113. </h5>
  114. <FormField
  115. control={form.control}
  116. name="environmentVariablesProduction"
  117. render={({ field }) => (
  118. <FormItem className="space-y-0 flex gap-x-4">
  119. <FormControl>
  120. <Switch
  121. disabled={disabled}
  122. className="mt-1"
  123. checked={field.value}
  124. onCheckedChange={field.onChange}
  125. />
  126. </FormControl>
  127. <div>
  128. <FormLabel className="!text">Production</FormLabel>
  129. <FormDescription className="text-xs text-foreground-lighter">
  130. Sync environment variables for <code>production</code> environment.
  131. </FormDescription>
  132. </div>
  133. </FormItem>
  134. )}
  135. />
  136. <FormField
  137. control={form.control}
  138. name="environmentVariablesPreview"
  139. render={({ field }) => (
  140. <FormItem className="space-y-0 flex gap-x-4">
  141. <FormControl>
  142. <Switch
  143. disabled={disabled}
  144. className="mt-1"
  145. checked={field.value}
  146. onCheckedChange={field.onChange}
  147. />
  148. </FormControl>
  149. <div>
  150. <FormLabel className="!text">Preview</FormLabel>
  151. <FormDescription className="text-xs text-foreground-lighter">
  152. Sync environment variables for <code>preview</code> environment.
  153. </FormDescription>
  154. </div>
  155. </FormItem>
  156. )}
  157. />
  158. <FormField
  159. control={form.control}
  160. name="environmentVariablesDevelopment"
  161. render={({ field }) => (
  162. <FormItem className="space-y-0 flex gap-x-4">
  163. <FormControl>
  164. <Switch
  165. disabled={disabled}
  166. className="mt-1"
  167. checked={field.value}
  168. onCheckedChange={field.onChange}
  169. />
  170. </FormControl>
  171. <div>
  172. <FormLabel className="!text">Development</FormLabel>
  173. <FormDescription className="text-xs text-foreground-lighter">
  174. Sync environment variables for <code>development</code> environment.
  175. </FormDescription>
  176. </div>
  177. </FormItem>
  178. )}
  179. />
  180. </div>
  181. )}
  182. </div>
  183. <h5 className="mt-2 text-foreground">Customize public environment variable prefix</h5>
  184. <div className="flex flex-col gap-4">
  185. <FormField
  186. control={form.control}
  187. name="publicEnvVarPrefix"
  188. render={({ field }) => (
  189. <FormItem className="grid gap-2 md:grid md:grid-cols-12 space-y-0">
  190. <FormLabel className="flex flex-col space-y-2 col-span-4 text-sm justify-center text-foreground-light">
  191. Prefix
  192. </FormLabel>
  193. <FormControl className="col-span-8">
  194. <Input
  195. {...field}
  196. className="w-full"
  197. disabled={disabled}
  198. placeholder="An empty prefix will result in no public env vars"
  199. />
  200. </FormControl>
  201. <FormDescription className="col-start-5 col-span-8 text-xs">
  202. e.g.{' '}
  203. <code
  204. className="cursor-pointer"
  205. role="button"
  206. onClick={() => {
  207. field.onChange('NEXT_PUBLIC_')
  208. }}
  209. >
  210. NEXT_PUBLIC_
  211. </code>
  212. ,{' '}
  213. <code
  214. className="cursor-pointer"
  215. role="button"
  216. onClick={() => {
  217. field.onChange('VITE_PUBLIC_')
  218. }}
  219. >
  220. VITE_PUBLIC_
  221. </code>
  222. ,{' '}
  223. <code
  224. className="cursor-pointer"
  225. role="button"
  226. onClick={() => {
  227. field.onChange('PUBLIC_')
  228. }}
  229. >
  230. PUBLIC_
  231. </code>
  232. , etc.
  233. </FormDescription>
  234. <FormMessage className="col-start-5 col-span-8" />
  235. </FormItem>
  236. )}
  237. />
  238. </div>
  239. {form.formState.isDirty ? (
  240. <p className="mt-2 text-sm text-warning">
  241. Note: Changing these settings will <strong>not</strong> trigger a resync of
  242. environment variables.
  243. </p>
  244. ) : (
  245. <div className="mt-2 h-5 w-full" />
  246. )}
  247. <FormActions
  248. disabled={disabled}
  249. form={vercelConnectionFormId}
  250. hasChanges={form.formState.isDirty}
  251. isSubmitting={isPending}
  252. handleReset={() => form.reset()}
  253. />
  254. </div>
  255. </form>
  256. </Form>
  257. )
  258. }
  259. export default VercelIntegrationConnectionForm