DisplaySwitcher.tsx 5.2 KB

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