DisplaySwitcher.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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,
  8. } from '~/stores/context';
  9. import { useDescendantsPageListModal } from '~/stores/modal';
  10. import { useSWRxPageByPath } 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 Editor 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: isUserPage } = useIsUserPage();
  32. const { data: isEditable } = useIsEditable();
  33. const { data: pageUser } = usePageUser();
  34. const { data: currentPage } = useSWRxPageByPath(currentPath);
  35. const { data: editorMode } = useEditorMode();
  36. const { open: openDescendantPageListModal } = useDescendantsPageListModal();
  37. const isPageExist = currentPageId != null;
  38. const isViewMode = editorMode === EditorMode.View;
  39. const isTopPagePath = isTopPage(currentPath ?? '');
  40. return (
  41. <>
  42. <TabContent activeTab={editorMode}>
  43. <TabPane tabId={EditorMode.View}>
  44. <div className="d-flex flex-column flex-lg-row-reverse">
  45. { isPageExist && (
  46. <div className="grw-side-contents-container">
  47. <div className="grw-side-contents-sticky-container">
  48. {/* Page list */}
  49. <div className="grw-page-accessories-control">
  50. { currentPath != null && !isSharedUser && (
  51. <button
  52. type="button"
  53. className="btn btn-block btn-outline-secondary grw-btn-page-accessories rounded-pill d-flex justify-content-between align-items-center"
  54. onClick={() => openDescendantPageListModal(currentPath)}
  55. >
  56. <div className="grw-page-accessories-control-icon">
  57. <PageListIcon />
  58. </div>
  59. {t('page_list')}
  60. {currentPage?.descendantCount != null && <CountBadge count={currentPage.descendantCount + 1} />}
  61. </button>
  62. ) }
  63. </div>
  64. {/* Comments */}
  65. { getCommentListDom != null && !isTopPagePath && (
  66. <div className="grw-page-accessories-control mt-2">
  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. {currentPage?.commentCount != null && <CountBadge count={currentPage.commentCount} />}
  75. </button>
  76. </div>
  77. ) }
  78. <div className="d-none d-lg-block">
  79. <div id="revision-toc" className="revision-toc">
  80. <TableOfContents />
  81. </div>
  82. <ContentLinkButtons />
  83. </div>
  84. </div>
  85. </div>
  86. ) }
  87. <div className="flex-grow-1 flex-basis-0 mw-0">
  88. { isUserPage && <UserInfo pageUser={pageUser} />}
  89. <Page />
  90. </div>
  91. </div>
  92. </TabPane>
  93. { isEditable && (
  94. <TabPane tabId={EditorMode.Editor}>
  95. <div data-testid="page-editor" id="page-editor">
  96. <Editor />
  97. </div>
  98. </TabPane>
  99. ) }
  100. { isEditable && (
  101. <TabPane tabId={EditorMode.HackMD}>
  102. <div id="page-editor-with-hackmd">
  103. <PageEditorByHackmd />
  104. </div>
  105. </TabPane>
  106. ) }
  107. </TabContent>
  108. { isEditable && !isViewMode && <EditorNavbarBottom /> }
  109. { isEditable && <HashChanged></HashChanged> }
  110. </>
  111. );
  112. };
  113. export default DisplaySwitcher;