import { createFileRoute, Link } from '@tanstack/react-router' import { useParams } from 'common' import { useCallback, useEffect, useState } from 'react' import { Button } from 'ui' import { Admonition } from 'ui-patterns/Admonition' import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader' import { isVercelUrl } from '@/components/interfaces/Integrations/Vercel/VercelIntegration.utils' import { VercelIntegrationFooter, VercelIntegrationLogo, } from '@/components/interfaces/Integrations/Vercel/VercelIntegrationInterstitial' import { ProjectCreationForm } from '@/components/interfaces/ProjectCreation/ProjectCreationForm' import { InterstitialLayout } from '@/components/layouts/InterstitialLayout' import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query' import { useIntegrationsQuery } from '@/data/integrations/integrations-query' import { useIntegrationVercelConnectionsCreateMutation } from '@/data/integrations/integrations-vercel-connections-create-mutation' import { useVercelProjectsQuery } from '@/data/integrations/integrations-vercel-projects-query' import { withAuth } from '@/hooks/misc/withAuth' import { buildStudioPageTitle } from '@/lib/page-title' import { useIntegrationInstallationSnapshot } from '@/state/integration-installation' export const Route = createFileRoute('/integrations/vercel/$slug/deploy-button/new-project')({ component: VercelDeployButtonNewProjectRoute, }) function VercelDeployButtonNewProjectRoute() { return } const PAGE_TITLE = buildStudioPageTitle({ section: 'Create Vercel Project', brand: 'Supabase', }) function VercelIntegration() { const { slug, next, currentProjectId: foreignProjectId } = useParams() const snapshot = useIntegrationInstallationSnapshot() const [newProjectRef, setNewProjectRef] = useState() const [connectionError, setConnectionError] = useState() const [isConnecting, setIsConnecting] = useState(false) const [isConnectionComplete, setIsConnectionComplete] = useState(false) const { data: integrationData } = useIntegrationsQuery() const organizationIntegration = integrationData?.find((x) => x.organization.slug === slug) const { data: vercelProjects } = useVercelProjectsQuery( { organization_integration_id: organizationIntegration?.id }, { enabled: organizationIntegration !== undefined } ) // Wait for the new project to be created before creating the connection const { data, isSuccess, isError: isProjectSettingsError, error: projectSettingsError, refetch: refetchProjectSettings, } = useProjectSettingsV2Query( { projectRef: newProjectRef }, { enabled: newProjectRef !== undefined, // refetch until the project is created refetchInterval: (query) => { const data = query.state.data return ((data?.service_api_keys ?? []).length ?? 0) > 0 ? false : 1000 }, } ) const { mutateAsync: createConnections } = useIntegrationVercelConnectionsCreateMutation() const connectProject = useCallback(async () => { const isReady = (data?.service_api_keys ?? []).length > 0 if (!isReady || !organizationIntegration || !foreignProjectId || !newProjectRef) { return } const projectDetails = vercelProjects?.find((x) => x.id === foreignProjectId) setConnectionError(undefined) setIsConnecting(true) try { await createConnections({ organizationIntegrationId: organizationIntegration.id, connection: { foreign_project_id: foreignProjectId, supabase_project_ref: newProjectRef, integration_id: '0', metadata: { ...projectDetails, supabaseConfig: { projectEnvVars: { write: true, }, }, }, }, orgSlug: slug, }) snapshot.setLoading(false) setIsConnectionComplete(true) if (next && isVercelUrl(next)) window.location.href = next } catch (error) { const message = error instanceof Error ? error.message : 'An unknown error occurred' setConnectionError(message) } finally { setIsConnecting(false) } }, [ createConnections, data?.service_api_keys, foreignProjectId, newProjectRef, next, organizationIntegration, snapshot, slug, vercelProjects, ]) useEffect(() => { if (isSuccess && !connectionError && !isConnecting && !isConnectionComplete) { void connectProject() } }, [connectProject, connectionError, isConnecting, isConnectionComplete, isSuccess]) const renderContent = () => { if (newProjectRef === undefined) { return } if (isProjectSettingsError) { return ( void refetchProjectSettings()} /> ) } if (connectionError) { return ( void connectProject()} /> ) } if (isConnectionComplete) { return (
) } return (

{isConnecting ? 'Connecting your project to Vercel' : 'Waiting for your project to be ready'}

) } return ( <> {PAGE_TITLE} } title="Create a new project" description="Your project will have its own dedicated instance and full Postgres database. An API will be set up so you can easily interact with your new database." footer={} widthClassName="max-w-2xl" > {renderContent()} ) } function VercelConnectionError({ projectRef, message, onRetry, }: { projectRef: string message: string onRetry: () => void }) { return (
) } const VercelIntegrationWithAuth = withAuth(VercelIntegration)