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 { useAsyncCheckProjectPermissions } from 'hooks/misc/useCheckPermissions' import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization' 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 align?: 'end' | 'center' onSelectHook: (hook: HOOK_DEFINITION_TITLE) => void } export const AddHookDropdown = ({ buttonText = 'Add hook', align = 'end', onSelectHook, }: AddHookDropdownProps) => { const { ref: projectRef } = useParams() const { data: organization } = useSelectedOrganizationQuery() const { data: authConfig } = useAuthConfigQuery({ projectRef }) const { can: canUpdateAuthHook } = useAsyncCheckProjectPermissions( 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 = organization?.plan.id === 'team' || organization?.plan.id === 'enterprise' if (!canUpdateAuthHook) { return ( {buttonText} ) } return ( }> {buttonText} {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} ) )} ) }
Team or Enterprise Plan required
The following hooks are not available on{' '} your plan .