// @flow import React, { useState } from 'react'; import { Manager, Reference, Popper } from 'react-popper'; import { connect } from 'react-redux'; import compose from 'recompose/compose'; import { openModal } from 'src/actions/modals'; import Flyout from 'src/components/flyout'; import OutsideClickHandler from 'src/components/outsideClickHandler'; import Icon from 'src/components/icon'; import { TextButton } from 'src/components/button'; import { withCurrentUser } from 'src/components/withCurrentUser'; import { FlyoutRow, DropWrap, Label } from '../style'; type Props = { thread: Object, // Injected currentUser: Object, dispatch: Function, }; const ActionsDropdown = (props: Props) => { const { thread, dispatch, currentUser } = props; if (!currentUser) return null; const { channel: { channelPermissions }, community: { communityPermissions }, } = thread; const isThreadAuthor = currentUser && currentUser.id === thread.author.user.id; const isChannelModerator = currentUser && channelPermissions.isModerator; const isCommunityModerator = currentUser && communityPermissions.isModerator; const isChannelOwner = currentUser && channelPermissions.isOwner; const isCommunityOwner = currentUser && communityPermissions.isOwner; const shouldRenderDeleteThreadAction = isThreadAuthor || isChannelModerator || isCommunityModerator || isChannelOwner || isCommunityOwner; const triggerDelete = e => { e.preventDefault(); let message; if (isCommunityOwner && !thread.isAuthor) { message = `You are about to delete another person's thread. As the owner of the ${ thread.community.name } community, you have permission to do this. The thread author will be notified that this thread was deleted.`; } else if (isChannelOwner && !thread.isAuthor) { message = `You are about to delete another person's thread. As the owner of the ${ thread.channel.name } channel, you have permission to do this. The thread author will be notified that this thread was deleted.`; } else { message = 'Are you sure you want to delete this thread?'; } return dispatch( openModal('DELETE_DOUBLE_CHECK_MODAL', { id: thread.id, entity: 'thread', message, extraProps: { thread, }, }) ); }; const [flyoutOpen, setFlyoutOpen] = useState(false); if (!shouldRenderDeleteThreadAction) { return null; } return ( {({ ref }) => { return ( setFlyoutOpen(!flyoutOpen)} data-cy="thread-actions-dropdown-trigger" /> ); }} {flyoutOpen && ( setFlyoutOpen(false)}> {({ style, ref }) => { return (
{shouldRenderDeleteThreadAction && ( )}
); }}
)}
); }; export default compose( withCurrentUser, connect() )(ActionsDropdown);