import { useQuery } from '@tanstack/react-query' import { ForwardIcon, GitBranchIcon, PlusIcon } from 'lucide-react' import { PageType, useRegisterCommands, useRegisterPage, useSetPage } from 'ui-patterns/CommandMenu' import { COMMAND_MENU_SECTIONS } from '../App/CommandMenu/CommandMenu.utils' import { orderCommandSectionsByPriority } from '../App/CommandMenu/ordering' import { branchesQueryOptions } from '@/data/branches/branches-query' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' import { useGrant } from '@/lib/grants' import { useAppStateSnapshot } from '@/state/app-state' const SWITCH_BRANCH_PAGE_NAME = 'Switch branch' const EMPTY_ARRAY = [] as Array export function useBranchCommands() { const setPage = useSetPage() const { setShowCreateBranchModal } = useAppStateSnapshot() const { data: selectedProject } = useSelectedProjectQuery() const ref = selectedProject?.ref || '_' const isBranchingEnabled = selectedProject?.is_branch_enabled === true const parentProjectRef = selectedProject?.parent_project_ref || selectedProject?.ref const createBranchGrant = useGrant('POST /v1/projects/{ref}/branches', { projectRef: parentProjectRef, }) const canCreateBranches = createBranchGrant.status === 'granted' const branchesGrant = useGrant('GET /v1/projects/{ref}/branches', { projectRef: parentProjectRef, }) let { data: branches } = useQuery( branchesQueryOptions( { projectRef: parentProjectRef, grant: branchesGrant.grant }, { 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: () => , })), }, ], }, { 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: () => , }, ], { enabled: isBranchingEnabled && branches.length > 0, orderSection: orderCommandSectionsByPriority, sectionMeta: { priority: 3 }, } ) useRegisterCommands( COMMAND_MENU_SECTIONS.ACTIONS, [ { id: 'create-branch', name: 'Create branch', value: 'Create branch, New branch', action: () => setShowCreateBranchModal(true), icon: () => , }, ], { enabled: canCreateBranches, orderSection: orderCommandSectionsByPriority, sectionMeta: { priority: 3 }, } ) useRegisterCommands( COMMAND_MENU_SECTIONS.NAVIGATE, [ { id: 'nav-branch-management', name: 'Branch management', route: `/project/${ref}/branches`, }, { id: 'nav-branch-merge-requests', name: 'Merge requests', value: 'Branch', route: `/project/${ref}/branches/merge-requests`, }, ], { enabled: !!selectedProject } ) }