Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

9.9 KB
Raw Blame
import { zodResolver } from '@hookform/resolvers/zod'
import { useFlag } from 'common'
import { useForm } from 'react-hook-form'
import { toast } from 'sonner'
import {
  Button,
  Form,
  FormControl,
  FormField,
  Input,
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
  Sheet,
  SheetContent,
  SheetDescription,
  SheetFooter,
  SheetHeader,
  SheetSection,
  SheetTitle,
} from 'ui'
import { Input as CopyableInput } from 'ui-patterns/DataInputs/Input'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { z } from 'zod'

import { AWSPrivateLinkAttentionAdmonition } from './AWSPrivateLinkAttentionAdmonition'
import { InlineLink } from '@/components/ui/InlineLink'
import { useAWSAccountCreateMutation } from '@/data/aws-accounts/aws-account-create-mutation'
import type { AWSAccount } from '@/data/aws-accounts/aws-accounts-query'
import { useReadReplicasQuery } from '@/data/read-replicas/replicas-query'
import { formatDatabaseID, formatDatabaseRegion } from '@/data/read-replicas/replicas.utils'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { DOCS_URL } from '@/lib/constants'

const FORM_ID = 'privatelink-connection-form'

const FormSchema = z.object({
  awsAccountId: z
    .string()
    .trim()
    .regex(/^\d{12}$/, 'Enter a 12-digit AWS account ID'),
  databaseIdentifier: z.string().min(1, 'Select a database'),
  accountName: z.string(),
})

type FormValues = z.infer<typeof FormSchema>

interface AWSPrivateLinkFormProps {
  account?: AWSAccount
  open: boolean
  onOpenChange: (open: boolean) => void
  onDelete?: () => void
}

export const AWSPrivateLinkForm = ({
  account,
  open,
  onOpenChange,
  onDelete,
}: AWSPrivateLinkFormProps) => {
  const isNew = !account
  const { data: project } = useSelectedProjectQuery()
  const showPrivateLinkReadReplica = useFlag('privatelinkReadReplica')
  const shouldLoadReadReplicas =
    !!project?.ref &&
    (isNew ? showPrivateLinkReadReplica : account?.database_type === 'READ_REPLICA')
  const { data: databases = [] } = useReadReplicasQuery(
    { projectRef: project?.ref },
    { enabled: shouldLoadReadReplicas }
  )
  const { mutate: createAccount, isPending } = useAWSAccountCreateMutation()

  const readReplicas = databases.filter((database) => database.identifier !== project?.ref)
  const showDatabaseTarget = showPrivateLinkReadReplica || !isNew
  const formValues: FormValues = {
    awsAccountId: account?.aws_account_id ?? '',
    databaseIdentifier: account?.database_identifier ?? project?.ref ?? '',
    accountName: account?.account_name ?? '',
  }

  const form = useForm<FormValues>({
    resolver: zodResolver(FormSchema),
    defaultValues: {
      awsAccountId: '',
      databaseIdentifier: '',
      accountName: '',
    },
    values: formValues,
    resetOptions: { keepDirtyValues: true },
  })

  const onSubmit = (values: FormValues) => {
    if (!project) return
    if (isNew) {
      createAccount(
        {
          projectRef: project.ref,
          awsAccountId: values.awsAccountId,
          accountName: values.accountName.trim() || undefined,
          databaseIdentifier:
            values.databaseIdentifier && values.databaseIdentifier !== project.ref
              ? values.databaseIdentifier
              : undefined,
        },
        {
          onSuccess: () => {
            form.reset(formValues)
            toast.success('Connection added')
            onOpenChange(false)
          },
        }
      )
    }
  }

  const handleOpenChange = (nextOpen: boolean) => {
    if (!nextOpen) form.reset(formValues)
    onOpenChange(nextOpen)
  }

  return (
    <Sheet open={open} onOpenChange={handleOpenChange}>
      <SheetContent className="flex flex-col gap-0">
        <SheetHeader>
          <SheetTitle>{isNew ? 'Add connection' : 'Connection details'}</SheetTitle>
          <SheetDescription>
            {isNew
              ? 'Enter an AWS account to connect. You’ll need to accept the share in AWS. '
              : 'These values identify the resource share in AWS. '}
            <InlineLink href={`${DOCS_URL}/guides/platform/privatelink`}>Learn more</InlineLink>
          </SheetDescription>
        </SheetHeader>
        <Form {...form}>
          <form
            id={FORM_ID}
            onSubmit={form.handleSubmit(onSubmit)}
            className="flex flex-col flex-1 min-h-0"
          >
            <SheetSection className="space-y-4 flex-1 overflow-y-auto">
              {!isNew && account && <AWSPrivateLinkAttentionAdmonition accounts={[account]} />}
              <FormField
                control={form.control}
                name="awsAccountId"
                render={({ field }) => (
                  <FormItemLayout
                    label="AWS account ID"
                    description="12-digit ID of the destination account."
                  >
                    <FormControl>
                      <Input
                        {...field}
                        readOnly={!isNew}
                        autoFocus={isNew}
                        placeholder="123456789012"
                        onFocus={(e) => {
                          if (!isNew) e.target.blur()
                        }}
                      />
                    </FormControl>
                  </FormItemLayout>
                )}
              />
              {showDatabaseTarget && (
                <FormField
                  control={form.control}
                  name="databaseIdentifier"
                  render={({ field }) => (
                    <FormItemLayout
                      label="Database"
                      description="Each database needs its own connection."
                    >
                      <FormControl>
                        <Select
                          value={field.value}
                          disabled={!isNew || !project?.ref}
                          onValueChange={field.onChange}
                        >
                          <SelectTrigger>
                            <SelectValue placeholder="Select a database" />
                          </SelectTrigger>
                          <SelectContent>
                            {project?.ref && (
                              <SelectItem value={project.ref}>Primary database</SelectItem>
                            )}
                            {readReplicas.map((database) => {
                              const region =
                                formatDatabaseRegion(database.region) ?? database.region
                              const id = formatDatabaseID(database.identifier)

                              return (
                                <SelectItem key={database.identifier} value={database.identifier}>
                                  {`Read replica (${region} - ${id})`}
                                </SelectItem>
                              )
                            })}
                          </SelectContent>
                        </Select>
                      </FormControl>
                    </FormItemLayout>
                  )}
                />
              )}
              <FormField
                control={form.control}
                name="accountName"
                render={({ field }) => (
                  <FormItemLayout
                    label="Name"
                    labelOptional="Optional"
                    description="Shown on the connections list. Defaults to the AWS account ID if left blank."
                  >
                    <FormControl>
                      <Input
                        {...field}
                        readOnly={!isNew}
                        placeholder="Production VPC"
                        onFocus={(e) => {
                          if (!isNew) e.target.blur()
                        }}
                      />
                    </FormControl>
                  </FormItemLayout>
                )}
              />
              {!isNew && account?.resource_access_manager_resource_config_id && (
                <FormItemLayout label="Resource configuration ID">
                  <CopyableInput
                    readOnly
                    copy
                    value={account.resource_access_manager_resource_config_id}
                  />
                </FormItemLayout>
              )}
              {!isNew && account?.resource_access_manager_resource_config_arn && (
                <FormItemLayout label="Resource configuration ARN">
                  <CopyableInput
                    readOnly
                    copy
                    value={account.resource_access_manager_resource_config_arn}
                  />
                </FormItemLayout>
              )}
              {!isNew && account?.resource_access_manager_share_arn && (
                <FormItemLayout label="Resource share ARN">
                  <CopyableInput readOnly copy value={account.resource_access_manager_share_arn} />
                </FormItemLayout>
              )}
            </SheetSection>

            <SheetFooter className={!isNew ? 'sm:justify-between' : undefined}>
              {!isNew ? (
                <>
                  <Button type="button" variant="danger" onClick={onDelete}>
                    Delete
                  </Button>
                  <Button type="button" onClick={() => handleOpenChange(false)}>
                    Close
                  </Button>
                </>
              ) : (
                <>
                  <Button
                    type="button"
                    disabled={isPending}
                    onClick={() => handleOpenChange(false)}
                  >
                    Cancel
                  </Button>
                  <Button variant="primary" form={FORM_ID} type="submit" loading={isPending}>
                    Add connection
                  </Button>
                </>
              )}
            </SheetFooter>
          </form>
        </Form>
      </SheetContent>
    </Sheet>
  )
}