import { useNavigate } from '@tanstack/react-router' import { useParams } from 'common' import { Button, GithubIcon } from 'ui' import { useGitHubAuthorizationQuery } from '@/data/integrations/github-authorization-query' import { useGitHubConnectionsQuery } from '@/data/integrations/github-connections-query' import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' import { openInstallGitHubIntegrationWindow } from '@/lib/github' import { navigateToUrl } from '@/lib/navigation' import { useAppStateSnapshot } from '@/state/app-state' export const ConnectToGitHub = () => { const navigate = useNavigate() const { ref } = useParams() const { data: project } = useSelectedProjectQuery() const { data: selectedOrg } = useSelectedOrganizationQuery() const { showCreateBranchModal, setShowCreateBranchModal } = useAppStateSnapshot() const isBranch = project?.parent_project_ref !== undefined const projectRef = project !== undefined ? (isBranch ? project.parent_project_ref : ref) : undefined const { data: gitHubAuthorization } = useGitHubAuthorizationQuery() const { data: connections } = useGitHubConnectionsQuery( { organizationId: selectedOrg?.id }, { enabled: showCreateBranchModal } ) const githubConnection = connections?.find((connection) => connection.project.ref === projectRef) const showAuthorizeCta = githubConnection && !gitHubAuthorization const onClick = () => { if (showAuthorizeCta) { openInstallGitHubIntegrationWindow('authorize') } else { if (showCreateBranchModal) setShowCreateBranchModal(false) void navigateToUrl(navigate, `/project/${projectRef}/settings/integrations`) } } return (
Keep this preview branch in sync with a chosen GitHub branch