DisplaySwitcher.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import React, { useMemo } from 'react';
  2. import { pagePathUtils } from '@growi/core';
  3. import { useTranslation } from 'next-i18next';
  4. import dynamic from 'next/dynamic';
  5. import { Link } from 'react-scroll';
  6. import { DEFAULT_AUTO_SCROLL_OPTS } from '~/client/util/smooth-scroll';
  7. import {
  8. useCurrentPagePath, useIsSharedUser, useIsEditable, useShareLinkId, useIsNotFound,
  9. } from '~/stores/context';
  10. import { useDescendantsPageListModal } from '~/stores/modal';
  11. import { useSWRxCurrentPage } from '~/stores/page';
  12. import { EditorMode, useEditorMode } from '~/stores/ui';
  13. import CountBadge from '../Common/CountBadge';
  14. import { ContentLinkButtonsProps } from '../ContentLinkButtons';
  15. import CustomTabContent from '../CustomNavigation/CustomTabContent';
  16. import PageListIcon from '../Icons/PageListIcon';
  17. import { Page } from '../Page';
  18. import TableOfContents from '../TableOfContents';
  19. import { UserInfoProps } from '../User/UserInfo';
  20. import styles from './DisplaySwitcher.module.scss';
  21. const { isTopPage, isUsersHomePage } = pagePathUtils;
  22. const PageEditor = dynamic(() => import('../PageEditor'), { ssr: false });
  23. const PageEditorByHackmd = dynamic(() => import('../PageEditorByHackmd').then(mod => mod.PageEditorByHackmd), { ssr: false });
  24. const EditorNavbarBottom = dynamic(() => import('../PageEditor/EditorNavbarBottom'), { ssr: false });
  25. const HashChanged = dynamic(() => import('../EventListeneres/HashChanged'), { ssr: false });
  26. const ContentLinkButtons = dynamic<ContentLinkButtonsProps>(() => import('../ContentLinkButtons').then(mod => mod.ContentLinkButtons), { ssr: false });
  27. const NotFoundPage = dynamic(() => import('../NotFoundPage'), { ssr: false });
  28. const UserInfo = dynamic<UserInfoProps>(() => import('../User/UserInfo').then(mod => mod.UserInfo), { ssr: false });
  29. const PageView = React.memo((): JSX.Element => {
  30. const { t } = useTranslation();
  31. const { data: currentPagePath } = useCurrentPagePath();
  32. const { data: isSharedUser } = useIsSharedUser();
  33. const { data: shareLinkId } = useShareLinkId();
  34. const { data: isNotFound } = useIsNotFound();
  35. const { data: currentPage } = useSWRxCurrentPage(shareLinkId ?? undefined);
  36. const { open: openDescendantPageListModal } = useDescendantsPageListModal();
  37. const isTopPagePath = isTopPage(currentPagePath ?? '');
  38. const isUsersHomePagePath = isUsersHomePage(currentPagePath ?? '');
  39. return (
  40. <div className="d-flex flex-column flex-lg-row">
  41. <div className="flex-grow-1 flex-basis-0 mw-0">
  42. { isUsersHomePagePath && <UserInfo author={currentPage?.creator} /> }
  43. { !isNotFound && <Page /> }
  44. { isNotFound && <NotFoundPage /> }
  45. </div>
  46. { !isNotFound && (
  47. <div className="grw-side-contents-container">
  48. <div className="grw-side-contents-sticky-container">
  49. {/* Page list */}
  50. <div className={`grw-page-accessories-control ${styles['grw-page-accessories-control']}`}>
  51. { currentPagePath != null && !isSharedUser && (
  52. <button
  53. type="button"
  54. className="btn btn-block btn-outline-secondary grw-btn-page-accessories rounded-pill d-flex justify-content-between align-items-center"
  55. onClick={() => openDescendantPageListModal(currentPagePath)}
  56. data-testid="pageListButton"
  57. >
  58. <div className="grw-page-accessories-control-icon">
  59. <PageListIcon />
  60. </div>
  61. {t('page_list')}
  62. <CountBadge count={currentPage?.descendantCount} offset={1} />
  63. </button>
  64. ) }
  65. </div>
  66. {/* Comments */}
  67. { !isTopPagePath && (
  68. <div className={`mt-2 grw-page-accessories-control ${styles['grw-page-accessories-control']}`}>
  69. <Link to={'page-comments'} offset={-100} {...DEFAULT_AUTO_SCROLL_OPTS}>
  70. <button
  71. type="button"
  72. className="btn btn-block btn-outline-secondary grw-btn-page-accessories rounded-pill d-flex justify-content-between align-items-center"
  73. >
  74. <i className="icon-fw icon-bubbles grw-page-accessories-control-icon"></i>
  75. <span>Comments</span>
  76. <CountBadge count={currentPage?.commentCount} />
  77. </button>
  78. </Link>
  79. </div>
  80. ) }
  81. <div className="d-none d-lg-block">
  82. <TableOfContents />
  83. { isUsersHomePagePath && <ContentLinkButtons author={currentPage?.creator} /> }
  84. </div>
  85. </div>
  86. </div>
  87. ) }
  88. </div>
  89. );
  90. });
  91. PageView.displayName = 'PageView';
  92. const DisplaySwitcher = React.memo((): JSX.Element => {
  93. const { data: isEditable } = useIsEditable();
  94. const { data: editorMode = EditorMode.View } = useEditorMode();
  95. const isViewMode = editorMode === EditorMode.View;
  96. const navTabMapping = useMemo(() => {
  97. return {
  98. [EditorMode.View]: {
  99. Content: () => (
  100. <div data-testid="page-view" id="page-view">
  101. <PageView />
  102. </div>
  103. ),
  104. },
  105. [EditorMode.Editor]: {
  106. Content: () => (
  107. isEditable
  108. ? (
  109. <div data-testid="page-editor" id="page-editor">
  110. <PageEditor />
  111. </div>
  112. )
  113. : <></>
  114. ),
  115. },
  116. [EditorMode.HackMD]: {
  117. Content: () => (
  118. isEditable
  119. ? (
  120. <div id="page-editor-with-hackmd">
  121. <PageEditorByHackmd />
  122. </div>
  123. )
  124. : <></>
  125. ),
  126. },
  127. };
  128. }, [isEditable]);
  129. return (
  130. <>
  131. <CustomTabContent activeTab={editorMode} navTabMapping={navTabMapping} />
  132. { isEditable && !isViewMode && <EditorNavbarBottom /> }
  133. { isEditable && <HashChanged></HashChanged> }
  134. </>
  135. );
  136. });
  137. DisplaySwitcher.displayName = 'DisplaySwitcher';
  138. export default DisplaySwitcher;