Comment.tsx 5.7 KB

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