Comment.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import React, { useEffect, useState } from 'react';
  2. import { UserPicture } from '@growi/ui';
  3. import { ConsoleFormattedStream } from 'browser-bunyan';
  4. import { format } from 'date-fns';
  5. import { useTranslation } from 'next-i18next';
  6. import { UncontrolledTooltip } from 'reactstrap';
  7. import { RendererOptions } from '~/services/renderer/renderer';
  8. import { useCurrentUser } from '~/stores/context';
  9. import { ICommentHasId } from '../../interfaces/comment';
  10. import FormattedDistanceDate from '../FormattedDistanceDate';
  11. import HistoryIcon from '../Icons/HistoryIcon';
  12. import RevisionRenderer from '../Page/RevisionRenderer';
  13. import Username from '../User/Username';
  14. import { CommentControl } from './CommentControl';
  15. import CommentEditor from './CommentEditor';
  16. type CommentProps = {
  17. comment: ICommentHasId,
  18. isReadOnly: boolean,
  19. deleteBtnClicked: (comment: ICommentHasId) => void,
  20. onComment: () => void,
  21. rendererOptions: RendererOptions,
  22. currentPagePath: string,
  23. currentRevisionId: string,
  24. currentRevisionCreatedAt: Date,
  25. }
  26. export const Comment = (props: CommentProps): JSX.Element => {
  27. const {
  28. comment, isReadOnly, deleteBtnClicked, onComment, rendererOptions, currentPagePath, currentRevisionId, currentRevisionCreatedAt,
  29. } = props;
  30. const { t } = useTranslation();
  31. const { data: currentUser } = useCurrentUser();
  32. const [markdown, setMarkdown] = useState('');
  33. const [isReEdit, setIsReEdit] = useState(false);
  34. const commentId = comment._id;
  35. const creator = comment.creator;
  36. const isMarkdown = comment.isMarkdown;
  37. const createdAt = new Date(comment.createdAt);
  38. const updatedAt = new Date(comment.updatedAt);
  39. const isEdited = createdAt < updatedAt;
  40. useEffect(() => {
  41. setMarkdown(comment.comment);
  42. const isCurrentRevision = () => {
  43. return comment.revision === currentRevisionId;
  44. };
  45. isCurrentRevision();
  46. }, [comment, currentRevisionId]);
  47. const isCurrentUserEqualsToAuthor = () => {
  48. const { creator }: any = comment;
  49. if (creator == null || currentUser == null) {
  50. return false;
  51. }
  52. return creator.username === currentUser.username;
  53. };
  54. const getRootClassName = (comment) => {
  55. let className = 'page-comment flex-column';
  56. if (comment.revision === currentRevisionId) {
  57. className += ' page-comment-current';
  58. }
  59. else if (comment.createdAt.getTime() > currentRevisionCreatedAt.getTime()) {
  60. className += ' page-comment-newer';
  61. }
  62. else {
  63. className += ' page-comment-older';
  64. }
  65. if (isCurrentUserEqualsToAuthor()) {
  66. className += ' page-comment-me';
  67. }
  68. return className;
  69. };
  70. const deleteBtnClickedHandler = (comment) => {
  71. deleteBtnClicked(comment);
  72. };
  73. const renderText = (comment) => {
  74. return <span style={{ whiteSpace: 'pre-wrap' }}>{comment}</span>;
  75. };
  76. // TODO: Remove when update ReplayComments.jsx
  77. if (currentPagePath == null) {
  78. return <></>;
  79. }
  80. const renderRevisionBody = () => {
  81. return (
  82. <RevisionRenderer
  83. rendererOptions={rendererOptions}
  84. markdown={markdown}
  85. additionalClassName="comment"
  86. pagePath={currentPagePath}
  87. />
  88. );
  89. };
  90. const rootClassName = getRootClassName(comment);
  91. const commentBody = isMarkdown ? renderRevisionBody() : renderText(comment.comment);
  92. const revHref = `?revision=${comment.revision}`;
  93. const editedDateId = `editedDate-${comment._id}`;
  94. const editedDateFormatted = isEdited
  95. ? format(updatedAt, 'yyyy/MM/dd HH:mm')
  96. : null;
  97. return (
  98. <>
  99. {(isReEdit && !isReadOnly) ? (
  100. <CommentEditor
  101. rendererOptions={rendererOptions}
  102. currentCommentId={commentId}
  103. commentBody={comment.comment}
  104. replyTo={undefined}
  105. commentCreator={creator?.username}
  106. onCancelButtonClicked={() => setIsReEdit(false)}
  107. onCommentButtonClicked={() => {
  108. setIsReEdit(false);
  109. if (onComment != null) onComment();
  110. }}
  111. />
  112. ) : (
  113. <div id={commentId} className={rootClassName}>
  114. <div className="page-comment-writer">
  115. <UserPicture user={creator} />
  116. </div>
  117. <div className="page-comment-main">
  118. <div className="page-comment-creator">
  119. <Username user={creator} />
  120. </div>
  121. <div className="page-comment-body">{commentBody}</div>
  122. <div className="page-comment-meta">
  123. <a href={`#${commentId}`}>
  124. <FormattedDistanceDate id={commentId} date={comment.createdAt} />
  125. </a>
  126. { isEdited && (
  127. <>
  128. <span id={editedDateId}>&nbsp;(edited)</span>
  129. <UncontrolledTooltip placement="bottom" fade={false} target={editedDateId}>{editedDateFormatted}</UncontrolledTooltip>
  130. </>
  131. )}
  132. <span className="ml-2">
  133. <a id={`page-comment-revision-${commentId}`} className="page-comment-revision" href={revHref}>
  134. <HistoryIcon />
  135. </a>
  136. <UncontrolledTooltip placement="bottom" fade={false} target={`page-comment-revision-${commentId}`}>
  137. {t('page_comment.display_the_page_when_posting_this_comment')}
  138. </UncontrolledTooltip>
  139. </span>
  140. </div>
  141. {(isCurrentUserEqualsToAuthor() && !isReadOnly) && (
  142. <CommentControl
  143. onClickDeleteBtn={deleteBtnClickedHandler}
  144. onClickEditBtn={() => setIsReEdit(true)}
  145. />
  146. ) }
  147. </div>
  148. </div>
  149. )
  150. }
  151. </>
  152. );
  153. };