PageView.tsx 6.0 KB

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