import type { FC } from 'react'; import React, { useState, useMemo, memo, useCallback, } from 'react'; import { isPopulated, getIdForRef, type IRevisionHasId } from '@growi/core'; import { UserPicture } from '@growi/ui/dist/components'; import { useTranslation } from 'next-i18next'; import { apiPost } from '~/client/util/apiv1-client'; import { toastError } from '~/client/util/toastr'; import type { RendererOptions } from '~/interfaces/renderer-options'; import { useSWRMUTxPageInfo } from '~/stores/page'; import { useCommentForCurrentPageOptions } from '~/stores/renderer'; import type { ICommentHasId, ICommentHasIdList } from '../interfaces/comment'; import { useSWRxPageComment } from '../stores/comment'; import { NotAvailableForGuest } from './NotAvailableForGuest'; import { NotAvailableForReadOnlyUser } from './NotAvailableForReadOnlyUser'; import { Comment } from './PageComment/Comment'; import { CommentEditor } from './PageComment/CommentEditor'; import { DeleteCommentModal } from './PageComment/DeleteCommentModal'; import { ReplyComments } from './PageComment/ReplyComments'; import styles from './PageComment.module.scss'; type PageCommentProps = { rendererOptions?: RendererOptions, pageId: string, pagePath: string, revision: string | IRevisionHasId, currentUser: any, isReadOnly: boolean, } export const PageComment: FC = memo((props: PageCommentProps): JSX.Element => { const { rendererOptions: rendererOptionsByProps, pageId, pagePath, revision, currentUser, isReadOnly, } = props; const { data: comments, mutate } = useSWRxPageComment(pageId); const { data: rendererOptionsForCurrentPage } = useCommentForCurrentPageOptions(); const [commentToBeDeleted, setCommentToBeDeleted] = useState(null); const [isDeleteConfirmModalShown, setIsDeleteConfirmModalShown] = useState(false); const [showEditorIds, setShowEditorIds] = useState>(new Set()); const [errorMessageOnDelete, setErrorMessageOnDelete] = useState(''); const { trigger: mutatePageInfo } = useSWRMUTxPageInfo(pageId); const { t } = useTranslation(''); const commentsFromOldest = useMemo(() => (comments != null ? [...comments].reverse() : null), [comments]); const commentsExceptReply: ICommentHasIdList | undefined = useMemo( () => commentsFromOldest?.filter(comment => comment.replyTo == null), [commentsFromOldest], ); const allReplies = {}; if (commentsFromOldest != null) { commentsFromOldest.forEach((comment) => { if (comment.replyTo != null) { allReplies[comment.replyTo] = allReplies[comment.replyTo] == null ? [comment] : [...allReplies[comment.replyTo], comment]; } }); } const onClickDeleteButton = useCallback((comment: ICommentHasId) => { setCommentToBeDeleted(comment); setIsDeleteConfirmModalShown(true); }, []); const onCancelDeleteComment = useCallback(() => { setCommentToBeDeleted(null); setIsDeleteConfirmModalShown(false); }, []); const onDeleteCommentAfterOperation = useCallback(() => { onCancelDeleteComment(); mutate(); mutatePageInfo(); }, [mutate, onCancelDeleteComment, mutatePageInfo]); const onDeleteComment = useCallback(async() => { if (commentToBeDeleted == null) return; try { await apiPost('/comments.remove', { comment_id: commentToBeDeleted._id }); onDeleteCommentAfterOperation(); } catch (error: unknown) { setErrorMessageOnDelete(error as string); toastError(`error: ${error}`); } }, [commentToBeDeleted, onDeleteCommentAfterOperation]); const removeShowEditorId = useCallback((commentId: string) => { setShowEditorIds((previousState) => { return new Set([...previousState].filter(id => id !== commentId)); }); }, []); const onReplyButtonClickHandler = useCallback((commentId: string) => { setShowEditorIds(previousState => new Set([...previousState, commentId])); }, []); const onCommentButtonClickHandler = useCallback((commentId: string) => { removeShowEditorId(commentId); mutate(); mutatePageInfo(); }, [removeShowEditorId, mutate, mutatePageInfo]); if (comments?.length === 0) { return <>; } const rendererOptions = rendererOptionsByProps ?? rendererOptionsForCurrentPage; if (commentsFromOldest == null || commentsExceptReply == null || rendererOptions == null) { return <>; } const revisionId = getIdForRef(revision); const revisionCreatedAt = (isPopulated(revision)) ? revision.createdAt : undefined; const commentElement = (comment: ICommentHasId) => ( ); const replyCommentsElement = (replyComments: ICommentHasIdList) => ( ); return (
{commentsExceptReply.map((comment) => { const defaultCommentThreadClasses = 'page-comment-thread mb-2'; const hasReply: boolean = Object.keys(allReplies).includes(comment._id); let commentThreadClasses = ''; commentThreadClasses = hasReply ? `${defaultCommentThreadClasses} page-comment-thread-no-replies` : defaultCommentThreadClasses; return (
{/* Comment */} {commentElement(comment)} {/* Reply comments */} {hasReply && replyCommentsElement(allReplies[comment._id])} {(!isReadOnly && !showEditorIds.has(comment._id)) && (
)} {/* Editor to reply */} {(!isReadOnly && showEditorIds.has(comment._id)) && ( { removeShowEditorId(comment._id); }} onCommented={() => onCommentButtonClickHandler(comment._id)} revisionId={revisionId} /> )}
); })}
{!isReadOnly && ( )}
); }); PageComment.displayName = 'PageComment';