Comment.tsx 5.6 KB

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