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