Branch.Commands.tsx 1.9 KB

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