Comment.tsx 5.6 KB

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