Branch.Commands.tsx63 lines · main
| 1 | import { Forward, GitBranch } from 'lucide-react' |
| 2 | import { PageType, useRegisterCommands, useRegisterPage, useSetPage } from 'ui-patterns/CommandMenu' |
| 3 | |
| 4 | import { COMMAND_MENU_SECTIONS } from '../App/CommandMenu/CommandMenu.utils' |
| 5 | import { orderCommandSectionsByPriority } from '../App/CommandMenu/ordering' |
| 6 | import { useBranchesQuery } from '@/data/branches/branches-query' |
| 7 | import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' |
| 8 | |
| 9 | const SWITCH_BRANCH_PAGE_NAME = 'Switch branch' |
| 10 | const EMPTY_ARRAY = [] as Array<any> |
| 11 | |
| 12 | export function useBranchCommands() { |
| 13 | const setPage = useSetPage() |
| 14 | |
| 15 | const { data: selectedProject } = useSelectedProjectQuery() |
| 16 | const isBranchingEnabled = selectedProject?.is_branch_enabled === true |
| 17 | |
| 18 | let { data: branches } = useBranchesQuery( |
| 19 | { |
| 20 | projectRef: selectedProject?.parent_project_ref || selectedProject?.ref, |
| 21 | }, |
| 22 | { enabled: isBranchingEnabled } |
| 23 | ) |
| 24 | branches ??= EMPTY_ARRAY |
| 25 | |
| 26 | useRegisterPage( |
| 27 | SWITCH_BRANCH_PAGE_NAME, |
| 28 | { |
| 29 | type: PageType.Commands, |
| 30 | sections: [ |
| 31 | { |
| 32 | id: 'switch-branch', |
| 33 | name: 'Switch branch', |
| 34 | commands: branches.map((branch) => ({ |
| 35 | id: `branch-${branch.id}`, |
| 36 | name: branch.name, |
| 37 | route: `/project/${branch.project_ref}`, |
| 38 | icon: () => <Forward />, |
| 39 | })), |
| 40 | }, |
| 41 | ], |
| 42 | }, |
| 43 | { enabled: isBranchingEnabled && branches.length > 0, deps: [branches] } |
| 44 | ) |
| 45 | |
| 46 | useRegisterCommands( |
| 47 | COMMAND_MENU_SECTIONS.ACTIONS, |
| 48 | [ |
| 49 | { |
| 50 | id: 'switch-branch', |
| 51 | name: 'Switch branch', |
| 52 | value: 'Switch branch, Change branch, Select branch', |
| 53 | action: () => setPage(SWITCH_BRANCH_PAGE_NAME), |
| 54 | icon: () => <GitBranch />, |
| 55 | }, |
| 56 | ], |
| 57 | { |
| 58 | enabled: isBranchingEnabled && branches.length > 0, |
| 59 | orderSection: orderCommandSectionsByPriority, |
| 60 | sectionMeta: { priority: 3 }, |
| 61 | } |
| 62 | ) |
| 63 | } |