Files
supabase/apps/studio/routes/project/$ref/branches/index.tsx
Joshen Lim 98876d2e90 Joshenlim/fe 5167 refactor pagelayout for branching pages (#53717)
## Context

Just some housekeeping to move off old layout components - this PR
targets the branching pages, specifically

Branching
Branching -> Merge requests
Branching -> Merge

Just need to make sure that these pages still render fine
2026-11-23 12:43:20 +07:00

190 lines
6.7 KiB
TypeScript

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 (
<BranchesPageWrapper>
<BranchesPage />
</BranchesPageWrapper>
)
}
function BranchesPage() {
const navigate = useNavigate()
const { ref } = useParams()
const snap = useAppStateSnapshot()
const { data: project } = useSelectedProjectQuery()
const { data: selectedOrg } = useSelectedOrganizationQuery()
const [selectedBranchToDelete, setSelectedBranchToDelete] = useState<Branch>()
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 (
<>
<PageContainer>
<PageSection>
<PageSectionContent>
<div className="space-y-4">
{branchesGrant.status === 'denied' && (
<NoPermission resourceText="view this project's branches" />
)}
{branchesGrant.status === 'error' && (
<AlertError
error={branchesGrant.error}
subject="Failed to check your permissions to view branches"
/>
)}
{(branchesGrant.status === 'loading' || branchesGrant.status === 'granted') && (
<>
{isErrorConnections && (
<AlertError
error={connectionsError}
subject="Failed to retrieve GitHub integration connection"
/>
)}
{isErrorBranches && (
<AlertError
error={branchesError}
subject="Failed to retrieve preview branches"
/>
)}
<Overview
isGithubConnected={isGithubConnected}
isLoading={isLoading}
isSuccess={isSuccess}
repo={repo}
mainBranch={mainBranch}
previewBranches={previewBranches}
onSelectCreateBranch={() => snap.setShowCreateBranchModal(true)}
onSelectDeleteBranch={setSelectedBranchToDelete}
generateCreatePullRequestURL={generateCreatePullRequestURL}
/>
</>
)}
</div>
</PageSectionContent>
</PageSection>
</PageContainer>
<DeleteBranchModal
branch={selectedBranchToDelete}
open={!!selectedBranchToDelete}
onClose={() => 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 (
<>
<PageHeader>
<PageHeaderMeta>
<PageHeaderSummary>
<PageHeaderTitle>Branches</PageHeaderTitle>
<PageHeaderDescription>
Manage your database preview branches and deployments
</PageHeaderDescription>
</PageHeaderSummary>
<PageHeaderAside>
<DocsButton href={`${DOCS_URL}/guides/platform/branching`} />
<CreateBranchButton />
</PageHeaderAside>
</PageHeaderMeta>
</PageHeader>
{children}
</>
)
}