Files
supabase/studio/lib/github-integration.ts
T
Alaister YoungandJoshen Lim dec916bbb1 chore: better github installation flow (#18066)
* chore: better github installation flow

* Update studio/components/interfaces/Integrations/ProjectLinker.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* Update studio/components/interfaces/Integrations/ProjectLinker.tsx

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>

* remove organizationIntegrationId state

* open Install Github Integration in current window

* enabledBranching -> enableBranching

* adds loading state for ProjectLinker

* add new project connection opens linker

* filter connections by projectRef

* truncate github repo name

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2023-10-17 17:59:19 +11:00

75 lines
2.2 KiB
TypeScript

import { useParams } from 'common'
import { useGitHubIntegrationCreateMutation } from 'data/integrations/github-integration-create-mutation'
import { useOrganizationsQuery } from 'data/organizations/organizations-query'
import { useProjectsQuery } from 'data/projects/projects-query'
import { useEffect, useState } from 'react'
export function useGitHubIntegrationAutoInstall(callback?: (id: string, orgSlug: string) => void) {
const { installation_id: installationId, state: projectRef } = useParams()
const shouldAttemptInstall = projectRef !== undefined && installationId !== undefined
const [isAutoInstalling, setIsAutoInstalling] = useState(false)
const { data: projects, isSuccess: isProjectsSuccess } = useProjectsQuery({
enabled: shouldAttemptInstall,
})
const { data: organizations, isSuccess: isOrganizationsSuccess } = useOrganizationsQuery({
enabled: shouldAttemptInstall,
})
const { mutate: createIntegration } = useGitHubIntegrationCreateMutation()
useEffect(() => {
async function autoInstall() {
if (shouldAttemptInstall) {
setIsAutoInstalling(true)
}
if (projectRef && isProjectsSuccess && isOrganizationsSuccess) {
const project = projects.find((x) => x.ref === projectRef)
const organization = organizations.find((x) => x.id === project?.organization_id)
if (project && organization) {
createIntegration(
{
installationId: Number(installationId),
orgSlug: organization.slug,
metadata: {
supabaseConfig: {
supabaseDirectory: '/supabase',
},
},
},
{
onSuccess({ id }) {
setIsAutoInstalling(false)
callback?.(id, organization.slug)
},
onError() {
setIsAutoInstalling(false)
},
}
)
} else {
setIsAutoInstalling(false)
}
}
}
autoInstall()
}, [
callback,
createIntegration,
installationId,
isOrganizationsSuccess,
isProjectsSuccess,
organizations,
projectRef,
projects,
shouldAttemptInstall,
])
return isAutoInstalling
}