diff --git a/frontend/web/components/UserAction.tsx b/frontend/web/components/UserAction.tsx index 18b979250e1c..cdf0d8b47fe8 100644 --- a/frontend/web/components/UserAction.tsx +++ b/frontend/web/components/UserAction.tsx @@ -1,10 +1,7 @@ import { FC, useCallback, useLayoutEffect, useRef, useState } from 'react' -import classNames from 'classnames' +import { createPortal } from 'react-dom' import useOutsideClick from 'common/useOutsideClick' -import Utils from 'common/utils/utils' -import Constants from 'common/constants' -import Permission from 'common/providers/Permission' import Button from './base/forms/Button' import Icon from './Icon' import ActionButton from './ActionButton' @@ -18,6 +15,15 @@ interface FeatureActionProps { type ActionType = 'edit' | 'remove' +type ActionDropdownProps = { + isOpen: boolean + canEdit?: boolean + canRemove?: boolean + btnRef: React.RefObject + onAction: (action: ActionType) => void + onOutsideClick: () => void +} + function calculateListPosition( btnEl: HTMLElement, listEl: HTMLElement, @@ -31,6 +37,62 @@ function calculateListPosition( } } +const ActionDropdown = ({ + btnRef, + canEdit, + canRemove, + isOpen, + onAction, + onOutsideClick, +}: ActionDropdownProps) => { + const dropDownRef = useRef(null) + + useOutsideClick(dropDownRef, onOutsideClick) + + useLayoutEffect(() => { + if (!isOpen || !dropDownRef.current || !btnRef.current) return + const listPosition = calculateListPosition( + btnRef.current, + dropDownRef.current, + ) + dropDownRef.current.style.top = `${listPosition.top}px` + dropDownRef.current.style.left = `${listPosition.left}px` + }, [btnRef, isOpen, dropDownRef]) + + if (!isOpen) return null + + return createPortal( +
+ {!!canEdit && ( +
{ + e.stopPropagation() + onAction('edit') + }} + > + + Manage user +
+ )} + + {!!canRemove && ( +
{ + e.stopPropagation() + onAction('remove') + }} + > + + Remove +
+ )} +
, + document.body, + ) +} + export const FeatureAction: FC = ({ canEdit, canRemove, @@ -40,7 +102,6 @@ export const FeatureAction: FC = ({ const [isOpen, setIsOpen] = useState(false) const btnRef = useRef(null) - const listRef = useRef(null) const close = useCallback(() => setIsOpen(false), []) @@ -61,15 +122,6 @@ export const FeatureAction: FC = ({ [close, onRemove, onEdit], ) - useOutsideClick(listRef, handleOutsideClick) - - useLayoutEffect(() => { - if (!isOpen || !listRef.current || !btnRef.current) return - const listPosition = calculateListPosition(btnRef.current, listRef.current) - listRef.current.style.top = `${listPosition.top}px` - listRef.current.style.left = `${listPosition.left}px` - }, [isOpen]) - if (!canEdit && !!canRemove) { return (