Comments.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import React, { useCallback, 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 } 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. onCommentUpdated?: () => void,
  18. }
  19. export const Comments = (props: CommentsProps): JSX.Element => {
  20. const {
  21. pageId, pagePath, revision, onLoaded, onCommentUpdated,
  22. } = props;
  23. const { mutate } = useSWRxPageComment(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 = useCallback(() => {
  53. mutate();
  54. if (onCommentUpdated != null) {
  55. onCommentUpdated()
  56. }
  57. }, [mutate, onCommentUpdated]);
  58. return (
  59. <div className="page-comments-row mt-5 py-4 d-edit-none d-print-none">
  60. <div className="container-lg">
  61. <div id="page-comments-list" className="page-comments-list" ref={pageCommentParentRef}>
  62. <PageComment
  63. pageId={pageId}
  64. pagePath={pagePath}
  65. revision={revision}
  66. currentUser={currentUser}
  67. isReadOnly={false}
  68. titleAlign="left"
  69. hideIfEmpty={false}
  70. />
  71. </div>
  72. {!isDeleted && (
  73. <div id="page-comment-write">
  74. <CommentEditor
  75. pageId={pageId}
  76. isForNewComment
  77. onCommentButtonClicked={onCommentButtonClickHandler}
  78. revisionId={revision._id}
  79. />
  80. </div>
  81. )}
  82. </div>
  83. </div>
  84. );
  85. };