DisplaySwitcher.tsx 5.7 KB

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