PageView.tsx 5.8 KB

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