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