PageView.tsx 5.4 KB

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