import { PermissionAction } from '@supabase/shared-types/out/constants' import { ChevronDown } from 'lucide-react' import { useParams } from 'common' import { ButtonTooltip } from 'components/ui/ButtonTooltip' import { useAuthConfigQuery } from 'data/auth/auth-config-query' import { useOrgSubscriptionQuery } from 'data/subscriptions/org-subscription-query' import { useCheckPermissions } from 'hooks/misc/useCheckPermissions' import { useSelectedOrganization } from 'hooks/misc/useSelectedOrganization' import { IS_PLATFORM } from 'lib/constants' import { Button, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from 'ui' import { HOOKS_DEFINITIONS, HOOK_DEFINITION_TITLE, Hook } from './hooks.constants' import { extractMethod, isValidHook } from './hooks.utils' interface AddHookDropdownProps { buttonText?: string onSelectHook: (hook: HOOK_DEFINITION_TITLE) => void } export const AddHookDropdown = ({ buttonText = 'Add hook', onSelectHook, }: AddHookDropdownProps) => { const { ref: projectRef } = useParams() const organization = useSelectedOrganization() const { data: subscription } = useOrgSubscriptionQuery( { orgSlug: organization?.slug }, { enabled: IS_PLATFORM } ) const { data: authConfig } = useAuthConfigQuery({ projectRef }) const canUpdateAuthHook = useCheckPermissions(PermissionAction.AUTH_EXECUTE, '*') const hooks: Hook[] = HOOKS_DEFINITIONS.map((definition) => { return { ...definition, enabled: authConfig?.[definition.enabledKey] || false, method: extractMethod( authConfig?.[definition.uriKey] || '', authConfig?.[definition.secretsKey] || '' ), } }) const nonEnterpriseHookOptions = hooks.filter((h) => !isValidHook(h) && !h.enterprise) const enterpriseHookOptions = hooks.filter((h) => !isValidHook(h) && h.enterprise) const isTeamsOrEnterprisePlan = subscription?.plan.id === 'team' || subscription?.plan.id === 'enterprise' if (!canUpdateAuthHook) { return ( {buttonText} ) } return (
{nonEnterpriseHookOptions.map((h) => ( onSelectHook(h.title)}> {h.title} ))}
{!isTeamsOrEnterprisePlan && (

Team or Enterprise Plan required

The following hooks are not available on{' '} your plan .

)} {enterpriseHookOptions.map((h) => isTeamsOrEnterprisePlan ? ( onSelectHook(h.title)} > {h.title} ) : ( onSelectHook(h.title)} > {h.title} ) )}
) }