import { useQuery } from '@tanstack/react-query' import { createFileRoute, useNavigate } from '@tanstack/react-router' import { useParams } from 'common' import { partition } from 'lodash' import { useState, type PropsWithChildren } from 'react' import { PageContainer } from 'ui-patterns/PageContainer' import { PageHeader, PageHeaderAside, PageHeaderDescription, PageHeaderMeta, PageHeaderSummary, PageHeaderTitle, } from 'ui-patterns/PageHeader' import { PageSection, PageSectionContent } from 'ui-patterns/PageSection' import { CreateBranchButton } from '@/components/interfaces/BranchManagement/CreateBranchButton' import { DeleteBranchModal } from '@/components/interfaces/BranchManagement/DeleteBranchModal' import { Overview } from '@/components/interfaces/BranchManagement/Overview' import { AlertError } from '@/components/ui/AlertError' import { DocsButton } from '@/components/ui/DocsButton' import { NoPermission } from '@/components/ui/NoPermission' import { branchesQueryOptions, type Branch } from '@/data/branches/branches-query' import { useGitHubConnectionsQuery } from '@/data/integrations/github-connections-query' import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' import { DOCS_URL } from '@/lib/constants' import { useGrant } from '@/lib/grants' import { useTrack } from '@/lib/telemetry/track' import { useAppStateSnapshot } from '@/state/app-state' export const Route = createFileRoute('/project/$ref/branches/')({ component: BranchesIndexRoute, }) function BranchesIndexRoute() { return ( ) } function BranchesPage() { const navigate = useNavigate() const { ref } = useParams() const snap = useAppStateSnapshot() const { data: project } = useSelectedProjectQuery() const { data: selectedOrg } = useSelectedOrganizationQuery() const [selectedBranchToDelete, setSelectedBranchToDelete] = useState() const track = useTrack() const isBranch = project?.parent_project_ref !== undefined const projectRef = project !== undefined ? (isBranch ? project.parent_project_ref : ref) : undefined const branchesGrant = useGrant('GET /v1/projects/{ref}/branches', { projectRef }) const { data: connections, error: connectionsError, isPending: isLoadingConnections, isSuccess: isSuccessConnections, isError: isErrorConnections, } = useGitHubConnectionsQuery({ organizationId: selectedOrg?.id, }) const { data: branches, error: branchesError, isPending: isLoadingBranches, isError: isErrorBranches, isSuccess: isSuccessBranches, } = useQuery(branchesQueryOptions({ projectRef, grant: branchesGrant.grant })) const [[mainBranch], previewBranchesUnsorted] = partition(branches, (branch) => branch.is_default) const previewBranches = previewBranchesUnsorted.sort((a, b) => new Date(a.updated_at) < new Date(b.updated_at) ? 1 : -1 ) const githubConnection = connections?.find((connection) => connection.project.ref === projectRef) const isGithubConnected = githubConnection !== undefined const repo = githubConnection?.repository.name ?? '' const isLoading = isLoadingConnections || isLoadingBranches const isSuccess = isSuccessConnections && isSuccessBranches const generateCreatePullRequestURL = (branch?: string) => { if (githubConnection === undefined) return 'https://github.com' return branch !== undefined ? `https://github.com/${githubConnection.repository.name}/compare/${mainBranch?.git_branch}...${branch}` : `https://github.com/${githubConnection.repository.name}/compare` } return ( <>
{branchesGrant.status === 'denied' && ( )} {branchesGrant.status === 'error' && ( )} {(branchesGrant.status === 'loading' || branchesGrant.status === 'granted') && ( <> {isErrorConnections && ( )} {isErrorBranches && ( )} snap.setShowCreateBranchModal(true)} onSelectDeleteBranch={setSelectedBranchToDelete} generateCreatePullRequestURL={generateCreatePullRequestURL} /> )}
setSelectedBranchToDelete(undefined)} onSuccess={() => { if (selectedBranchToDelete?.project_ref === ref && projectRef) { void navigate({ to: '/project/$ref/branches', params: { ref: projectRef } }) } track( 'branch_delete_button_clicked', { branchType: selectedBranchToDelete?.persistent ? 'persistent' : 'preview', origin: 'branches_page', }, { project: projectRef } ) }} /> ) } const BranchesPageWrapper = ({ children }: PropsWithChildren) => { return ( <> Branches Manage your database preview branches and deployments {children} ) }