PageAccessoriesModal.jsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import React, { useCallback, useMemo, useState } from 'react';
  2. import PropTypes from 'prop-types';
  3. import {
  4. Modal, ModalBody, ModalHeader, TabContent, TabPane,
  5. } from 'reactstrap';
  6. import { withTranslation } from 'react-i18next';
  7. import PageListIcon from './Icons/PageListIcon';
  8. import TimeLineIcon from './Icons/TimeLineIcon';
  9. import HistoryIcon from './Icons/HistoryIcon';
  10. import AttachmentIcon from './Icons/AttachmentIcon';
  11. import ShareLinkIcon from './Icons/ShareLinkIcon';
  12. import { withUnstatedContainers } from './UnstatedUtils';
  13. import PageAccessoriesContainer from '../services/PageAccessoriesContainer';
  14. import PageAttachment from './PageAttachment';
  15. import PageTimeline from './PageTimeline';
  16. import PageList from './PageList';
  17. import PageHistory from './PageHistory';
  18. import ShareLink from './ShareLink/ShareLink';
  19. import { CustomNav } from './CustomNavigation';
  20. import ExpandOrContractButton from './ExpandOrContractButton';
  21. const PageAccessoriesModal = (props) => {
  22. const {
  23. t, pageAccessoriesContainer, onClose, isGuestUser, isSharedUser,
  24. } = props;
  25. const { switchActiveTab } = pageAccessoriesContainer;
  26. const { activeTab, activeComponents } = pageAccessoriesContainer.state;
  27. const [isWindowExpanded, setIsWindowExpanded] = useState(false);
  28. const navTabMapping = useMemo(() => {
  29. return {
  30. pagelist: {
  31. Icon: PageListIcon,
  32. i18n: t('page_list'),
  33. index: 0,
  34. isLinkEnabled: v => !isSharedUser,
  35. },
  36. timeline: {
  37. Icon: TimeLineIcon,
  38. i18n: t('Timeline View'),
  39. index: 1,
  40. isLinkEnabled: v => !isSharedUser,
  41. },
  42. pageHistory: {
  43. Icon: HistoryIcon,
  44. i18n: t('History'),
  45. index: 2,
  46. isLinkEnabled: v => !isSharedUser,
  47. },
  48. attachment: {
  49. Icon: AttachmentIcon,
  50. i18n: t('attachment_data'),
  51. index: 3,
  52. },
  53. shareLink: {
  54. Icon: ShareLinkIcon,
  55. i18n: t('share_links.share_link_management'),
  56. index: 4,
  57. isLinkEnabled: v => !isGuestUser || !isSharedUser,
  58. },
  59. };
  60. }, [t, isGuestUser, isSharedUser]);
  61. const closeModalHandler = useCallback(() => {
  62. if (onClose == null) {
  63. return;
  64. }
  65. onClose();
  66. }, [onClose]);
  67. const expandWindow = () => {
  68. setIsWindowExpanded(true);
  69. };
  70. const contractWindow = () => {
  71. setIsWindowExpanded(false);
  72. };
  73. const buttons = (
  74. <span>
  75. {/* change order because of `float: right` by '.close' class */}
  76. <button type="button" className="close" onClick={closeModalHandler} aria-label="Close">
  77. <span aria-hidden="true">&times;</span>
  78. </button>
  79. <ExpandOrContractButton
  80. isWindowExpanded={isWindowExpanded}
  81. expandWindow={expandWindow}
  82. contractWindow={contractWindow}
  83. />
  84. </span>
  85. );
  86. return (
  87. <React.Fragment>
  88. <Modal size="xl" isOpen={props.isOpen} toggle={closeModalHandler} className={`grw-page-accessories-modal ${isWindowExpanded && 'grw-modal-expanded'} `}>
  89. <ModalHeader className="p-0" toggle={closeModalHandler} close={buttons}>
  90. <CustomNav activeTab={activeTab} navTabMapping={navTabMapping} onNavSelected={switchActiveTab} hideBorderBottom />
  91. </ModalHeader>
  92. <ModalBody className="overflow-auto grw-modal-body-style p-0">
  93. {/* Do not use CustomTabContent because of performance problem:
  94. the 'navTabMapping[tabId].Content' for PageAccessoriesModal depends on activeComponents */}
  95. <TabContent activeTab={activeTab} className="p-5">
  96. <TabPane tabId="pagelist">
  97. {activeComponents.has('pagelist') && <PageList />}
  98. </TabPane>
  99. <TabPane tabId="timeline">
  100. {activeComponents.has('timeline') && <PageTimeline /> }
  101. </TabPane>
  102. <TabPane tabId="pageHistory">
  103. <div className="overflow-auto">
  104. {activeComponents.has('pageHistory') && <PageHistory /> }
  105. </div>
  106. </TabPane>
  107. <TabPane tabId="attachment">
  108. {activeComponents.has('attachment') && <PageAttachment />}
  109. </TabPane>
  110. {!isGuestUser && (
  111. <TabPane tabId="shareLink">
  112. {activeComponents.has('shareLink') && <ShareLink />}
  113. </TabPane>
  114. )}
  115. </TabContent>
  116. </ModalBody>
  117. </Modal>
  118. </React.Fragment>
  119. );
  120. };
  121. /**
  122. * Wrapper component for using unstated
  123. */
  124. const PageAccessoriesModalWrapper = withUnstatedContainers(PageAccessoriesModal, [PageAccessoriesContainer]);
  125. PageAccessoriesModal.propTypes = {
  126. t: PropTypes.func.isRequired, // i18next
  127. pageAccessoriesContainer: PropTypes.instanceOf(PageAccessoriesContainer).isRequired,
  128. isGuestUser: PropTypes.bool.isRequired,
  129. isSharedUser: PropTypes.bool.isRequired,
  130. isOpen: PropTypes.bool.isRequired,
  131. onClose: PropTypes.func,
  132. };
  133. export default withTranslation()(PageAccessoriesModalWrapper);