DisplaySwitcher.tsx 4.6 KB

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