import { useState } from 'react'; import { createPortal } from 'react-dom'; import { useTranslation } from 'react-i18next'; import { Comment } from '@plebbit/plebbit-react-hooks'; import { autoUpdate, flip, FloatingFocusManager, offset, shift, useClick, useDismiss, useFloating, useId, useInteractions, useRole } from '@floating-ui/react'; import styles from './post-menu-mobile.module.css'; import { getCommentMediaInfo } from '../../../../lib/utils/media-utils'; import { copyShareLinkToClipboard, isValidURL } from '../../../../lib/utils/url-utils'; import useEditCommentPrivileges from '../../../../hooks/use-edit-comment-privileges'; import EditMenu from '../../edit-menu/edit-menu'; interface PostMenuMobileProps { cid: string; isDescription?: boolean; isRules?: boolean; subplebbitAddress: string; onClose: () => void; } const CopyLinkButton = ({ cid, subplebbitAddress, onClose }: PostMenuMobileProps) => { const { t } = useTranslation(); return (
{ copyShareLinkToClipboard(subplebbitAddress, cid); onClose(); }} >
{t('copy_link')}
); }; const ImageSearchButtons = ({ url, onClose }: { url: string; onClose: () => void }) => { const { t } = useTranslation(); return (
{t('search_image_on_google')}
{t('search_image_on_yandex')}
{t('search_image_on_saucenao')}
); }; const ViewOnButtons = ({ cid, isDescription, isRules, subplebbitAddress, onClose }: PostMenuMobileProps) => { const { t } = useTranslation(); const viewOnOtherClientLink = `p/${subplebbitAddress}${isDescription || isRules ? '' : `/c/${cid}`}`; return (
{t('view_on_client', { client: 'Seedit' })}
{t('view_on_client', { client: 'Plebones' })}
); }; const PostMenuMobile = ({ post }: { post: Comment }) => { const { author, cid, isDescription, isRules, link, subplebbitAddress } = post || {}; const { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor } = useEditCommentPrivileges({ commentAuthorAddress: author?.address, subplebbitAddress }); const commentMediaInfo = getCommentMediaInfo(post); const { thumbnail, type, url } = commentMediaInfo || {}; const [isMenuOpen, setIsMenuOpen] = useState(false); const { refs, floatingStyles, context } = useFloating({ placement: 'bottom-start', open: isMenuOpen, onOpenChange: setIsMenuOpen, middleware: [offset({ mainAxis: 3, crossAxis: 8 }), flip(), shift({ padding: 10 })], whileElementsMounted: autoUpdate, }); const click = useClick(context); const dismiss = useDismiss(context); const role = useRole(context); const { getReferenceProps, getFloatingProps } = useInteractions([click, dismiss, role]); const headingId = useId(); const handleClose = () => setIsMenuOpen(false); return ( <> setIsMenuOpen((prev) => !prev)} ref={refs.setReference} {...getReferenceProps()}> ... {isMenuOpen && createPortal(
{cid && subplebbitAddress && } {link && isValidURL(link) && (type === 'image' || type === 'gif' || thumbnail) && url && }
, document.body, )} {(isAccountMod || isAccountCommentAuthor) && ( )} ); }; export default PostMenuMobile;