import { useNavigate } from '@tanstack/react-router'
import { FeatureFlagContext, useFlag, useParams } from 'common'
import { useContext, useEffect, useMemo, type PropsWithChildren } from 'react'
import { Badge } from 'ui'
import { ProjectLayout } from '../ProjectLayout'
import { NoPermission } from '@/components/ui/NoPermission'
import { ProductMenu } from '@/components/ui/ProductMenu'
import type { ProductMenuGroup } from '@/components/ui/ProductMenu/ProductMenu.types'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
import { useRoutePattern } from '@/hooks/misc/useRoutePattern'
import { withAuth } from '@/hooks/misc/withAuth'
import { PRODUCT_NAME } from '@/lib/constants/compute'
const useGenerateComputeMenu = (): ProductMenuGroup[] => {
const { ref: projectRef = 'default' } = useParams()
return useMemo(
() => [
{
title: 'Manage',
items: [
{
name: PRODUCT_NAME,
key: 'main',
pages: ['', '$name'],
url: `/project/${projectRef}/compute`,
items: [],
},
{
name: 'Secrets',
key: 'secrets',
url: `/project/${projectRef}/compute/secrets`,
items: [],
},
],
},
],
[projectRef]
)
}
export const ComputeProductMenu = () => {
const routePattern = useRoutePattern()
const page = routePattern.split('/')[4]
const menu = useGenerateComputeMenu()
return
}
interface ComputeLayoutProps {
title?: string
}
const ComputeLayoutContent = ({ children, title }: PropsWithChildren) => {
const navigate = useNavigate()
const { ref: projectRef } = useParams()
const { hasLoaded } = useContext(FeatureFlagContext)
const computeEnabled = useFlag('compute')
// The v2 compute routes require the FGA workers_read permission, which shared-types does not
// expose yet; they reuse the Edge Functions OAuth scope, so gate on the same product here.
const { isLoading: isLoadingPermissions, can: canReadCompute } = useAsyncCheckPermissionsV2(
FGA_PERMISSIONS.PROJECT.WORKERS_READ
)
useEffect(() => {
if (hasLoaded && !computeEnabled && projectRef) {
void navigate({ to: '/project/$ref', params: { ref: projectRef }, replace: true })
}
}, [navigate, hasLoaded, computeEnabled, projectRef])
if (!computeEnabled) return null
if (isLoadingPermissions) {
return (
)
}
return (
Private Alpha}
productMenu={}
isBlocking={false}
browserTitle={{ entity: PRODUCT_NAME, section: title }}
>
{canReadCompute ? (
children
) : (
)}
)
}
export const ComputeLayout = withAuth(ComputeLayoutContent)