Comment.tsx 6.3 KB

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