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