import React, { createElement, FC, Fragment, memo, ReactNode, useCallback, useMemo, useState, } from 'react'; import classnames from 'classnames'; import { CommentForm } from '~/components/comment/CommentForm'; import { Authorized } from '~/components/containers/Authorized'; 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 { imagePresets } from '~/constants/urls'; import { IComment, IFile } from '~/types'; import { formatCommentText, getPrettyDate, getURL } from '~/utils/dom'; import { append, assocPath, path, reduce } from '~/utils/ramda'; import { CommentImageGrid } from '../CommentImageGrid'; import { CommentMenu } from '../CommentMenu'; import styles from './styles.module.scss'; interface IProps { prefix?: ReactNode; nodeId: number; comment: IComment; canEdit: boolean; saveComment: (data: IComment) => Promise; onDelete: (id: IComment['id'], isLocked: boolean) => void; onShowImageModal: (images: IFile[], index: number) => void; } const CommentContent: FC = memo( ({ comment, canEdit, nodeId, saveComment, 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.id, !comment.deleted_at); }, [comment, onDelete]); 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}
} {comment.text.trim() && ( {menu} {blocks.map( (block, key) => COMMENT_BLOCK_RENDERERS[block.type] && createElement(COMMENT_BLOCK_RENDERERS[block.type], { block, key, }), )}
{getPrettyDate(comment.created_at)}
)} {groupped.image && groupped.image.length > 0 && (
{menu}
{getPrettyDate(comment.created_at)}
)} {groupped.audio && groupped.audio.length > 0 && ( {groupped.audio.map((file) => (
{menu}
{getPrettyDate(comment.created_at)}
))}
)}
); }, ); export { CommentContent };