Newer
Older
/* eslint-disable no-param-reassign */
import React, { useContext, useMemo, useState, useEffect } from 'react';
import { CommentDecorationPluginKey } from '../plugins/CommentDecorationPlugin';
const ConnectedCommentStyled = styled.div`
margin-left: ${props => (props.active ? `${-20}px` : `${50}px`)};
transition: ${props =>
props.active && props.length ? `none!important` : `all 1.3s`};
export default ({ comment, top, commentId, recalculateTops, users }) => {
const [isActive, setIsActive] = useState(false);
const { viewId, conversation } = comment.data;
const commentConfig = app.config.get('config.CommentsService');
const isReadOnly =
commentConfig && commentConfig.readOnly ? commentConfig.readOnly : false;
const showTitle =
commentConfig && commentConfig.showTitle ? commentConfig.showTitle : false;
const commentPlugin = app.PmPlugins.get('commentPlugin');
const activeComment = commentPlugin.getState(activeView.state).comment;
useEffect(() => {
setIsActive(false);
if (activeComment && commentId === activeComment.id) {
setIsActive(true);
}
}, [activeComment]);
const onClickPost = ({ commentValue, title }) => {
const currentUser = user || (users || []).find(u => u.currentUser === true);
displayName: currentUser
? currentUser.displayName || currentUser.username
: 'Anonymous',
userId: currentUser ? currentUser.userId : '1',
comment.data.title = title || comment.data.title;
comment.data.conversation.push(obj);
type: 'updateComment',
id: activeComment.id,
data: comment.data,
}),
);
pmViews[viewId].dispatch(
pmViews[viewId].state.tr.setSelection(
new TextSelection(
pmViews[viewId].state.tr.doc.resolve(comment.data.pmFrom),
),
type: 'deleteComment',
id: activeComment.id,
}),
);
const onTextAreaBlur = () => {
if (conversation.length === 0 && !clickPost) {
onClickResolve();
activeView.focus();
}
};
const MemorizedComponent = useMemo(
() => (
<ConnectedCommentStyled
commentId={commentId}
onClickPost={onClickPost}
onClickResolve={onClickResolve}
onTextAreaBlur={onTextAreaBlur}
[isActive, top, conversation.length, users],