PageView.tsx 6.2 KB

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