OrgProjectSwitcher.tsx 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { IS_PLATFORM } from 'common'
  2. import { Building, Forward, Wrench } from 'lucide-react'
  3. import { useMemo } from 'react'
  4. import { PageType, useRegisterCommands, useRegisterPage, useSetPage } from 'ui-patterns/CommandMenu'
  5. import { COMMAND_MENU_SECTIONS } from './CommandMenu.utils'
  6. import { useOrganizationsQuery } from '@/data/organizations/organizations-query'
  7. import { useProjectsInfiniteQuery } from '@/data/projects/projects-infinite-query'
  8. const PROJECT_SWITCHER_PAGE_NAME = 'Switch project'
  9. const ORGANIZATION_SWITCHER_PAGE_NAME = 'Configure organization'
  10. export function useProjectSwitchCommand() {
  11. const setPage = useSetPage()
  12. // [Joshen] Using paginated data here which means we won't be showing all projects
  13. // Ideally we somehow support searching with Cmd K if we want to make this ideal
  14. // e.g Cmd K input to support async searching while in "switch project" state
  15. const { data } = useProjectsInfiniteQuery({}, { enabled: IS_PLATFORM })
  16. const projects = useMemo(() => data?.pages.flatMap((page) => page.projects), [data?.pages]) || []
  17. useRegisterPage(
  18. PROJECT_SWITCHER_PAGE_NAME,
  19. {
  20. type: PageType.Commands,
  21. sections: [
  22. {
  23. id: 'switch-project',
  24. name: 'Switch project',
  25. commands: projects.map(({ name, ref }) => ({
  26. id: `project-${ref}`,
  27. name,
  28. value: `${name} (${ref})`,
  29. route: `/project/${ref}`,
  30. icon: () => <Forward />,
  31. })),
  32. },
  33. ],
  34. },
  35. { deps: [projects], enabled: !!projects && projects.length > 0 }
  36. )
  37. useRegisterCommands(
  38. COMMAND_MENU_SECTIONS.ACTIONS,
  39. [
  40. {
  41. id: 'switch-project',
  42. name: 'Switch project...',
  43. value: 'Switch project, Change project, Select project',
  44. action: () => setPage(PROJECT_SWITCHER_PAGE_NAME),
  45. icon: () => <Wrench />,
  46. },
  47. ],
  48. { enabled: !!projects && projects.length > 0 }
  49. )
  50. }
  51. export function useConfigureOrganizationCommand() {
  52. const setPage = useSetPage()
  53. const { data: organizations } = useOrganizationsQuery({ enabled: IS_PLATFORM })
  54. useRegisterPage(
  55. ORGANIZATION_SWITCHER_PAGE_NAME,
  56. {
  57. type: PageType.Commands,
  58. sections: [
  59. {
  60. id: 'configure-organization',
  61. name: 'Configure organization',
  62. commands:
  63. organizations?.map(({ name, slug }) => ({
  64. id: `organization-${slug}`,
  65. name,
  66. value: `${name} (${slug})`,
  67. route: `/org/${slug}/general`,
  68. icon: () => <Building />,
  69. })) ?? [],
  70. },
  71. ],
  72. },
  73. { deps: [organizations], enabled: !!organizations && organizations.length > 0 }
  74. )
  75. useRegisterCommands(
  76. COMMAND_MENU_SECTIONS.ACTIONS,
  77. [
  78. {
  79. id: 'configure-organization',
  80. name: 'Configure organization...',
  81. value: 'Configure organization, Change organization, Select organization',
  82. action: () => setPage(ORGANIZATION_SWITCHER_PAGE_NAME),
  83. icon: () => <Building />,
  84. },
  85. ],
  86. { enabled: !!organizations && organizations.length > 0 }
  87. )
  88. }