Files
supabase/apps/studio/components/interfaces/Organization/IntegrationSettings/SidePanelVercelProjectLinker.tsx
Joshen Lim 7f42765070 Joshen/fe 3983 no way to create a new project in vercel integration when (#48230)
## Context

For the Vercel integration flow (e.g "Deploy with Vercel" button on GH)
If an organization has no projects, there currently isn't a way to
create a project and connect it in the same session - users can only hit
"Skip".

This addresses that by directing users to the /deploy-button/new-project
route in this scenario

<img width="505" height="539" alt="image"
src="https://github.com/user-attachments/assets/6cc85030-42c7-4e58-b4b3-cb8ac0f5da9e"
/>

## Other changes involved
- Also separates `ProjectLinker` into smaller components - preference
for avoiding declaration of components within a component

## To test

I'm not sure if this can be tested on staging to be honest, but
otherwise we can give it a go on production after the changes are
through, as this doesn't change any existing logic to the usual "Connect
project" flow

I did try clicking the "Deploy with Vercel" button on a repo, and just
changing the URL to the staging URL at the Supabase step - seems to work

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Summary

* **UI Improvements**
* Streamlined the Vercel/GitHub project-linking step while keeping the
same create/connect/skip flow, including the searchable project picker,
branding/status indicators, and the feature-flagged “create new project”
option.
* On the Vercel choose-project step, the default selection now reflects
the current project context.

* **Bug Fixes / Tests**
* Improved Vercel install routing query handling to preserve
deploy-button configuration when present, with updated automated test
coverage.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Danny White <3104761+dnywh@users.noreply.github.com>
2026-07-24 11:22:57 +08:00

160 lines
5.4 KiB
TypeScript

import { keyBy } from 'lodash'
import { useCallback, useMemo } from 'react'
import { toast } from 'sonner'
import { SidePanel } from 'ui'
import { type ForeignProject } from '../../Integrations/VercelGithub/VercelGithub.types'
import { ENV_VAR_RAW_KEYS } from '@/components/interfaces/Integrations/Vercel/Integrations-Vercel.constants'
import { ProjectLinker } from '@/components/interfaces/Integrations/VercelGithub/ProjectLinker'
import { Markdown } from '@/components/interfaces/Markdown'
import { vercelIcon } from '@/components/to-be-cleaned/ListIcons'
import { useOrgIntegrationsQuery } from '@/data/integrations/integrations-query-org-only'
import { useIntegrationVercelConnectionsCreateMutation } from '@/data/integrations/integrations-vercel-connections-create-mutation'
import { useVercelProjectsQuery } from '@/data/integrations/integrations-vercel-projects-query'
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { BASE_PATH } from '@/lib/constants'
import { EMPTY_ARR } from '@/lib/void'
import { useSidePanelsStateSnapshot } from '@/state/side-panels'
const VERCEL_ICON = (
<svg xmlns="http://www.w3.org/2000/svg" fill="white" viewBox="0 0 512 512" className="w-6">
<path fillRule="evenodd" d="M256,48,496,464H16Z" />
</svg>
)
export const SidePanelVercelProjectLinker = () => {
const { data: selectedProject } = useSelectedProjectQuery()
const { data: selectedOrganization } = useSelectedOrganizationQuery()
const sidePanelStateSnapshot = useSidePanelsStateSnapshot()
const organizationIntegrationId = sidePanelStateSnapshot.vercelConnectionsIntegrationId
const { data: integrationData } = useOrgIntegrationsQuery({
orgSlug: selectedOrganization?.slug,
})
const vercelIntegrations = integrationData?.filter(
(integration) => integration.integration.name === 'Vercel'
) // vercel
/**
* Find the right integration
*
* we use the snapshot.organizationIntegrationId which should be set whenever this sidepanel is opened
*/
const selectedIntegration = vercelIntegrations?.find((x) => x.id === organizationIntegrationId)
const { data: vercelProjectsData } = useVercelProjectsQuery(
{
organization_integration_id: organizationIntegrationId,
},
{ enabled: organizationIntegrationId !== undefined }
)
const vercelProjects = useMemo(() => vercelProjectsData ?? EMPTY_ARR, [vercelProjectsData])
const vercelProjectsById = useMemo(() => keyBy(vercelProjects, 'id'), [vercelProjects])
const getForeignProjectIcon = useCallback(
(_project: ForeignProject) => {
const project = vercelProjectsById[_project.id]
return !project?.framework ? (
vercelIcon
) : (
<img
src={`${BASE_PATH}/img/icons/frameworks/${project.framework}.svg`}
width={21}
height={21}
alt={`icon`}
/>
)
},
[vercelProjectsById]
)
const { mutate: createConnections, isPending: isCreatingConnection } =
useIntegrationVercelConnectionsCreateMutation({
async onSuccess({ env_sync_error: envSyncError }) {
if (envSyncError) {
toast.error(
`Failed to sync environment variables: ${envSyncError.message}. Please try re-syncing manually from settings.`
)
}
sidePanelStateSnapshot.setVercelConnectionsOpen(false)
},
})
const onCreateConnections = useCallback(
(vars: any) => {
createConnections({
...vars,
connection: {
...vars.connection,
metadata: {
...vars.connection.metadata,
supabaseConfig: {
projectEnvVars: {
write: true,
},
},
},
},
})
},
[createConnections]
)
return (
<SidePanel
hideFooter
size="large"
header="Add new Vercel project connection"
visible={sidePanelStateSnapshot.vercelConnectionsOpen}
onCancel={() => sidePanelStateSnapshot.setVercelConnectionsOpen(false)}
>
<div className="py-6 flex flex-col gap-6 bg-studio h-full">
<SidePanel.Content>
<Markdown
content={`
### Choose repository to connect to
Check the details below before proceeding
`}
/>
</SidePanel.Content>
<SidePanel.Content className="flex flex-col gap-2">
<ProjectLinker
slug={selectedOrganization?.slug}
defaultSupabaseProject={selectedProject}
organizationIntegrationId={selectedIntegration?.id}
foreignProjects={vercelProjects}
onCreateConnections={onCreateConnections}
installedConnections={selectedIntegration?.connections}
isLoading={isCreatingConnection}
integrationIcon={VERCEL_ICON}
getForeignProjectIcon={getForeignProjectIcon}
choosePrompt="Choose Vercel Project"
mode="Vercel"
/>
<Markdown
content={`
The following environment variables will be added:
${ENV_VAR_RAW_KEYS.map((x) => {
return `\n - \`${x}\``
})}
`}
/>
</SidePanel.Content>
<SidePanel.Content>
<ul>
<li className="border px-10">
{/* <IntegrationConnectionOption connection={githubIntegrations[0]} /> */}
</li>
</ul>
</SidePanel.Content>
</div>
</SidePanel>
)
}