Comment.tsx 5.9 KB

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