Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: branches tabs use url state (#27085)

Alaister Younga82bf1311d3c
diff --git a/apps/studio/components/interfaces/BranchManagement/BranchManagement.tsx b/apps/studio/components/interfaces/BranchManagement/BranchManagement.tsx
index 5e90f0db918..9ea6f8a0a47 100644
--- a/apps/studio/components/interfaces/BranchManagement/BranchManagement.tsx
+++ b/apps/studio/components/interfaces/BranchManagement/BranchManagement.tsx
@@ -12,7 +12,7 @@ import { useBranchDeleteMutation } from 'data/branches/branch-delete-mutation'
import { useBranchesDisableMutation } from 'data/branches/branches-disable-mutation'
import { Branch, useBranchesQuery } from 'data/branches/branches-query'
import { useGitHubConnectionsQuery } from 'data/integrations/github-connections-query'
-import { useSelectedOrganization, useSelectedProject } from 'hooks'
+import { useSelectedOrganization, useSelectedProject, useUrlState } from 'hooks'
import { Button, IconExternalLink, IconGitHub } from 'ui'
import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
import TextConfirmModal from 'ui-patterns/Dialogs/TextConfirmModal'
@@ -25,6 +25,8 @@ import {
} from './EmptyStates'
import Overview from './Overview'
+type Tab = 'overview' | 'prs' | 'branches'
+
const BranchManagement = () => {
const router = useRouter()
const { ref } = useParams()
@@ -37,7 +39,10 @@ const BranchManagement = () => {
const projectRef =
project !== undefined ? (isBranch ? project.parent_project_ref : ref) : undefined
- const [view, setView] = useState<'overview' | 'prs' | 'branches'>('overview')
+ const [urlParams, setParams] = useUrlState<{ tab: Tab }>()
+ const tab = urlParams.tab ?? 'overview'
+ const setTab = (tab: Tab) => setParams({ tab })
+
const [showCreateBranch, setShowCreateBranch] = useState(false)
const [showDisableBranching, setShowDisableBranching] = useState(false)
const [selectedBranchToDelete, setSelectedBranchToDelete] = useState<Branch>()
@@ -146,27 +151,27 @@ const BranchManagement = () => {
<Button
type="default"
className={`rounded-r-none transition hover:opacity-90 ${
- view === 'overview' ? 'opacity-100' : 'opacity-60'
+ tab === 'overview' ? 'opacity-100' : 'opacity-60'
}`}
- onClick={() => setView('overview')}
+ onClick={() => setTab('overview')}
>
Overview
</Button>
<Button
type="default"
className={`rounded-none transition hover:opacity-90 ${
- view === 'prs' ? 'opacity-100' : 'opacity-60'
+ tab === 'prs' ? 'opacity-100' : 'opacity-60'
}`}
- onClick={() => setView('prs')}
+ onClick={() => setTab('prs')}
>
Pull requests
</Button>
<Button
type="default"
className={`rounded-l-none transition hover:opacity-90 ${
- view === 'branches' ? 'opacity-100' : 'opacity-60'
+ tab === 'branches' ? 'opacity-100' : 'opacity-60'
}`}
- onClick={() => setView('branches')}
+ onClick={() => setTab('branches')}
>
All branches
</Button>
@@ -237,26 +242,26 @@ const BranchManagement = () => {
</div>
)}
- {isErrorBranches && view === 'overview' && (
+ {isErrorBranches && tab === 'overview' && (
<AlertError error={branchesError} subject="Failed to retrieve preview branches" />
)}
{!isError && (
<>
- {view === 'overview' && (
+ {tab === 'overview' && (
<Overview
isLoading={isLoading}
isSuccess={isSuccess}
repo={repo}
mainBranch={mainBranch}
previewBranches={previewBranches}
- onViewAllBranches={() => setView('branches')}
+ onViewAllBranches={() => setTab('branches')}
onSelectCreateBranch={() => setShowCreateBranch(true)}
onSelectDeleteBranch={setSelectedBranchToDelete}
generateCreatePullRequestURL={generateCreatePullRequestURL}
/>
)}
- {view === 'prs' && (
+ {tab === 'prs' && (
<BranchManagementSection
header={`${branchesWithPRs.length} branches with pull requests found`}
>
@@ -280,7 +285,7 @@ const BranchManagement = () => {
)}
</BranchManagementSection>
)}
- {view === 'branches' && (
+ {tab === 'branches' && (
<BranchManagementSection header={`${previewBranches.length} branches found`}>
{isLoadingBranches && <BranchLoader />}
{isErrorBranches && (