Comments.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import React, { useEffect, useRef } from 'react';
  2. import { type IRevisionHasId, pagePathUtils } from '@growi/core';
  3. import dynamic from 'next/dynamic';
  4. import { ROOT_ELEM_ID as PageCommentRootElemId, type PageCommentProps } from '~/components/PageComment';
  5. import { useSWRxPageComment } from '~/stores/comment';
  6. import { useIsTrashPage, useSWRMUTxPageInfo } from '~/stores/page';
  7. import { useCurrentUser } from '../stores/context';
  8. import type { CommentEditorProps } from './PageComment/CommentEditor';
  9. const { isTopPage } = pagePathUtils;
  10. const PageComment = dynamic<PageCommentProps>(() => import('~/components/PageComment').then(mod => mod.PageComment), { ssr: false });
  11. const CommentEditor = dynamic<CommentEditorProps>(() => import('./PageComment/CommentEditor').then(mod => mod.CommentEditor), { ssr: false });
  12. export type CommentsProps = {
  13. pageId: string,
  14. pagePath: string,
  15. revision: IRevisionHasId,
  16. onLoaded?: () => void,
  17. }
  18. export const Comments = (props: CommentsProps): JSX.Element => {
  19. const {
  20. pageId, pagePath, revision, onLoaded,
  21. } = props;
  22. const { mutate } = useSWRxPageComment(pageId);
  23. const { trigger: mutatePageInfo } = useSWRMUTxPageInfo(pageId);
  24. const { data: isDeleted } = useIsTrashPage();
  25. const { data: currentUser } = useCurrentUser();
  26. const pageCommentParentRef = useRef<HTMLDivElement>(null);
  27. useEffect(() => {
  28. const parent = pageCommentParentRef.current;
  29. if (parent == null) return;
  30. const observerCallback = (mutationRecords: MutationRecord[]) => {
  31. mutationRecords.forEach((record: MutationRecord) => {
  32. const target = record.target as HTMLElement;
  33. for (const child of Array.from(target.children)) {
  34. const childId = (child as HTMLElement).id;
  35. if (childId === PageCommentRootElemId) {
  36. onLoaded?.();
  37. break;
  38. }
  39. }
  40. });
  41. };
  42. const observer = new MutationObserver(observerCallback);
  43. observer.observe(parent, { childList: true });
  44. return () => {
  45. observer.disconnect();
  46. };
  47. }, [onLoaded]);
  48. const isTopPagePath = isTopPage(pagePath);
  49. if (pageId == null || isTopPagePath) {
  50. return <></>;
  51. }
  52. const onCommentButtonClickHandler = () => {
  53. mutate();
  54. mutatePageInfo();
  55. };
  56. return (
  57. <div className="page-comments-row mt-5 py-4 d-edit-none d-print-none">
  58. <div className="container-lg">
  59. <div id="page-comments-list" className="page-comments-list" ref={pageCommentParentRef}>
  60. <PageComment
  61. pageId={pageId}
  62. pagePath={pagePath}
  63. revision={revision}
  64. currentUser={currentUser}
  65. isReadOnly={false}
  66. titleAlign="left"
  67. hideIfEmpty={false}
  68. />
  69. </div>
  70. {!isDeleted && (
  71. <div id="page-comment-write">
  72. <CommentEditor
  73. pageId={pageId}
  74. isForNewComment
  75. onCommentButtonClicked={onCommentButtonClickHandler}
  76. revisionId={revision._id}
  77. />
  78. </div>
  79. )}
  80. </div>
  81. </div>
  82. );
  83. };