import { createElement, FC, memo, ReactNode, useCallback, useMemo, useState, } from 'react'; import classnames from 'classnames'; import { Group } from '~/components/containers/Group'; import { AudioPlayer } from '~/components/media/AudioPlayer'; import { COMMENT_BLOCK_RENDERERS } from '~/constants/comment'; import { UploadType } from '~/constants/uploads'; import { IComment, IFile } from '~/types'; import { formatCommentText, getPrettyDate } from '~/utils/dom'; import { append, assocPath, path, reduce } from '~/utils/ramda'; import { CommentEditingForm } from '../CommentEditingForm'; import { CommentImageGrid } from '../CommentImageGrid'; import { CommentLike } from '../CommentLike'; import { CommentMenu } from '../CommentMenu'; import styles from './styles.module.scss'; interface IProps { prefix?: ReactNode; nodeId: number; comment: IComment; canEdit: boolean; canLike: boolean; saveComment: (data: IComment) => Promise; onDelete: (isLocked: boolean) => void; onLike: () => void; onShowImageModal: (images: IFile[], index: number) => void; } const CommentContent: FC = memo( ({ comment, nodeId, saveComment, canEdit, canLike, onLike, onDelete, onShowImageModal, prefix, }) => { const [isEditing, setIsEditing] = useState(false); const startEditing = useCallback(() => setIsEditing(true), [setIsEditing]); const stopEditing = useCallback(() => setIsEditing(false), [setIsEditing]); const groupped = useMemo>( () => reduce( (group, file) => file.type ? assocPath([file.type], append(file, group[file.type]), group) : group, {} as Record, comment.files, ), [comment], ); const onLockClick = useCallback(() => { onDelete(!comment.deleted_at); }, [comment, onDelete]); const onImageClick = useCallback( (file: IFile) => onShowImageModal(groupped.image, groupped.image.indexOf(file)), [onShowImageModal, groupped], ); const menu = useMemo( () => canEdit && (
), [canEdit, startEditing, onLockClick], ); const blocks = useMemo( () => !!comment.text.trim() ? formatCommentText(path(['user', 'username'], comment), comment.text) : [], [comment], ); if (isEditing) { return ( ); } return (
{!!prefix &&
{prefix}
}
{menu}
{comment.text.trim() && ( {blocks.map( (block, key) => COMMENT_BLOCK_RENDERERS[block.type] && createElement(COMMENT_BLOCK_RENDERERS[block.type], { block, key, }), )} )} {groupped.image && groupped.image.length > 0 && (
)} {groupped.audio && groupped.audio.length > 0 && groupped.audio.map((file) => (
))}
{getPrettyDate(comment.created_at)}
); }, ); export { CommentContent };