Files
supabase/apps/studio/components/interfaces/Auth/Hooks/AddHookDropdown.tsx
fffb1e23ca feat: implement profile permissions v2 api (#50966)
## Problem

Migrating Studio's permission checks from the old RLS json logic to
openfga backed permissions.

### New hooks and helpers

- `usePermissionsQueryV2` fetches the new profile openfga permisisons 
- `useAsyncCheckPermissionsV2` the replacement for the old check hook;
takes an FGA permission string (or a list)
- `FGA_PERMISSIONS` permission constants derived from
`@supabase/shared-types`. At the moment, the hook also accepts plain
strings for permissions not yet published in the package
- `useAsyncCheckUserContentPermissions` new hook for permission checks
on user content (SQL snippets, reports, log snippets). This wraps
``useAsyncCheckPermissionsV2` and adds existing content level rules on
top of the fga permission check

### Notes

- ~~Need api to support new permissions: project_operations_write,
billing_read/write, audit_logs_read~~ [DONE]
- ~~Still need to support: organization_config_read/write, auth hooks
and users permissions~~ [DONE]

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com>
2026-10-21 06:54:16 -04:00

131 lines
4.2 KiB
TypeScript

import { useParams } from 'common'
import { ChevronDownIcon } from 'lucide-react'
import { useMemo } from 'react'
import {
Button,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from 'ui'
import { Hook, HOOK_DEFINITION_TITLE, HOOKS_DEFINITIONS } from './hooks.constants'
import { extractMethod, isValidHook } from './hooks.utils'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { InlineLink } from '@/components/ui/InlineLink'
import { useAuthConfigQuery } from '@/data/auth/auth-config-query'
import { useCheckEntitlements } from '@/hooks/misc/useCheckEntitlements'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
interface AddHookDropdownProps {
buttonText?: string
align?: 'end' | 'center'
variant?: 'primary' | 'default'
open?: boolean
onOpenChange?: (open: boolean) => void
onSelectHook: (hook: HOOK_DEFINITION_TITLE) => void
}
export const AddHookDropdown = ({
buttonText = 'Add hook',
align = 'end',
variant = 'primary',
open,
onOpenChange,
onSelectHook,
}: AddHookDropdownProps) => {
const { ref: projectRef } = useParams()
const { data: organization } = useSelectedOrganizationQuery()
const { data: authConfig } = useAuthConfigQuery({ projectRef })
const { can: canUpdateAuthHook } = useAsyncCheckPermissionsV2(
FGA_PERMISSIONS.PROJECT.AUTH_HOOKS_WRITE
)
const { getEntitlementSetValues: getEntitledHookSet } = useCheckEntitlements('auth.hooks')
const entitledHookSet = getEntitledHookSet()
const { availableHooks, nonAvailableHooks } = useMemo(() => {
const allHooks: Hook[] = HOOKS_DEFINITIONS.map((definition) => ({
...definition,
enabled: authConfig?.[definition.enabledKey] || false,
method: extractMethod(
authConfig?.[definition.uriKey] || '',
authConfig?.[definition.secretsKey] || ''
),
}))
const availableHooks: Hook[] = allHooks.filter(
(h) => !isValidHook(h) && entitledHookSet.includes(h.entitlementKey)
)
const nonAvailableHooks: Hook[] = allHooks.filter(
(h) => !isValidHook(h) && !entitledHookSet.includes(h.entitlementKey)
)
return { availableHooks, nonAvailableHooks }
}, [entitledHookSet, authConfig])
if (!canUpdateAuthHook) {
return (
<ButtonTooltip
disabled
variant={variant}
tooltip={{
content: { side: 'bottom', text: 'You need additional permissions to add auth hooks' },
}}
>
{buttonText}
</ButtonTooltip>
)
}
return (
<DropdownMenu open={open} onOpenChange={onOpenChange}>
<DropdownMenuTrigger asChild>
<Button variant={variant} iconRight={<ChevronDownIcon />}>
{buttonText}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-76" align={align}>
<div>
{availableHooks.length === 0 && (
<DropdownMenuLabel className="text-foreground-light">
All available hooks have been added
</DropdownMenuLabel>
)}
{availableHooks.map((h) => (
<DropdownMenuItem key={h.title} onClick={() => onSelectHook(h.title)}>
{h.title}
</DropdownMenuItem>
))}
</div>
{nonAvailableHooks.length > 0 && (
<>
{availableHooks.length > 0 && <DropdownMenuSeparator />}
<DropdownMenuLabel className="grid gap-1 bg-surface-200">
<p className="text-foreground-light">Team or Enterprise Plan required</p>
<p className="text-foreground-lighter text-xs">
The following hooks are not available on{' '}
<InlineLink href={`/org/${organization?.slug ?? '_'}/billing`}>
your plan
</InlineLink>
.
</p>
</DropdownMenuLabel>
{nonAvailableHooks.map((h) => (
<DropdownMenuItem key={h.title} disabled={true}>
{h.title}
</DropdownMenuItem>
))}
</>
)}
</DropdownMenuContent>
</DropdownMenu>
)
}