import Link from 'next/link' import { useEffect } from 'react' import { useForm } from 'react-hook-form' import { toast } from 'sonner' import { Badge, Button, Form, FormControl, FormField, Input, Sheet, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetSection, SheetTitle, } from 'ui' import { Admonition } from 'ui-patterns' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' 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 { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' import { DOCS_URL } from '@/lib/constants' interface AWSPrivateLinkFormProps { account?: AWSAccount open: boolean onOpenChange: (open: boolean) => void } interface FormValues { awsAccountId: string accountName: string } export const AWSPrivateLinkForm = ({ account, open, onOpenChange }: AWSPrivateLinkFormProps) => { const isNew = !account const { data: project } = useSelectedProjectQuery() const { mutate: createAccount, isPending } = useAWSAccountCreateMutation() const form = useForm({ defaultValues: { awsAccountId: account?.aws_account_id ?? '', accountName: account?.account_name ?? '', }, }) const title = account?.status === 'ASSOCIATION_ACCEPTED' ? 'This connection is active' : account?.status === 'READY' ? 'Connection is ready' : account?.status === 'CREATING' ? 'This account connection is being created' : account?.status === 'DELETING' ? 'This account is being deleted' : account?.status === 'ASSOCIATION_REQUEST_EXPIRED' ? 'Account acceptance request has expired' : account?.status === 'CREATION_FAILED' ? 'Failed to create account' : 'This account needs to be accepted by the AWS account owner.' const description = account?.status === 'ASSOCIATION_ACCEPTED' ? 'The resource share has been accepted by the AWS account owner and the connection is established.' : account?.status === 'READY' ? 'It may be waiting acceptance from the AWS account owner. Association requests are automatically deleted if not accepted within 12 hours.' : account?.status === 'ASSOCIATION_REQUEST_EXPIRED' ? 'Reconnect this account to initiate a new connection request' : account?.status === 'CREATION_FAILED' ? 'Reconnect this account to initiate a new connection request' : '' const onSubmit = (values: FormValues) => { if (!project) return if (isNew) { createAccount( { projectRef: project.ref, awsAccountId: values.awsAccountId, accountName: values.accountName, }, { onSuccess: () => { toast.success('Successfully added AWS account') onOpenChange(false) }, } ) } } // Reset form when account changes useEffect(() => { form.reset({ awsAccountId: account?.aws_account_id ?? '', accountName: account?.account_name ?? '', }) }, [account, form]) return ( {isNew ? 'Add AWS Account' : 'AWS Account Details'} Connect to your Briven project from your AWS VPC using AWS PrivateLink.{' '} Learn more
{!isNew && account && ( <> {title} {account.status === 'ASSOCIATION_ACCEPTED' ? 'Connected' : account.status === 'READY' ? 'Ready' : account.status === 'CREATING' ? 'Creating' : account.status === 'CREATION_FAILED' ? 'Failed' : account.status === 'ASSOCIATION_REQUEST_EXPIRED' ? 'Expired' : account.status === 'DELETING' ? 'Deleting' : 'Unknown'} } description={description} actions={ account.status === 'READY' && ( ) } /> )} ( { if (!isNew) { e.target.blur() } }} /> )} /> ( { if (!isNew) { e.target.blur() } }} /> )} /> {isNew && ( )}
) }