useSelectedOrganization.ts 749 B

12345678910111213141516171819202122
  1. import { useIsLoggedIn, useParams } from 'common'
  2. import { useOrganizationsQuery } from '@/data/organizations/organizations-query'
  3. import { useProjectDetailQuery } from '@/data/projects/project-detail-query'
  4. export function useSelectedOrganizationQuery({ enabled = true } = {}) {
  5. const isLoggedIn = useIsLoggedIn()
  6. const { ref, slug } = useParams()
  7. const { data: selectedProject } = useProjectDetailQuery({ ref })
  8. return useOrganizationsQuery({
  9. enabled: isLoggedIn && enabled,
  10. select: (data) => {
  11. return data.find((org) => {
  12. if (slug !== undefined) return org.slug === slug
  13. if (selectedProject !== undefined) return org.id === selectedProject.organization_id
  14. return undefined
  15. })
  16. },
  17. })
  18. }