DisplaySwitcher.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 PageEditor from '../PageEditor';
  19. // import PageEditorByHackmd from '../PageEditorByHackmd';
  20. import TableOfContents from '../TableOfContents';
  21. import UserInfo from '../User/UserInfo';
  22. const WIKI_HEADER_LINK = 120;
  23. const { isTopPage } = pagePathUtils;
  24. const DisplaySwitcher = (): JSX.Element => {
  25. const { t } = useTranslation();
  26. const PageEditor = dynamic(() => import('../PageEditor'), { ssr: false });
  27. const EditorNavbarBottom = dynamic(() => import('../PageEditor/EditorNavbarBottom'), { ssr: false });
  28. const HashChanged = dynamic(() => import('../EventListeneres/HashChanged'), { ssr: false });
  29. const ContentLinkButtons = dynamic(() => import('../ContentLinkButtons'), { 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-reverse">
  51. { !isNotFound && !currentPage?.isEmpty && (
  52. <div className="grw-side-contents-container">
  53. <div className="grw-side-contents-sticky-container">
  54. {/* Page list */}
  55. <div className="grw-page-accessories-control">
  56. { currentPagePath != null && !isSharedUser && (
  57. <button
  58. type="button"
  59. className="btn btn-block btn-outline-secondary grw-btn-page-accessories rounded-pill d-flex justify-content-between align-items-center"
  60. onClick={() => openDescendantPageListModal(currentPagePath)}
  61. data-testid="pageListButton"
  62. >
  63. <div className="grw-page-accessories-control-icon">
  64. <PageListIcon />
  65. </div>
  66. {t('page_list')}
  67. <CountBadge count={currentPage?.descendantCount} offset={1} />
  68. </button>
  69. ) }
  70. </div>
  71. {/* Comments */}
  72. {/* { getCommentListDom != null && !isTopPagePath && ( */}
  73. { !isTopPagePath && (
  74. <div className="grw-page-accessories-control mt-2">
  75. <button
  76. type="button"
  77. className="btn btn-block btn-outline-secondary grw-btn-page-accessories rounded-pill d-flex justify-content-between align-items-center"
  78. // onClick={() => smoothScrollIntoView(getCommentListDom, WIKI_HEADER_LINK)}
  79. >
  80. <i className="icon-fw icon-bubbles grw-page-accessories-control-icon"></i>
  81. <span>Comments</span>
  82. <CountBadge count={currentPage?.commentCount} />
  83. </button>
  84. </div>
  85. ) }
  86. <div className="d-none d-lg-block">
  87. <div id="revision-toc" className="revision-toc">
  88. {/* <TableOfContents /> */}
  89. </div>
  90. <ContentLinkButtons />
  91. </div>
  92. </div>
  93. </div>
  94. ) }
  95. <div className="flex-grow-1 flex-basis-0 mw-0">
  96. { isUserPage && <UserInfo pageUser={pageUser} />}
  97. { !isNotFound && <Page /> }
  98. { isNotFound && !isNotCreatable && <NotFoundPage /> }
  99. { isNotFound && isNotCreatable && <NotCreatablePage /> }
  100. </div>
  101. </div>
  102. </TabPane>
  103. { isEditable && (
  104. <TabPane tabId={EditorMode.Editor}>
  105. <div data-testid="page-editor" id="page-editor">
  106. <PageEditor />
  107. </div>
  108. </TabPane>
  109. ) }
  110. { isEditable && (
  111. <TabPane tabId={EditorMode.HackMD}>
  112. <div id="page-editor-with-hackmd">
  113. {/* <PageEditorByHackmd /> */}
  114. </div>
  115. </TabPane>
  116. ) }
  117. </TabContent>
  118. { isEditable && !isViewMode && <EditorNavbarBottom /> }
  119. { isEditable && <HashChanged></HashChanged> }
  120. </>
  121. );
  122. };
  123. export default DisplaySwitcher;