| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- import React, {
- type JSX,
- useCallback,
- useEffect,
- useMemo,
- useRef,
- useState,
- } from 'react';
- import dynamic from 'next/dynamic';
- import type { IPagePopulatedToShowRevision } from '@growi/core';
- import { isUsersHomepage } from '@growi/core/dist/utils/page-path-utils';
- import { useSlidesByFrontmatter } from '@growi/presentation/dist/services';
- import { PagePathNavTitle } from '~/components/Common/PagePathNavTitle';
- import type { RendererConfig } from '~/interfaces/services/renderer';
- import { useShouldExpandContent } from '~/services/layout/use-should-expand-content';
- import { generateSSRViewOptions } from '~/services/renderer/renderer';
- import { useIsNotFound, useSWRxCurrentPage } from '~/stores/page';
- import { useViewOptions } from '~/stores/renderer';
- import {
- useIsForbidden,
- useIsIdenticalPath,
- useIsNotCreatable,
- } from '~/stores-universal/context';
- import { UserInfo } from '../User/UserInfo';
- import { PageAlerts } from './PageAlerts/PageAlerts';
- import { PageContentFooter } from './PageContentFooter';
- import { PageViewLayout } from './PageViewLayout';
- import RevisionRenderer from './RevisionRenderer';
- const NotCreatablePage = dynamic(
- () =>
- import('~/client/components/NotCreatablePage').then(
- (mod) => mod.NotCreatablePage,
- ),
- { ssr: false },
- );
- const ForbiddenPage = dynamic(
- () => import('~/client/components/ForbiddenPage'),
- { ssr: false },
- );
- const NotFoundPage = dynamic(() => import('~/client/components/NotFoundPage'), {
- ssr: false,
- });
- const PageSideContents = dynamic(
- () =>
- import('~/client/components/PageSideContents').then(
- (mod) => mod.PageSideContents,
- ),
- { ssr: false },
- );
- const PageContentsUtilities = dynamic(
- () =>
- import('~/client/components/Page/PageContentsUtilities').then(
- (mod) => mod.PageContentsUtilities,
- ),
- { ssr: false },
- );
- const Comments = dynamic(
- () => import('~/client/components/Comments').then((mod) => mod.Comments),
- { ssr: false },
- );
- const UsersHomepageFooter = dynamic(
- () =>
- import('~/client/components/UsersHomepageFooter').then(
- (mod) => mod.UsersHomepageFooter,
- ),
- { ssr: false },
- );
- const IdenticalPathPage = dynamic(
- () =>
- import('~/client/components/IdenticalPathPage').then(
- (mod) => mod.IdenticalPathPage,
- ),
- { ssr: false },
- );
- const SlideRenderer = dynamic(
- () =>
- import('~/client/components/Page/SlideRenderer').then(
- (mod) => mod.SlideRenderer,
- ),
- { ssr: false },
- );
- type Props = {
- pagePath: string;
- rendererConfig: RendererConfig;
- initialPage?: IPagePopulatedToShowRevision;
- className?: string;
- };
- export const PageView = (props: Props): JSX.Element => {
- const commentsContainerRef = useRef<HTMLDivElement>(null);
- const { pagePath, initialPage, rendererConfig, className } = props;
- const { data: isIdenticalPathPage } = useIsIdenticalPath();
- const { data: isForbidden } = useIsForbidden();
- const { data: isNotCreatable } = useIsNotCreatable();
- const { data: isNotFoundMeta } = useIsNotFound();
- const { data: pageBySWR } = useSWRxCurrentPage();
- const { data: viewOptions } = useViewOptions();
- const page = pageBySWR ?? initialPage;
- const isNotFound = isNotFoundMeta || page == null;
- const isUsersHomepagePath = isUsersHomepage(pagePath);
- const shouldExpandContent = useShouldExpandContent(page);
- const markdown = page?.revision?.body;
- const isSlide = useSlidesByFrontmatter(
- markdown,
- rendererConfig.isEnabledMarp,
- );
- const [currentPageId, setCurrentPageId] = useState<string | undefined>(
- page?._id,
- );
- useEffect(() => {
- if (page?._id !== undefined) {
- setCurrentPageId(page._id);
- }
- }, [page?._id]);
- // *************************** Auto Scroll ***************************
- useEffect(() => {
- if (currentPageId == null) {
- return;
- }
- // do nothing if hash is empty
- const { hash } = window.location;
- if (hash.length === 0) {
- return;
- }
- const contentContainer = document.getElementById(
- 'page-view-content-container',
- );
- if (contentContainer == null) return;
- const targetId = decodeURIComponent(hash.slice(1));
- const target = document.getElementById(targetId);
- if (target != null) {
- target.scrollIntoView();
- return;
- }
- const observer = new MutationObserver(() => {
- const target = document.getElementById(targetId);
- if (target != null) {
- target.scrollIntoView();
- observer.disconnect();
- }
- });
- observer.observe(contentContainer, { childList: true, subtree: true });
- return () => observer.disconnect();
- }, [currentPageId]);
- // ******************************* end *******************************
- const specialContents = useMemo(() => {
- if (isIdenticalPathPage) {
- return <IdenticalPathPage />;
- }
- if (isForbidden) {
- return <ForbiddenPage />;
- }
- if (isNotCreatable) {
- return <NotCreatablePage />;
- }
- }, [isForbidden, isIdenticalPathPage, isNotCreatable]);
- const headerContents = (
- <PagePathNavTitle
- pageId={page?._id}
- pagePath={pagePath}
- isWipPage={page?.wip}
- />
- );
- const sideContents =
- !isNotFound && !isNotCreatable ? <PageSideContents page={page} /> : null;
- const footerContents =
- !isIdenticalPathPage && !isNotFound ? (
- <>
- {isUsersHomepagePath && page.creator != null && (
- <UsersHomepageFooter creatorId={page.creator._id} />
- )}
- <PageContentFooter page={page} />
- </>
- ) : null;
- const Contents = useCallback(() => {
- if (isNotFound || page?.revision == null) {
- return <NotFoundPage path={pagePath} />;
- }
- const markdown = page.revision.body;
- const rendererOptions =
- viewOptions ?? generateSSRViewOptions(rendererConfig, pagePath);
- return (
- <>
- <PageContentsUtilities />
- <div className="flex-expand-vert justify-content-between">
- {isSlide != null ? (
- <SlideRenderer marp={isSlide.marp} markdown={markdown} />
- ) : (
- <RevisionRenderer
- rendererOptions={rendererOptions}
- markdown={markdown}
- />
- )}
- {!isIdenticalPathPage && !isNotFound && (
- <div id="comments-container" ref={commentsContainerRef}>
- <Comments
- pageId={page._id}
- pagePath={pagePath}
- revision={page.revision}
- />
- </div>
- )}
- </div>
- </>
- );
- }, [
- isNotFound,
- page?.revision,
- page?._id,
- rendererConfig,
- pagePath,
- viewOptions,
- isSlide,
- isIdenticalPathPage,
- ]);
- return (
- <PageViewLayout
- className={className}
- headerContents={headerContents}
- sideContents={sideContents}
- footerContents={footerContents}
- expandContentWidth={shouldExpandContent}
- >
- <PageAlerts />
- {specialContents}
- {specialContents == null && (
- <>
- {isUsersHomepagePath && page?.creator != null && (
- <UserInfo author={page.creator} />
- )}
- <div id="page-view-content-container" className="flex-expand-vert">
- <Contents />
- </div>
- </>
- )}
- </PageViewLayout>
- );
- };
|