Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

1.0 KB
Raw Blame
import { API_URL } from '~/lib/constants'
import { get } from '~/lib/fetchWrapper'
import { components } from 'api-types'
import { useEffect, useState } from 'react'

export type Organization = components['schemas']['OrganizationResponse_Output']

export function useOrganizations() {
  const [organizations, setOrganizations] = useState<Organization[]>([])
  const [isLoading, setIsLoading] = useState(true)

  useEffect(() => {
    let isMounted = true
    const controller = new AbortController()

    get(`${API_URL}/organizations`, { signal: controller.signal })
      .then((res) => {
        if (!res.ok) {
          throw res
        }

        return res
      })
      .then((res) => res.json())
      .then((data) => {
        if (isMounted) {
          setOrganizations(data)
          setIsLoading(false)
        }
      })
      .catch(() => {
        // eat all errors
        setIsLoading(false)
      })

    return () => {
      isMounted = false
      controller.abort()
    }
  }, [])

  return {
    organizations,
    isLoading,
  } as const
}