Files
supabase/apps/studio/components/interfaces/BranchManagement/Branch.Commands.tsx
Joshen Lim cab0585533 Fe 1799/consolidate to useselectedprojectquery and (#37684)
* Replace all usage of useProjectContext with useSelectedProjectQuery

* Replace all usage of useSelectedProject with useSelectedProjectQuery

* Replace all usage of useProjectByRef with useProjectByRefQuery

* Replace all usage of useSelectedOrganization with useSelectedOrganizationQuery

* Deprecate useSelectedProject, useSelectedOrganization, and useProjectByRef hooks

* Deprecate ProjecContext
2025-08-06 10:53:10 +07:00

64 lines
1.9 KiB
TypeScript

import { Forward, GitBranch } from 'lucide-react'
import { useBranchesQuery } from 'data/branches/branches-query'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
import { PageType, useRegisterCommands, useRegisterPage, useSetPage } from 'ui-patterns/CommandMenu'
import { COMMAND_MENU_SECTIONS } from '../App/CommandMenu/CommandMenu.utils'
import { orderCommandSectionsByPriority } from '../App/CommandMenu/ordering'
const SWITCH_BRANCH_PAGE_NAME = 'Switch branch'
const EMPTY_ARRAY = [] as Array<any>
export function useBranchCommands() {
const setPage = useSetPage()
const { data: selectedProject } = useSelectedProjectQuery()
const isBranchingEnabled = selectedProject?.is_branch_enabled === true
let { data: branches } = useBranchesQuery(
{
projectRef: selectedProject?.parent_project_ref || selectedProject?.ref,
},
{ enabled: isBranchingEnabled }
)
branches ??= EMPTY_ARRAY
useRegisterPage(
SWITCH_BRANCH_PAGE_NAME,
{
type: PageType.Commands,
sections: [
{
id: 'switch-branch',
name: 'Switch branch',
commands: branches.map((branch) => ({
id: `branch-${branch.id}`,
name: branch.name,
route: `/project/${branch.project_ref}`,
icon: () => <Forward />,
})),
},
],
},
{ enabled: isBranchingEnabled && branches.length > 0, deps: [branches] }
)
useRegisterCommands(
COMMAND_MENU_SECTIONS.ACTIONS,
[
{
id: 'switch-branch',
name: 'Switch branch',
value: 'Switch branch, Change branch, Select branch',
action: () => setPage(SWITCH_BRANCH_PAGE_NAME),
icon: () => <GitBranch />,
},
],
{
enabled: isBranchingEnabled && branches.length > 0,
orderSection: orderCommandSectionsByPriority,
sectionMeta: { priority: 3 },
}
)
}