import { useMemo } from 'react' import { toast } from 'sonner' import ProjectLinker from 'components/interfaces/Integrations/ProjectLinker' import { Markdown } from 'components/interfaces/Markdown' import { useGitHubAuthorizationQuery } from 'data/integrations/github-authorization-query' import { useGitHubConnectionCreateMutation } from 'data/integrations/github-connection-create-mutation' import { useGitHubConnectionDeleteMutation } from 'data/integrations/github-connection-delete-mutation' import { useGitHubConnectionsQuery } from 'data/integrations/github-connections-query' import { useGitHubRepositoriesQuery } from 'data/integrations/github-repositories-query' import type { IntegrationConnectionsCreateVariables } from 'data/integrations/integrations.types' import { useProjectsQuery } from 'data/projects/projects-query' import { useSelectedOrganization } from 'hooks/misc/useSelectedOrganization' import { openInstallGitHubIntegrationWindow } from 'lib/github' import { EMPTY_ARR } from 'lib/void' import { useSidePanelsStateSnapshot } from 'state/side-panels' import { Button, SidePanel } from 'ui' const GITHUB_ICON = ( ) export type SidePanelGitHubRepoLinkerProps = { projectRef?: string } const SidePanelGitHubRepoLinker = ({ projectRef }: SidePanelGitHubRepoLinkerProps) => { const selectedOrganization = useSelectedOrganization() const sidePanelStateSnapshot = useSidePanelsStateSnapshot() const { data: gitHubAuthorization, isLoading: isLoadingGitHubAuthorization } = useGitHubAuthorizationQuery({ enabled: sidePanelStateSnapshot.githubConnectionsOpen }) // [Alaister]: temp override with until the typegen is fixed const { data: githubReposData, isLoading: isLoadingGitHubRepos } = useGitHubRepositoriesQuery< any[] >({ enabled: Boolean(gitHubAuthorization), }) /** * Supabase projects available */ const { data: supabaseProjectsData, isLoading: isLoadingSupabaseProjects } = useProjectsQuery() const supabaseProjects = useMemo( () => supabaseProjectsData ?.filter((project) => project.organization_id === selectedOrganization?.id) .map((project) => ({ name: project.name, ref: project.ref })) ?? EMPTY_ARR, [selectedOrganization?.id, supabaseProjectsData] ) const githubRepos = useMemo( () => githubReposData?.map((repo: any) => ({ id: repo.id.toString(), name: repo.name, installation_id: repo.installation_id, })) ?? EMPTY_ARR, [githubReposData] ) const { data: connections } = useGitHubConnectionsQuery({ organizationId: selectedOrganization?.id, }) const { mutate: createConnection, isLoading: isCreatingConnection } = useGitHubConnectionCreateMutation({ onSuccess() { toast.success('Successfully linked project to repository!') sidePanelStateSnapshot.setGithubConnectionsOpen(false) }, }) const { mutateAsync: deleteConnection } = useGitHubConnectionDeleteMutation() const createGithubConnection = async (variables: IntegrationConnectionsCreateVariables) => { if (!selectedOrganization?.id) { throw new Error('No organization id') } if (!variables.new) { throw new Error('No new connection') } const existingConnection = connections?.find( (connection) => connection.project.ref === projectRef ) if (existingConnection) { // remove existing connection so we can recreate it or update it try { await deleteConnection({ organizationId: selectedOrganization.id, connectionId: existingConnection.id, }) } catch (error) { // ignore the error to let createConnection handle it } } createConnection({ organizationId: selectedOrganization.id, connection: variables.new, }) } return ( sidePanelStateSnapshot.setGithubConnectionsOpen(false)} > {gitHubAuthorization === null ? ( Connect your Supabase projects with your GitHub repositories Authorize with GitHub to retrieve your GitHub repositories { openInstallGitHubIntegrationWindow('authorize') }} > Authorize GitHub ) : ( )} ) } export default SidePanelGitHubRepoLinker
Connect your Supabase projects with your GitHub repositories
Authorize with GitHub to retrieve your GitHub repositories