Skip to content

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

1.4 KB
Raw Blame
import { useParams } from 'common'
import { useRouter } from 'next/router'
import { useEffect } from 'react'

import { getInfrastructurePath } from '@/components/interfaces/Settings/Infrastructure/Infrastructure.utils'
import { DefaultLayout } from '@/components/layouts/DefaultLayout'
import SettingsLayout from '@/components/layouts/ProjectSettingsLayout/SettingsLayout'
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
import type { NextPageWithLayout } from '@/types'

const ProjectBillingUsage: NextPageWithLayout = () => {
  // This component is only used for redirects, as nextjs cant redirect based on hash
  const router = useRouter()
  const { ref } = useParams()
  const { data: organization } = useSelectedOrganizationQuery()

  const hash = router.asPath.split('#')[1]
  const route = router.route

  useEffect(() => {
    if (!organization) return

    let redirectUrl

    if (['cpu', 'ram', 'disk_io'].includes(hash)) {
      redirectUrl = `${getInfrastructurePath(ref)}#${hash}`
    } else {
      redirectUrl = `/org/${organization.slug}/usage?projectRef=${ref}`
    }

    router.push(redirectUrl)
  }, [hash, route, organization, ref, router])

  return null
}

ProjectBillingUsage.getLayout = (page) => (
  <DefaultLayout>
    <SettingsLayout title="Usage">{page}</SettingsLayout>
  </DefaultLayout>
)

export default ProjectBillingUsage