PageAccessoriesModal.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import React, { useEffect, useMemo, useState } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import {
  4. Modal, ModalBody, ModalHeader,
  5. } from 'reactstrap';
  6. import {
  7. useDisableLinkSharing, useIsGuestUser, useIsReadOnlyUser, useIsSharedUser,
  8. } from '~/stores/context';
  9. import { usePageAccessoriesModal, PageAccessoriesModalContents } from '~/stores/modal';
  10. import { CustomNavTab } from './CustomNavigation/CustomNav';
  11. import CustomTabContent from './CustomNavigation/CustomTabContent';
  12. import ExpandOrContractButton from './ExpandOrContractButton';
  13. import AttachmentIcon from './Icons/AttachmentIcon';
  14. import HistoryIcon from './Icons/HistoryIcon';
  15. import ShareLinkIcon from './Icons/ShareLinkIcon';
  16. import PageAttachment from './PageAttachment';
  17. import { PageHistory, getQueryParam } from './PageHistory';
  18. import ShareLink from './ShareLink/ShareLink';
  19. import styles from './PageAccessoriesModal.module.scss';
  20. const PageAccessoriesModal = (): JSX.Element => {
  21. const { t } = useTranslation();
  22. const [activeTab, setActiveTab] = useState<PageAccessoriesModalContents>();
  23. const [sourceRevisionId, setSourceRevisionId] = useState<string>();
  24. const [targetRevisionId, setTargetRevisionId] = useState<string>();
  25. const [isWindowExpanded, setIsWindowExpanded] = useState(false);
  26. const { data: isSharedUser } = useIsSharedUser();
  27. const { data: isGuestUser } = useIsGuestUser();
  28. const { data: isReadOnlyUser } = useIsReadOnlyUser();
  29. const { data: isLinkSharingDisabled } = useDisableLinkSharing();
  30. const { data: status, mutate, close } = usePageAccessoriesModal();
  31. // activate tab when open
  32. useEffect(() => {
  33. if (status == null) return;
  34. const { isOpened, activatedContents } = status;
  35. if (isOpened && activatedContents != null) {
  36. setActiveTab(activatedContents);
  37. }
  38. }, [status]);
  39. // Set sourceRevisionId and targetRevisionId as state with valid object id string
  40. useEffect(() => {
  41. const queryParams = getQueryParam();
  42. // https://regex101.com/r/YHTDsr/1
  43. const regex = /^([0-9a-f]{24})...([0-9a-f]{24})$/i;
  44. if (queryParams == null || !regex.test(queryParams)) {
  45. return;
  46. }
  47. const matches = queryParams.match(regex);
  48. if (matches == null) {
  49. return;
  50. }
  51. const [, sourceRevisionId, targetRevisionId] = matches;
  52. setSourceRevisionId(sourceRevisionId);
  53. setTargetRevisionId(targetRevisionId);
  54. mutate({ isOpened: true });
  55. }, [mutate]);
  56. const navTabMapping = useMemo(() => {
  57. return {
  58. [PageAccessoriesModalContents.PageHistory]: {
  59. Icon: HistoryIcon,
  60. Content: () => {
  61. return <PageHistory onClose={close} sourceRevisionId={sourceRevisionId} targetRevisionId={targetRevisionId}/>;
  62. },
  63. i18n: t('History'),
  64. isLinkEnabled: () => !isGuestUser && !isSharedUser,
  65. },
  66. [PageAccessoriesModalContents.Attachment]: {
  67. Icon: AttachmentIcon,
  68. Content: () => {
  69. return <PageAttachment />;
  70. },
  71. i18n: t('attachment_data'),
  72. },
  73. [PageAccessoriesModalContents.ShareLink]: {
  74. Icon: ShareLinkIcon,
  75. Content: () => {
  76. return <ShareLink />;
  77. },
  78. i18n: t('share_links.share_link_management'),
  79. isLinkEnabled: () => !isGuestUser && !isReadOnlyUser && !isSharedUser && !isLinkSharingDisabled,
  80. },
  81. };
  82. }, [t, close, sourceRevisionId, targetRevisionId, isGuestUser, isReadOnlyUser, isSharedUser, isLinkSharingDisabled]);
  83. const buttons = useMemo(() => (
  84. <div className="d-flex flex-nowrap">
  85. <ExpandOrContractButton
  86. isWindowExpanded={isWindowExpanded}
  87. expandWindow={() => setIsWindowExpanded(true)}
  88. contractWindow={() => setIsWindowExpanded(false)}
  89. />
  90. <button type="button" className="close" onClick={close} aria-label="Close">
  91. <span aria-hidden="true">&times;</span>
  92. </button>
  93. </div>
  94. ), [close, isWindowExpanded]);
  95. if (status == null || activeTab == null) {
  96. return <></>;
  97. }
  98. const { isOpened } = status;
  99. return (
  100. <Modal
  101. size="xl"
  102. isOpen={isOpened}
  103. toggle={close}
  104. data-testid="page-accessories-modal"
  105. className={`grw-page-accessories-modal ${styles['grw-page-accessories-modal']} ${isWindowExpanded ? 'grw-modal-expanded' : ''} `}
  106. >
  107. <ModalHeader className="p-0" toggle={close} close={buttons}>
  108. <CustomNavTab
  109. activeTab={activeTab}
  110. navTabMapping={navTabMapping}
  111. breakpointToHideInactiveTabsDown="md"
  112. onNavSelected={(v: PageAccessoriesModalContents) => {
  113. setActiveTab(v);
  114. }}
  115. hideBorderBottom
  116. />
  117. </ModalHeader>
  118. <ModalBody className="overflow-auto grw-modal-body-style">
  119. <CustomTabContent activeTab={activeTab} navTabMapping={navTabMapping} />
  120. </ModalBody>
  121. </Modal>
  122. );
  123. };
  124. export default PageAccessoriesModal;