PageView.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import { type JSX, memo, useCallback, useEffect, useMemo, useRef } from 'react';
  2. import dynamic from 'next/dynamic';
  3. import { isUsersHomepage } from '@growi/core/dist/utils/page-path-utils';
  4. import { useSlidesByFrontmatter } from '@growi/presentation/dist/services';
  5. import { PagePathNavTitle } from '~/components/Common/PagePathNavTitle';
  6. import type { RendererConfig } from '~/interfaces/services/renderer';
  7. import { useShouldExpandContent } from '~/services/layout/use-should-expand-content';
  8. import { generateSSRViewOptions } from '~/services/renderer/renderer';
  9. import {
  10. useCurrentPageData,
  11. useCurrentPageId,
  12. useIsForbidden,
  13. useIsIdenticalPath,
  14. useIsNotCreatable,
  15. usePageNotFound,
  16. } from '~/states/page';
  17. import { useViewOptions } from '~/stores/renderer';
  18. import { UserInfo } from '../User/UserInfo';
  19. import { PageAlerts } from './PageAlerts/PageAlerts';
  20. import { PageContentFooter } from './PageContentFooter';
  21. import { PageViewLayout } from './PageViewLayout';
  22. import RevisionRenderer from './RevisionRenderer';
  23. // biome-ignore-start lint/style/noRestrictedImports: no-problem dynamic import
  24. const NotCreatablePage = dynamic(
  25. () =>
  26. import('~/client/components/NotCreatablePage').then(
  27. (mod) => mod.NotCreatablePage,
  28. ),
  29. { ssr: false },
  30. );
  31. const ForbiddenPage = dynamic(
  32. () => import('~/client/components/ForbiddenPage'),
  33. { ssr: false },
  34. );
  35. const NotFoundPage = dynamic(() => import('~/client/components/NotFoundPage'), {
  36. ssr: false,
  37. });
  38. const PageSideContents = dynamic(
  39. () =>
  40. import('~/client/components/PageSideContents').then(
  41. (mod) => mod.PageSideContents,
  42. ),
  43. { ssr: false },
  44. );
  45. const PageContentsUtilities = dynamic(
  46. () =>
  47. import('~/client/components/Page/PageContentsUtilities').then(
  48. (mod) => mod.PageContentsUtilities,
  49. ),
  50. { ssr: false },
  51. );
  52. const Comments = dynamic(
  53. () => import('~/client/components/Comments').then((mod) => mod.Comments),
  54. { ssr: false },
  55. );
  56. const UsersHomepageFooter = dynamic(
  57. () =>
  58. import('~/client/components/UsersHomepageFooter').then(
  59. (mod) => mod.UsersHomepageFooter,
  60. ),
  61. { ssr: false },
  62. );
  63. const IdenticalPathPage = dynamic(
  64. () =>
  65. import('~/client/components/IdenticalPathPage').then(
  66. (mod) => mod.IdenticalPathPage,
  67. ),
  68. { ssr: false },
  69. );
  70. const SlideRenderer = dynamic(
  71. () =>
  72. import('~/client/components/Page/SlideRenderer').then(
  73. (mod) => mod.SlideRenderer,
  74. ),
  75. { ssr: false },
  76. );
  77. // biome-ignore-end lint/style/noRestrictedImports: no-problem dynamic import
  78. type Props = {
  79. pagePath: string;
  80. rendererConfig: RendererConfig;
  81. className?: string;
  82. };
  83. export const PageView = memo((props: Props): JSX.Element => {
  84. const renderStartTime = performance.now();
  85. const commentsContainerRef = useRef<HTMLDivElement>(null);
  86. const { pagePath, rendererConfig, className } = props;
  87. const currentPageId = useCurrentPageId();
  88. const isIdenticalPathPage = useIsIdenticalPath();
  89. const isForbidden = useIsForbidden();
  90. const isNotCreatable = useIsNotCreatable();
  91. const isNotFoundMeta = usePageNotFound();
  92. const page = useCurrentPageData();
  93. const { data: viewOptions } = useViewOptions();
  94. // DEBUG: Log PageView render start
  95. console.log('[PAGEVIEW-DEBUG] PageView render started:', {
  96. pagePath,
  97. currentPageId,
  98. pageId: page?._id,
  99. timestamp: new Date().toISOString(),
  100. renderStartTime,
  101. });
  102. const isNotFound = isNotFoundMeta || page == null;
  103. const isUsersHomepagePath = isUsersHomepage(pagePath);
  104. const shouldExpandContent = useShouldExpandContent(page);
  105. const markdown = page?.revision?.body;
  106. const isSlide = useSlidesByFrontmatter(
  107. markdown,
  108. rendererConfig.isEnabledMarp,
  109. );
  110. // *************************** Auto Scroll ***************************
  111. useEffect(() => {
  112. const scrollEffectStartTime = performance.now();
  113. console.log('[PAGEVIEW-DEBUG] Auto scroll effect triggered:', {
  114. currentPageId,
  115. hash: window.location.hash,
  116. timestamp: new Date().toISOString(),
  117. effectStartTime: scrollEffectStartTime,
  118. });
  119. if (currentPageId == null) {
  120. console.log('[PAGEVIEW-DEBUG] Auto scroll skipped - no currentPageId');
  121. return;
  122. }
  123. // do nothing if hash is empty
  124. const { hash } = window.location;
  125. if (hash.length === 0) {
  126. console.log('[PAGEVIEW-DEBUG] Auto scroll skipped - no hash');
  127. return;
  128. }
  129. const contentContainer = document.getElementById(
  130. 'page-view-content-container',
  131. );
  132. if (contentContainer == null) return;
  133. const targetId = decodeURIComponent(hash.slice(1));
  134. const target = document.getElementById(targetId);
  135. if (target != null) {
  136. target.scrollIntoView();
  137. return;
  138. }
  139. const observer = new MutationObserver(() => {
  140. const target = document.getElementById(targetId);
  141. if (target != null) {
  142. target.scrollIntoView();
  143. observer.disconnect();
  144. }
  145. });
  146. observer.observe(contentContainer, { childList: true, subtree: true });
  147. return () => observer.disconnect();
  148. }, [currentPageId]);
  149. // ******************************* end *******************************
  150. const specialContents = useMemo(() => {
  151. if (isIdenticalPathPage) {
  152. return <IdenticalPathPage />;
  153. }
  154. if (isForbidden) {
  155. return <ForbiddenPage />;
  156. }
  157. if (isNotCreatable) {
  158. return <NotCreatablePage />;
  159. }
  160. }, [isForbidden, isIdenticalPathPage, isNotCreatable]);
  161. const headerContents = (
  162. <PagePathNavTitle
  163. pageId={page?._id}
  164. pagePath={pagePath}
  165. isWipPage={page?.wip}
  166. />
  167. );
  168. const sideContents =
  169. !isNotFound && !isNotCreatable ? <PageSideContents page={page} /> : null;
  170. const footerContents =
  171. !isIdenticalPathPage && !isNotFound ? (
  172. <>
  173. {isUsersHomepagePath && page.creator != null && (
  174. <UsersHomepageFooter creatorId={page.creator._id} />
  175. )}
  176. <PageContentFooter page={page} />
  177. </>
  178. ) : null;
  179. const Contents = useCallback(() => {
  180. const contentsRenderStartTime = performance.now();
  181. console.log('[PAGEVIEW-DEBUG] Contents component render started:', {
  182. isNotFound,
  183. hasPage: page != null,
  184. hasRevision: page?.revision != null,
  185. pageId: page?._id,
  186. timestamp: new Date().toISOString(),
  187. contentsRenderStartTime,
  188. });
  189. if (isNotFound || page?.revision == null) {
  190. console.log('[PAGEVIEW-DEBUG] Rendering NotFoundPage');
  191. return <NotFoundPage path={pagePath} />;
  192. }
  193. const markdown = page.revision.body;
  194. const rendererOptions =
  195. viewOptions ?? generateSSRViewOptions(rendererConfig, pagePath);
  196. console.log('[PAGEVIEW-DEBUG] Rendering page content:', {
  197. markdownLength: markdown?.length,
  198. hasViewOptions: viewOptions != null,
  199. isSlide: isSlide != null,
  200. renderDuration: performance.now() - contentsRenderStartTime,
  201. });
  202. return (
  203. <>
  204. <PageContentsUtilities />
  205. <div className="flex-expand-vert justify-content-between">
  206. {isSlide != null ? (
  207. <SlideRenderer marp={isSlide.marp} markdown={markdown} />
  208. ) : (
  209. <RevisionRenderer
  210. rendererOptions={rendererOptions}
  211. markdown={markdown}
  212. />
  213. )}
  214. {!isIdenticalPathPage && !isNotFound && (
  215. <div id="comments-container" ref={commentsContainerRef}>
  216. <Comments
  217. pageId={page._id}
  218. pagePath={pagePath}
  219. revision={page.revision}
  220. />
  221. </div>
  222. )}
  223. </div>
  224. </>
  225. );
  226. }, [
  227. isNotFound,
  228. page?.revision,
  229. page?._id,
  230. rendererConfig,
  231. pagePath,
  232. viewOptions,
  233. isSlide,
  234. isIdenticalPathPage,
  235. page,
  236. ]);
  237. // DEBUG: Log final render completion time
  238. const renderEndTime = performance.now();
  239. console.log('[PAGEVIEW-DEBUG] PageView render completed:', {
  240. pagePath,
  241. currentPageId,
  242. pageId: page?._id,
  243. totalRenderDuration: renderEndTime - renderStartTime,
  244. timestamp: new Date().toISOString(),
  245. });
  246. return (
  247. <PageViewLayout
  248. className={className}
  249. headerContents={headerContents}
  250. sideContents={sideContents}
  251. footerContents={footerContents}
  252. expandContentWidth={shouldExpandContent}
  253. >
  254. <PageAlerts />
  255. {specialContents}
  256. {specialContents == null && (
  257. <>
  258. {isUsersHomepagePath && page?.creator != null && (
  259. <UserInfo author={page.creator} />
  260. )}
  261. <div id="page-view-content-container" className="flex-expand-vert">
  262. <Contents />
  263. </div>
  264. </>
  265. )}
  266. </PageViewLayout>
  267. );
  268. });