import { useQuery } from '@tanstack/react-query' import { useParams } from 'common' import { Loader2Icon } from 'lucide-react' import { useState } from 'react' import { toast } from 'sonner' import { cn } from 'ui' import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal' import { EmptyStatePresentational } from 'ui-patterns/EmptyStatePresentational' import { PageSection, PageSectionAside, PageSectionContent, PageSectionDescription, PageSectionMeta, PageSectionSummary, PageSectionTitle, } from 'ui-patterns/PageSection' import { AddIntegrationDropdown } from './AddIntegrationDropdown' import { CreateAuth0IntegrationDialog } from './CreateAuth0Dialog' import { CreateAwsCognitoAuthIntegrationDialog } from './CreateAwsCognitoAuthDialog' import { CreateClerkAuthIntegrationDialog } from './CreateClerkAuthDialog' import { CreateCustomOidcIntegrationDialog } from './CreateCustomOidcDialog' import { CreateFirebaseAuthIntegrationDialog } from './CreateFirebaseAuthDialog' import { CreateWorkOSIntegrationDialog } from './CreateWorkOSDialog' import { IntegrationCard } from './IntegrationCard' import { getIntegrationType, getIntegrationTypeLabel, INTEGRATION_TYPES, } from './ThirdPartyAuthForm.utils' import { AlertError } from '@/components/ui/AlertError' import { DocsButton } from '@/components/ui/DocsButton' import { InlineLink } from '@/components/ui/InlineLink' import { NoPermission } from '@/components/ui/NoPermission' import { useDeleteThirdPartyAuthIntegrationMutation } from '@/data/third-party-auth/integration-delete-mutation' import { ThirdPartyAuthIntegration, thirdPartyAuthIntegrationsQueryOptions, } from '@/data/third-party-auth/integrations-query' import { DOCS_URL } from '@/lib/constants' import { getWriteGrantToastMessage, getWriteGrantTooltip, useGrant } from '@/lib/grants' import { SHORTCUT_IDS } from '@/state/shortcuts/registry' import { useShortcut } from '@/state/shortcuts/useShortcut' export const ThirdPartyAuthForm = () => { const { ref: projectRef } = useParams() const integrationsGrant = useGrant('GET /v1/projects/{ref}/config/auth/third-party-auth', { projectRef, }) const deleteGrant = useGrant('DELETE /v1/projects/{ref}/config/auth/third-party-auth/{tpa_id}', { projectRef, }) const { data: integrationsData, isPending: isLoadingIntegrations, isError, isSuccess, error, } = useQuery( thirdPartyAuthIntegrationsQueryOptions({ projectRef, grant: integrationsGrant.grant }) ) const isLoading = integrationsGrant.status === 'loading' || isLoadingIntegrations const integrations = integrationsData || [] const [selectedIntegration, setSelectedIntegration] = useState() const [selectedIntegrationForDeletion, setSelectedIntegrationForDeletion] = useState() const [addIntegrationOpen, setAddIntegrationOpen] = useState(false) useShortcut(SHORTCUT_IDS.LIST_PAGE_NEW_ITEM, () => setAddIntegrationOpen(true), { label: 'Add provider', }) const { mutateAsync: deleteIntegration } = useDeleteThirdPartyAuthIntegrationMutation() const canUpdateConfig = deleteGrant.status === 'granted' if (integrationsGrant.status === 'denied') { return } if (integrationsGrant.status === 'error') { return ( ) } if (isError) { return ( ) } return ( Third-Party Auth Billing is based on the number of monthly active users (MAUs) requesting your API throughout the billing period.{' '} Learn more {isLoading && (
)} {isSuccess ? ( integrations.length === 0 ? ( ) : (
{integrations.map((integration) => { return ( { setSelectedIntegrationForDeletion(integration) }} /> ) })}
) ) : null} {}} onClose={() => setSelectedIntegration(undefined)} /> {}} onClose={() => setSelectedIntegration(undefined)} /> {}} onClose={() => setSelectedIntegration(undefined)} /> {}} onClose={() => setSelectedIntegration(undefined)} /> {}} onClose={() => setSelectedIntegration(undefined)} /> setSelectedIntegration(undefined)} /> setSelectedIntegrationForDeletion(undefined)} onConfirm={async () => { if (!selectedIntegrationForDeletion) return if (!deleteGrant.grant) { toast.error( getWriteGrantToastMessage( deleteGrant.status, 'delete', 'third-party auth integrations' ) ) return } const type = getIntegrationType(selectedIntegrationForDeletion) try { await deleteIntegration({ projectRef: projectRef!, tpaId: selectedIntegrationForDeletion.id, grant: deleteGrant.grant, }) toast.success(`Successfully deleted ${getIntegrationTypeLabel(type)}.`) setSelectedIntegrationForDeletion(undefined) setSelectedIntegration(undefined) } catch (error) { toast.error(`Failed to delete ${getIntegrationTypeLabel(type)}.`) console.error(error) } }} >

Are you sure you want to delete the{' '} {getIntegrationTypeLabel(getIntegrationType(selectedIntegrationForDeletion))}{' '} integration?

) }