PageView.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import React, {
  2. useEffect, useMemo, useRef, useState,
  3. } from 'react';
  4. import { type IPagePopulatedToShowRevision, pagePathUtils } from '@growi/core';
  5. import dynamic from 'next/dynamic';
  6. import type { RendererConfig } from '~/interfaces/services/renderer';
  7. import { generateSSRViewOptions } from '~/services/renderer/renderer';
  8. import {
  9. useIsForbidden, useIsIdenticalPath, useIsNotCreatable,
  10. } from '~/stores/context';
  11. import { useSWRxCurrentPage, useIsNotFound } from '~/stores/page';
  12. import { useViewOptions } from '~/stores/renderer';
  13. import { useIsMobile } from '~/stores/ui';
  14. import type { CommentsProps } from '../Comments';
  15. import { MainPane } from '../Layout/MainPane';
  16. import { PageAlerts } from '../PageAlert/PageAlerts';
  17. import { PageContentFooter } from '../PageContentFooter';
  18. import type { PageSideContentsProps } from '../PageSideContents';
  19. import { UserInfo } from '../User/UserInfo';
  20. import type { UsersHomePageFooterProps } from '../UsersHomePageFooter';
  21. import RevisionRenderer from './RevisionRenderer';
  22. import styles from './PageView.module.scss';
  23. const { isUsersHomePage } = pagePathUtils;
  24. const NotCreatablePage = dynamic(() => import('../NotCreatablePage').then(mod => mod.NotCreatablePage), { ssr: false });
  25. const ForbiddenPage = dynamic(() => import('../ForbiddenPage'), { ssr: false });
  26. const NotFoundPage = dynamic(() => import('../NotFoundPage'), { ssr: false });
  27. const PageSideContents = dynamic<PageSideContentsProps>(() => import('../PageSideContents').then(mod => mod.PageSideContents), { ssr: false });
  28. const PageContentsUtilities = dynamic(() => import('./PageContentsUtilities').then(mod => mod.PageContentsUtilities), { ssr: false });
  29. const Comments = dynamic<CommentsProps>(() => import('../Comments').then(mod => mod.Comments), { ssr: false });
  30. const UsersHomePageFooter = dynamic<UsersHomePageFooterProps>(() => import('../UsersHomePageFooter')
  31. .then(mod => mod.UsersHomePageFooter), { ssr: false });
  32. const IdenticalPathPage = dynamic(() => import('../IdenticalPathPage').then(mod => mod.IdenticalPathPage), { ssr: false });
  33. type Props = {
  34. pagePath: string,
  35. rendererConfig: RendererConfig,
  36. initialPage?: IPagePopulatedToShowRevision,
  37. }
  38. export const PageView = (props: Props): JSX.Element => {
  39. const commentsContainerRef = useRef<HTMLDivElement>(null);
  40. const [isCommentsLoaded, setCommentsLoaded] = useState(false);
  41. const {
  42. pagePath, initialPage, rendererConfig,
  43. } = props;
  44. const { data: isIdenticalPathPage } = useIsIdenticalPath();
  45. const { data: isForbidden } = useIsForbidden();
  46. const { data: isNotCreatable } = useIsNotCreatable();
  47. const { data: isNotFoundMeta } = useIsNotFound();
  48. const { data: isMobile } = useIsMobile();
  49. const { data: pageBySWR } = useSWRxCurrentPage();
  50. const { data: viewOptions } = useViewOptions();
  51. const page = pageBySWR ?? initialPage;
  52. const isNotFound = isNotFoundMeta || page?.revision == null;
  53. const isUsersHomePagePath = isUsersHomePage(pagePath);
  54. // *************************** Auto Scroll ***************************
  55. useEffect(() => {
  56. // do nothing if hash is empty
  57. const { hash } = window.location;
  58. if (hash.length === 0) {
  59. return;
  60. }
  61. const targetId = hash.slice(1);
  62. const target = document.getElementById(decodeURIComponent(targetId));
  63. target?.scrollIntoView();
  64. }, [isCommentsLoaded]);
  65. // ******************************* end *******************************
  66. const specialContents = useMemo(() => {
  67. if (isIdenticalPathPage) {
  68. return <IdenticalPathPage />;
  69. }
  70. if (isForbidden) {
  71. return <ForbiddenPage />;
  72. }
  73. if (isNotCreatable) {
  74. return <NotCreatablePage />;
  75. }
  76. }, [isForbidden, isIdenticalPathPage, isNotCreatable]);
  77. const sideContents = !isNotFound && !isNotCreatable
  78. ? (
  79. <PageSideContents page={page} />
  80. )
  81. : null;
  82. const footerContents = !isIdenticalPathPage && !isNotFound
  83. ? (
  84. <>
  85. <div id="comments-container" ref={commentsContainerRef}>
  86. <Comments
  87. pageId={page._id}
  88. pagePath={pagePath}
  89. revision={page.revision}
  90. onLoaded={() => setCommentsLoaded(true)}
  91. />
  92. </div>
  93. {(isUsersHomePagePath && page.creator != null) && (
  94. <UsersHomePageFooter creatorId={page.creator._id} />
  95. )}
  96. <PageContentFooter page={page} />
  97. </>
  98. )
  99. : null;
  100. const Contents = () => {
  101. if (isNotFound) {
  102. return <NotFoundPage path={pagePath} />;
  103. }
  104. const rendererOptions = viewOptions ?? generateSSRViewOptions(rendererConfig, pagePath);
  105. const markdown = page.revision.body;
  106. return (
  107. <>
  108. <PageContentsUtilities />
  109. <RevisionRenderer rendererOptions={rendererOptions} markdown={markdown} />
  110. </>
  111. );
  112. };
  113. return (
  114. <MainPane
  115. sideContents={sideContents}
  116. footerContents={footerContents}
  117. >
  118. <PageAlerts />
  119. {specialContents}
  120. {specialContents == null && (
  121. <>
  122. {(isUsersHomePagePath && page?.creator != null) && <UserInfo author={page.creator} />}
  123. <div className={`mb-5 ${isMobile ? `page-mobile ${styles['page-mobile']}` : ''}`}>
  124. <Contents />
  125. </div>
  126. </>
  127. )}
  128. </MainPane>
  129. );
  130. };