Files
supabase/apps/studio/hooks/misc/useSelectedOrganization.ts
Ivan Vasilov 436bdb10ae chore: Move the studio app to apps/studio (#18915)
* Move all studio files from /studio to /apps/studio.

* Move studio specific prettier ignores.

* Fix the ui references from studio.

* Fix the css imports.

* Fix all package.json issues.

* Fix the prettier setup for the studio app.

* Add .turbo folder to prettierignore.

* Fix the github workflows.
2023-11-15 12:38:55 +01:00

31 lines
1.1 KiB
TypeScript

import { useIsLoggedIn, useParams } from 'common'
import { useOrganizationsQuery } from 'data/organizations/organizations-query'
import { useMemo } from 'react'
import { LOCAL_STORAGE_KEYS } from 'lib/constants'
import { useProjectByRef } from './useSelectedProject'
export function useSelectedOrganization({ enabled = true } = {}) {
const isLoggedIn = useIsLoggedIn()
const { ref, slug } = useParams()
const { data } = useOrganizationsQuery({ enabled: isLoggedIn && enabled })
const selectedProject = useProjectByRef(ref)
const localStorageSlug = useMemo(() => {
return typeof window !== 'undefined'
? localStorage.getItem(LOCAL_STORAGE_KEYS.RECENTLY_VISITED_ORGANIZATION)
: null
}, [])
return useMemo(() => {
return data?.find((org) => {
if (slug !== undefined) return org.slug === slug
if (selectedProject !== undefined) return org.id === selectedProject.organization_id
if (localStorageSlug !== undefined) return org.slug === localStorageSlug
return undefined
})
}, [data, selectedProject, slug, localStorageSlug])
}