DisplaySwitcher.tsx 4.7 KB

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