Files
supabase/studio/components/interfaces/Settings/Integrations/GithubIntegration/GithubSection.tsx
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

145 lines
5.3 KiB
TypeScript

import { useCallback } from 'react'
import { useParams } from 'common'
import { IntegrationConnectionItem } from 'components/interfaces/Integrations/IntegrationConnection'
import {
IntegrationConnectionHeader,
IntegrationInstallation,
} from 'components/interfaces/Integrations/IntegrationPanels'
import { Markdown } from 'components/interfaces/Markdown'
import {
ScaffoldContainer,
ScaffoldSection,
ScaffoldSectionContent,
ScaffoldSectionDetail,
} from 'components/layouts/Scaffold'
import { useIntegrationsGitHubInstalledConnectionDeleteMutation } from 'data/integrations/integrations-github-connection-delete-mutation'
import { useOrgIntegrationsQuery } from 'data/integrations/integrations-query-org-only'
import { IntegrationName, IntegrationProjectConnection } from 'data/integrations/integrations.types'
import { useSelectedOrganization, useStore } from 'hooks'
import { pluralize } from 'lib/helpers'
import { IntegrationImageHandler } from '../IntegrationsSettings'
import GitHubIntegrationConnectionForm from './GitHubIntegrationConnectionForm'
const GitHubTitle = `GitHub Connections`
const GitHubDetailsSection = `
Connect any of your GitHub repositories to a project.
`
const GitHubContentSectionTop = `
### How will GitHub connections work?
You will be able to connect a GitHub repository to a Supabase project.
The GitHub app will watch for changes in your repository such as file changes, branch changes as well as pull request activity.
These connections will be part of a GitHub workflow that is currently in development.
`
const GitHubSection = () => {
const { ui } = useStore()
const org = useSelectedOrganization()
const { data } = useOrgIntegrationsQuery({ orgSlug: org?.slug })
const { ref: projectRef } = useParams()
const githubIntegrations = data?.filter(
(integration) => integration.integration.name === 'GitHub'
)
const { mutate: deleteGitHubConnection } = useIntegrationsGitHubInstalledConnectionDeleteMutation(
{
onSuccess: () => {
ui.setNotification({
category: 'success',
message: 'Successfully deleted Github connection',
})
},
}
)
const onDeleteGitHubConnection = useCallback(
async (connection: IntegrationProjectConnection) => {
deleteGitHubConnection({
connectionId: connection.id,
integrationId: connection.organization_integration_id,
orgSlug: org?.slug,
})
},
[deleteGitHubConnection, org?.slug]
)
return (
<ScaffoldContainer>
<ScaffoldSection>
<ScaffoldSectionDetail title={GitHubTitle}>
<Markdown content={GitHubDetailsSection} />
<IntegrationImageHandler title="github" />
</ScaffoldSectionDetail>
<ScaffoldSectionContent>
<Markdown content={GitHubContentSectionTop} />
{githubIntegrations &&
githubIntegrations.length > 0 &&
githubIntegrations.map((integration, i) => {
const ConnectionHeaderTitle = `${integration.connections.length} project ${pluralize(
integration.connections.length,
'connection'
)} `
const connections = integration.connections.filter((connection) =>
projectRef ? connection.supabase_project_ref === projectRef : true
)
return (
<div key={integration.id}>
<IntegrationInstallation title={'GitHub'} integration={integration} />
{connections.length > 0 ? (
<>
<IntegrationConnectionHeader
// title={ConnectionHeaderTitle}
// markdown={`Repository connections for GitHub`}
/>
<ul className="flex flex-col">
{connections.map((connection) => (
<div
key={connection.id}
className="relative flex flex-col -gap-[1px] [&>li]:pb-0"
>
<IntegrationConnectionItem
showNode={false}
key={connection.id}
connection={connection}
type={'GitHub' as IntegrationName}
onDeleteConnection={onDeleteGitHubConnection}
className="!rounded-b-none !mb-0"
/>
<div className="border-b border-l border-r rounded-b-lg">
<GitHubIntegrationConnectionForm
connection={connection}
integration={integration}
/>
</div>
</div>
))}
</ul>
</>
) : (
<IntegrationConnectionHeader
markdown={`### ${connections.length} project ${pluralize(
connections.length,
'connection'
)} Repository connections for GitHub`}
/>
)}
</div>
)
})}
</ScaffoldSectionContent>
</ScaffoldSection>
</ScaffoldContainer>
)
}
export default GitHubSection