Comments.tsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import React, { useEffect, useMemo, useRef } from 'react';
  2. import type { IRevisionHasId } from '@growi/core';
  3. import { pagePathUtils } from '@growi/core/dist/utils';
  4. import dynamic from 'next/dynamic';
  5. import { debounce } from 'throttle-debounce';
  6. import { type PageCommentProps } from '~/components/PageComment';
  7. import { useSWRxPageComment } from '~/stores/comment';
  8. import { useIsTrashPage, useSWRMUTxPageInfo } from '~/stores/page';
  9. import { useCurrentUser } from '../stores/context';
  10. import type { CommentEditorProps } from './PageComment/CommentEditor';
  11. const { isTopPage } = pagePathUtils;
  12. const PageComment = dynamic<PageCommentProps>(() => import('~/components/PageComment').then(mod => mod.PageComment), { ssr: false });
  13. const CommentEditor = dynamic<CommentEditorProps>(() => import('./PageComment/CommentEditor').then(mod => mod.CommentEditor), { ssr: false });
  14. export type CommentsProps = {
  15. pageId: string,
  16. pagePath: string,
  17. revision: IRevisionHasId,
  18. onLoaded?: () => void,
  19. }
  20. export const Comments = (props: CommentsProps): JSX.Element => {
  21. const {
  22. pageId, pagePath, revision, onLoaded,
  23. } = props;
  24. const { mutate } = useSWRxPageComment(pageId);
  25. const { trigger: mutatePageInfo } = useSWRMUTxPageInfo(pageId);
  26. const { data: isDeleted } = useIsTrashPage();
  27. const { data: currentUser } = useCurrentUser();
  28. const pageCommentParentRef = useRef<HTMLDivElement>(null);
  29. const onLoadedDebounced = useMemo(() => debounce(500, () => onLoaded?.()), [onLoaded]);
  30. useEffect(() => {
  31. const parent = pageCommentParentRef.current;
  32. if (parent == null) return;
  33. const observer = new MutationObserver(() => {
  34. onLoadedDebounced();
  35. });
  36. observer.observe(parent, { childList: true, subtree: true });
  37. // no cleanup function -- 2023.07.31 Yuki Takei
  38. // see: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe
  39. // > You can call observe() multiple times on the same MutationObserver
  40. // > to watch for changes to different parts of the DOM tree and/or different types of changes.
  41. }, [onLoaded]);
  42. const isTopPagePath = isTopPage(pagePath);
  43. if (pageId == null || isTopPagePath) {
  44. return <></>;
  45. }
  46. const onCommentButtonClickHandler = () => {
  47. mutate();
  48. mutatePageInfo();
  49. };
  50. return (
  51. <div className="page-comments-row mt-5 py-4 d-edit-none d-print-none">
  52. <div className="container-lg">
  53. <div id="page-comments-list" className="page-comments-list" ref={pageCommentParentRef}>
  54. <PageComment
  55. pageId={pageId}
  56. pagePath={pagePath}
  57. revision={revision}
  58. currentUser={currentUser}
  59. isReadOnly={false}
  60. titleAlign="left"
  61. />
  62. </div>
  63. {!isDeleted && (
  64. <div id="page-comment-write">
  65. <CommentEditor
  66. pageId={pageId}
  67. isForNewComment
  68. onCommentButtonClicked={onCommentButtonClickHandler}
  69. revisionId={revision._id}
  70. />
  71. </div>
  72. )}
  73. </div>
  74. </div>
  75. );
  76. };