import { useState, type ComponentProps, useCallback, type Dispatch, type SetStateAction, } from 'react' import { useTranslation } from 'react-i18next' import { Dropdown, MenuItem } from 'react-bootstrap' import { User } from '../../../../../../types/group-management/user' import useAsync from '@/shared/hooks/use-async' import { type FetchError, postJSON } from '@/infrastructure/fetch-json' import Icon from '@/shared/components/icon' import { ManagedUserAlert } from '../../utils/types' import { useGroupMembersContext } from '../../context/group-members-context' import getMeta from '@/utils/meta' type resendInviteResponse = { success: boolean } type ManagedUserDropdownButtonProps = { user: User openOffboardingModalForUser: (user: User) => void groupId: string setManagedUserAlert: Dispatch> } export default function DropdownButton({ user, openOffboardingModalForUser, groupId, setManagedUserAlert, }: ManagedUserDropdownButtonProps) { const { t } = useTranslation() const { removeMember } = useGroupMembersContext() const [isOpened, setIsOpened] = useState(false) const { runAsync: runResendManagedUserInviteAsync, isLoading: isResendingManagedUserInvite, } = useAsync() const { runAsync: runResendLinkSSOInviteAsync, isLoading: isResendingSSOLinkInvite, } = useAsync() const { runAsync: runResendGroupInviteAsync, isLoading: isResendingGroupInvite, } = useAsync() const managedUsersActive = getMeta('ol-managedUsersActive') const groupSSOActive = getMeta('ol-groupSSOActive') const userPending = user.invite const isGroupSSOLinked = !userPending && user.enrollment?.sso?.some(sso => sso.groupId === groupId) const isUserManaged = !userPending && user.enrollment?.managedBy === groupId const handleResendManagedUserInvite = useCallback( async user => { try { const result = await runResendManagedUserInviteAsync( postJSON( `/manage/groups/${groupId}/resendManagedUserInvite/${user._id}` ) ) if (result.success) { setManagedUserAlert({ variant: 'resendManagedUserInviteSuccess', email: user.email, }) setIsOpened(false) } } catch (err) { if ((err as FetchError)?.response?.status === 429) { setManagedUserAlert({ variant: 'resendInviteTooManyRequests', email: user.email, }) } else { setManagedUserAlert({ variant: 'resendManagedUserInviteFailed', email: user.email, }) } setIsOpened(false) } }, [setManagedUserAlert, groupId, runResendManagedUserInviteAsync] ) const handleResendLinkSSOInviteAsync = useCallback( async user => { try { const result = await runResendLinkSSOInviteAsync( postJSON(`/manage/groups/${groupId}/resendSSOLinkInvite/${user._id}`) ) if (result.success) { setManagedUserAlert({ variant: 'resendSSOLinkInviteSuccess', email: user.email, }) setIsOpened(false) } } catch (err) { if ((err as FetchError)?.response?.status === 429) { setManagedUserAlert({ variant: 'resendInviteTooManyRequests', email: user.email, }) } else { setManagedUserAlert({ variant: 'resendSSOLinkInviteFailed', email: user.email, }) } setIsOpened(false) } }, [setManagedUserAlert, groupId, runResendLinkSSOInviteAsync] ) const handleResendGroupInvite = useCallback( async user => { try { await runResendGroupInviteAsync( postJSON(`/manage/groups/${groupId}/resendInvite/`, { body: { email: user.email, }, }) ) setManagedUserAlert({ variant: 'resendGroupInviteSuccess', email: user.email, }) setIsOpened(false) } catch (err) { if ((err as FetchError)?.response?.status === 429) { setManagedUserAlert({ variant: 'resendInviteTooManyRequests', email: user.email, }) } else { setManagedUserAlert({ variant: 'resendGroupInviteFailed', email: user.email, }) } setIsOpened(false) } }, [setManagedUserAlert, groupId, runResendGroupInviteAsync] ) const onResendManagedUserInviteClick = () => { handleResendManagedUserInvite(user) } const onResendSSOLinkInviteClick = () => { handleResendLinkSSOInviteAsync(user) } const onResendGroupInviteClick = () => { handleResendGroupInvite(user) } const onDeleteUserClick = () => { openOffboardingModalForUser(user) } const onRemoveFromGroup = () => { removeMember(user) } const buttons = [] if (userPending) { buttons.push( {t('resend_group_invite')} {isResendingGroupInvite ? ( ) : null} ) } if (managedUsersActive && !isUserManaged && !userPending) { buttons.push( {t('resend_managed_user_invite')} {isResendingManagedUserInvite ? ( ) : null} ) } if (groupSSOActive && !isGroupSSOLinked && !userPending) { buttons.push( {t('resend_link_sso')} {isResendingSSOLinkInvite ? ( ) : null} ) } if (isUserManaged && !user.isEntityAdmin) { buttons.push( {t('delete_user')} ) } else if (!isUserManaged) { buttons.push( {t('remove_from_group')} ) } if (buttons.length === 0) { buttons.push( {t('no_actions')} ) } return ( setIsOpened(open)} >