import { createFileRoute, useNavigate } from '@tanstack/react-router' import { useParams } from 'common' import { useEffect, useMemo, useState } from 'react' import { Badge, Button, Card, CardContent, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from 'ui' import { Admonition } from 'ui-patterns/Admonition' import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader' import { VercelIntegrationFooter, VercelIntegrationInterstitialErrorState, VercelIntegrationLogo, } from '@/components/interfaces/Integrations/Vercel/VercelIntegrationInterstitial' import { getHasInstalledObject } from '@/components/layouts/IntegrationsLayout/Integrations.utils' import { InterstitialAccountRow, InterstitialActionError, InterstitialLayout, } from '@/components/layouts/InterstitialLayout' import { useIntegrationsQuery } from '@/data/integrations/integrations-query' import { useVercelIntegrationCreateMutation } from '@/data/integrations/vercel-integration-create-mutation' import { useOrganizationsQuery } from '@/data/organizations/organizations-query' import { withAuth } from '@/hooks/misc/withAuth' import { buildVercelInstallRouteQuery, getErrorMessage, resolveVercelInstallSource, } from '@/lib/integrations/vercel-install.utils' import { buildStudioPageTitle } from '@/lib/page-title' import { useProfileNameAndPicture } from '@/lib/profile' import { useTrack } from '@/lib/telemetry/track' import { useIntegrationInstallationSnapshot } from '@/state/integration-installation' import type { Organization } from '@/types' export const Route = createFileRoute('/integrations/vercel/install')({ component: VercelInstallRoute, }) function VercelInstallRoute() { return } const PAGE_TITLE = buildStudioPageTitle({ section: 'Install Vercel Integration', brand: 'Supabase', }) function VercelIntegration() { const track = useTrack() const navigate = useNavigate() const snapshot = useIntegrationInstallationSnapshot() const { code, configurationId, currentProjectId, externalId, next, teamId, source } = useParams() const [selectedOrg, setSelectedOrg] = useState(null) const [validationError, setValidationError] = useState() const { username, primaryEmail, avatarUrl } = useProfileNameAndPicture() const displayName = primaryEmail ?? username ?? '' /** * Fetch the list of organization based integration installations for Vercel. * * Array of integrations installed on all */ const { data: integrationData, isPending: isLoadingIntegrationsQuery, isError: isIntegrationsError, error: integrationsError, } = useIntegrationsQuery() const { data: organizationsData, isPending: isLoadingOrganizationsQuery, isSuccess: isOrganizationsDataSuccess, isError: isOrganizationsError, error: organizationsError, } = useOrganizationsQuery() /** * Organizations with extra `installationInstalled` attribute * * Used to show label/badge and allow/disallow installing * */ const installed = useMemo( () => integrationData && organizationsData ? getHasInstalledObject({ integrationName: 'Vercel', integrationData, organizationsData, installationId: configurationId, }) : {}, [configurationId, integrationData, organizationsData] ) /** * Handle the correct route change based on whether the vercel integration * is following the 'marketplace/external' flow or 'deploy button' flow. * See: * - https://vercel.com/docs/integrations/create-integration/submit-integration#query-parameters-for-marketplace * - https://vercel.com/docs/integrations/create-integration/submit-integration#query-parameters-for-external-flow * - https://vercel.com/docs/integrations/create-integration/submit-integration#query-parameters-for-deploy-button */ function handleRouteChange(orgSlug: string) { const vercelInstallSource = resolveVercelInstallSource({ source, currentProjectId, externalId, }) const query = buildVercelInstallRouteQuery({ source: vercelInstallSource, organizationSlug: orgSlug, configurationId, currentProjectId, externalId, next, }) switch (vercelInstallSource) { case 'deploy-button': { void navigate({ to: '/integrations/vercel/$slug/deploy-button/new-project', params: { slug: orgSlug }, search: query, }) break } case 'marketplace': case 'external': { void navigate({ to: '/integrations/vercel/$slug/marketplace/choose-project', params: { slug: orgSlug }, search: query, }) break } default: setValidationError( `Unsupported Vercel installation source: ${source}. Please contact support if this error persists.` ) } } const { mutate, isPending: isLoadingVercelIntegrationCreateMutation, error: createIntegrationError, reset: resetCreateIntegrationError, } = useVercelIntegrationCreateMutation({ onMutate() { snapshot.setLoading(true) }, onSuccess(_data, variables) { handleRouteChange(variables.orgSlug) snapshot.setLoading(false) }, onError() { snapshot.setLoading(false) }, }) const actionError = validationError ?? (createIntegrationError ? `Creating Vercel integration failed: ${createIntegrationError.message}` : undefined) function onInstall() { setValidationError(undefined) resetCreateIntegrationError() const orgSlug = selectedOrg?.slug const isIntegrationInstalled = orgSlug ? installed[orgSlug] : false if (!orgSlug) { return setValidationError('Please select an organization') } if (!code) { return setValidationError('Vercel code missing') } if (!configurationId) { return setValidationError('Vercel configuration ID missing') } if (!source) { return setValidationError('Vercel configuration source missing') } /** * Only install if integration hasn't already been installed */ if (!isIntegrationInstalled) { track( 'integration_install_submitted', { integrationName: 'Vercel', method: source }, { organization: orgSlug } ) mutate({ code, configurationId, orgSlug, metadata: {}, source, teamId: teamId, }) } else { handleRouteChange(orgSlug) } } const dataLoading = isLoadingVercelIntegrationCreateMutation || isLoadingOrganizationsQuery || isLoadingIntegrationsQuery const noOrganizations = useMemo(() => { return isOrganizationsDataSuccess && organizationsData?.length === 0 ? true : false }, [isOrganizationsDataSuccess, organizationsData]) const missingParams = [ !code ? 'code' : undefined, !configurationId ? 'configurationId' : undefined, !source ? 'source' : undefined, ].filter(Boolean) as string[] const isError = isOrganizationsError || isIntegrationsError const errorMessage = getErrorMessage(organizationsError) ?? getErrorMessage(integrationsError) const showLoadingState = isLoadingOrganizationsQuery || isLoadingIntegrationsQuery const disableInstallationForm = dataLoading || noOrganizations || !selectedOrg || missingParams.length > 0 || isError useEffect(() => { if (organizationsData !== undefined && integrationData !== undefined) { const firstOrg = organizationsData[0] if (firstOrg && selectedOrg === null) { setSelectedOrg(firstOrg) } } }, [organizationsData, integrationData, selectedOrg]) return ( <> {PAGE_TITLE} } title="Install Vercel integration" description="Choose the Supabase organization Vercel can connect to" footer={} >
{showLoadingState ? ( ) : isError ? ( ) : (
{ setSelectedOrg(organization) setValidationError(undefined) resetCreateIntegrationError() }} /> {missingParams.length > 0 && ( )} {noOrganizations && ( Create a Supabase organization before installing the Vercel integration. You can create a new organization{' '} here . } /> )}
)}
) } const InstallationLoadingState = () => (
) interface OrganizationSelectProps { organizations: Organization[] selectedOrg: Organization | null disabled?: boolean installed: Record onSelectedOrgChange: (organization: Organization) => void } function OrganizationSelect({ organizations, selectedOrg, disabled, installed, onSelectedOrgChange, }: OrganizationSelectProps) { return (

Organization

) } const VercelIntegrationWithAuth = withAuth(VercelIntegration)