Comment.tsx 6.3 KB

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