import { useEffect, useRef, useState } from 'react'; import { Trans, useTranslation } from 'react-i18next'; import { autoUpdate, flip, FloatingFocusManager, offset, shift, useClick, useDismiss, useFloating, useId, useInteractions, useRole } from '@floating-ui/react'; import { PublishCommentEditOptions, useComment, useEditedComment, usePublishCommentEdit } from '@plebbit/plebbit-react-hooks'; import styles from './edit-menu.module.css'; import { alertChallengeVerificationFailed } from '../../lib/utils/challenge-utils'; import useChallengesStore from '../../stores/use-challenges-store'; import _ from 'lodash'; import useIsMobile from '../../hooks/use-is-mobile'; const { addChallenge } = useChallengesStore.getState(); type EditMenuProps = { commentCid: string; isAccountMod?: boolean; isAccountCommentAuthor?: boolean; isCommentAuthorMod?: boolean; }; const EditMenu = ({ commentCid, isAccountMod, isAccountCommentAuthor, isCommentAuthorMod }: EditMenuProps) => { const { t } = useTranslation(); const isMobile = useIsMobile(); let post: any; const comment = useComment({ commentCid }); const { editedComment } = useEditedComment({ comment }); if (editedComment) { post = editedComment; } else if (comment) { post = comment; } const { banExpiresAt, content, deleted, locked, parentCid, pinned, removed, spoiler, subplebbitAddress } = post || {}; const isReply = parentCid; const [isEditMenuOpen, setIsEditMenuOpen] = useState(false); const [isContentEditorOpen, setIsContentEditorOpen] = useState(false); const contentEditorRef = useRef(null); useEffect(() => { if (contentEditorRef.current) { contentEditorRef.current.focus(); } }, []); const defaultPublishEditOptions: PublishCommentEditOptions = { commentAuthor: isAccountMod && !isAccountCommentAuthor && banExpiresAt !== undefined ? { banExpiresAt } : undefined, commentCid, content: isAccountCommentAuthor ? content : undefined, deleted: isAccountCommentAuthor ? deleted : undefined, locked: isAccountMod ? locked : undefined, pinned: isAccountMod ? pinned : undefined, removed: isAccountMod ? removed : undefined, spoiler, subplebbitAddress, onChallenge: (...args: any) => addChallenge([...args, post]), onChallengeVerification: alertChallengeVerificationFailed, onError: (error: Error) => { console.warn(error); alert('Comment edit failed. ' + error.message); }, }; const [publishCommentEditOptions, setPublishCommentEditOptions] = useState(defaultPublishEditOptions); const { publishCommentEdit } = usePublishCommentEdit(publishCommentEditOptions); const [banDuration, setBanDuration] = useState(1); const daysToTimestampInSeconds = (days: number) => { const now = new Date(); now.setDate(now.getDate() + days); return Math.floor(now.getTime() / 1000); }; const onCheckbox = (e: React.ChangeEvent) => { const { id, checked } = e.target; if (id === 'banUser') { setPublishCommentEditOptions((state) => ({ ...state, commentAuthor: { ...state.commentAuthor, banExpiresAt: checked ? daysToTimestampInSeconds(banDuration) : undefined }, })); } else { setPublishCommentEditOptions((state) => ({ ...state, [id]: checked })); } }; const onBanDurationChange = (e: React.ChangeEvent) => { const days = parseInt(e.target.value, 10) || 1; setBanDuration(days); setPublishCommentEditOptions((state) => ({ ...state, commentAuthor: { ...state.commentAuthor, banExpiresAt: daysToTimestampInSeconds(days) }, })); }; const onReason = (e: React.ChangeEvent) => setPublishCommentEditOptions((state) => ({ ...state, reason: e.target.value ? e.target.value : undefined })); const { refs, floatingStyles, context } = useFloating({ placement: 'bottom-start', open: isEditMenuOpen, onOpenChange: setIsEditMenuOpen, middleware: [offset(2), flip({ fallbackAxisSideDirection: 'end' }), shift()], 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 _publishCommentEdit = async () => { console.log('publishCommentEditOptions', publishCommentEditOptions); try { await publishCommentEdit(); } catch (error) { if (error instanceof Error) { console.warn(error); alert(error.message); } } setIsEditMenuOpen(false); }; return ( <> commentCid && setIsEditMenuOpen(!isEditMenuOpen)} checked={isEditMenuOpen} disabled={!isAccountCommentAuthor && !isAccountMod} /> {isEditMenuOpen && (isAccountCommentAuthor || isAccountMod) && (
{isAccountCommentAuthor && ( <>
{isContentEditorOpen && (