ShareLinkPageView.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import React, { useMemo } from 'react';
  2. import type { IPagePopulatedToShowRevision } from '@growi/core';
  3. import dynamic from 'next/dynamic';
  4. import { useShouldExpandContent } from '~/client/services/layout';
  5. import type { RendererConfig } from '~/interfaces/services/renderer';
  6. import type { IShareLinkHasId } from '~/interfaces/share-link';
  7. import { generateSSRViewOptions } from '~/services/renderer/renderer';
  8. import { useIsNotFound } from '~/stores/page';
  9. import { useViewOptions } from '~/stores/renderer';
  10. import loggerFactory from '~/utils/logger';
  11. import { PagePathNavSticky } from './Common/PagePathNav';
  12. import { PageViewLayout } from './Common/PageViewLayout';
  13. import RevisionRenderer from './Page/RevisionRenderer';
  14. import ShareLinkAlert from './Page/ShareLinkAlert';
  15. import type { PageSideContentsProps } from './PageSideContents';
  16. const logger = loggerFactory('growi:Page');
  17. const PageSideContents = dynamic<PageSideContentsProps>(() => import('./PageSideContents').then(mod => mod.PageSideContents), { ssr: false });
  18. const ForbiddenPage = dynamic(() => import('./ForbiddenPage'), { ssr: false });
  19. type Props = {
  20. pagePath: string,
  21. rendererConfig: RendererConfig,
  22. page?: IPagePopulatedToShowRevision,
  23. shareLink?: IShareLinkHasId,
  24. isExpired: boolean,
  25. disableLinkSharing: boolean,
  26. }
  27. export const ShareLinkPageView = (props: Props): JSX.Element => {
  28. const {
  29. pagePath, rendererConfig,
  30. page, shareLink,
  31. isExpired, disableLinkSharing,
  32. } = props;
  33. const { data: isNotFoundMeta } = useIsNotFound();
  34. const { data: viewOptions } = useViewOptions();
  35. const shouldExpandContent = useShouldExpandContent(page);
  36. const isNotFound = isNotFoundMeta || page == null || shareLink == null;
  37. const specialContents = useMemo(() => {
  38. if (disableLinkSharing) {
  39. return <ForbiddenPage isLinkSharingDisabled={props.disableLinkSharing} />;
  40. }
  41. }, [disableLinkSharing, props.disableLinkSharing]);
  42. const headerContents = (
  43. <PagePathNavSticky pageId={page?._id} pagePath={pagePath} />
  44. );
  45. const sideContents = !isNotFound
  46. ? (
  47. <PageSideContents page={page} />
  48. )
  49. : null;
  50. const Contents = () => {
  51. if (isNotFound || page.revision == null) {
  52. return <></>;
  53. }
  54. if (isExpired) {
  55. return (
  56. <>
  57. <h2 className="text-muted mt-4">
  58. <span className="material-symbols-outlined" aria-hidden="true">block</span>
  59. <span> Page is expired</span>
  60. </h2>
  61. </>
  62. );
  63. }
  64. const rendererOptions = viewOptions ?? generateSSRViewOptions(rendererConfig, pagePath);
  65. const markdown = page.revision.body;
  66. return (
  67. <>
  68. <RevisionRenderer rendererOptions={rendererOptions} markdown={markdown} />
  69. </>
  70. );
  71. };
  72. return (
  73. <PageViewLayout
  74. headerContents={headerContents}
  75. sideContents={sideContents}
  76. expandContentWidth={shouldExpandContent}
  77. >
  78. { specialContents }
  79. { specialContents == null && (
  80. <>
  81. { isNotFound && (
  82. <h2 className="text-muted mt-4">
  83. <span className="material-symbols-outlined" aria-hidden="true">block</span>
  84. <span> Page is not found</span>
  85. </h2>
  86. ) }
  87. { !isNotFound && (
  88. <>
  89. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  90. <div className="mb-5">
  91. <Contents />
  92. </div>
  93. </>
  94. ) }
  95. </>
  96. ) }
  97. </PageViewLayout>
  98. );
  99. };