diff --git a/src/components/edit-menu/edit-menu.tsx b/src/components/edit-menu/edit-menu.tsx index 97228d1a..cf83432b 100644 --- a/src/components/edit-menu/edit-menu.tsx +++ b/src/components/edit-menu/edit-menu.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef, useState } from 'react'; +import { 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 { Comment, PublishCommentEditOptions, usePublishCommentEdit } from '@plebbit/plebbit-react-hooks'; @@ -17,25 +17,27 @@ type EditMenuProps = { post: Comment; }; +const daysToTimestampInSeconds = (days: number) => { + const now = new Date(); + now.setDate(now.getDate() + days); + return Math.floor(now.getTime() / 1000); +}; + +const timestampToDays = (timestamp: number) => { + const now = Math.floor(Date.now() / 1000); + return Math.max(1, Math.floor((timestamp - now) / (24 * 60 * 60))); +}; + const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, post }: EditMenuProps) => { const { t } = useTranslation(); const isMobile = useIsMobile(); - - const { banExpiresAt, cid, content, deleted, locked, parentCid, pinned, removed, spoiler, subplebbitAddress } = post || {}; + const { cid, commentAuthor, content, deleted, locked, parentCid, pinned, reason, 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, + commentAuthor: isAccountMod && !isAccountCommentAuthor ? commentAuthor : undefined, commentCid: cid, content: isAccountCommentAuthor ? content : undefined, deleted: isAccountCommentAuthor ? deleted : undefined, @@ -55,13 +57,9 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po 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 [banDuration, setBanDuration] = useState(() => + publishCommentEditOptions.commentAuthor?.banExpiresAt ? timestampToDays(publishCommentEditOptions.commentAuthor.banExpiresAt) : 1, + ); const onCheckbox = (e: React.ChangeEvent) => { const { id, checked } = e.target; @@ -145,8 +143,8 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po