DescendantsPageListModal.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import React, { useState, useMemo, useEffect } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import dynamic from 'next/dynamic';
  4. import { useRouter } from 'next/router';
  5. import Modal from 'reactstrap/es/Modal';
  6. import ModalBody from 'reactstrap/es/ModalBody';
  7. import ModalHeader from 'reactstrap/es/ModalHeader';
  8. import { useIsSharedUser } from '~/stores/context';
  9. import { useDescendantsPageListModal } from '~/stores/modal';
  10. import { CustomNavTab } from './CustomNavigation/CustomNav';
  11. import CustomTabContent from './CustomNavigation/CustomTabContent';
  12. import { DescendantsPageListProps } from './DescendantsPageList';
  13. import ExpandOrContractButton from './ExpandOrContractButton';
  14. import PageListIcon from './Icons/PageListIcon';
  15. import TimeLineIcon from './Icons/TimeLineIcon';
  16. import styles from './DescendantsPageListModal.module.scss';
  17. const DescendantsPageList = dynamic<DescendantsPageListProps>(() => import('./DescendantsPageList').then(mod => mod.DescendantsPageList), { ssr: false });
  18. const PageTimeline = dynamic(() => import('./PageTimeline').then(mod => mod.PageTimeline), { ssr: false });
  19. export const DescendantsPageListModal = (): JSX.Element => {
  20. const { t } = useTranslation();
  21. const [activeTab, setActiveTab] = useState('pagelist');
  22. const [isWindowExpanded, setIsWindowExpanded] = useState(false);
  23. const { data: isSharedUser } = useIsSharedUser();
  24. const { data: status, close } = useDescendantsPageListModal();
  25. const { events } = useRouter();
  26. useEffect(() => {
  27. events.on('routeChangeStart', close);
  28. return () => {
  29. events.off('routeChangeStart', close);
  30. };
  31. }, [close, events]);
  32. const navTabMapping = useMemo(() => {
  33. return {
  34. pagelist: {
  35. Icon: PageListIcon,
  36. Content: () => {
  37. if (status == null || status.path == null || !status.isOpened) {
  38. return <></>;
  39. }
  40. return <DescendantsPageList path={status.path} />;
  41. },
  42. i18n: t('page_list'),
  43. isLinkEnabled: () => !isSharedUser,
  44. },
  45. timeline: {
  46. Icon: TimeLineIcon,
  47. Content: () => {
  48. if (status == null || !status.isOpened) {
  49. return <></>;
  50. }
  51. return <PageTimeline />;
  52. },
  53. i18n: t('Timeline View'),
  54. isLinkEnabled: () => !isSharedUser,
  55. },
  56. };
  57. }, [isSharedUser, status, t]);
  58. const buttons = useMemo(() => (
  59. <div className="d-flex flex-nowrap">
  60. <ExpandOrContractButton
  61. isWindowExpanded={isWindowExpanded}
  62. expandWindow={() => setIsWindowExpanded(true)}
  63. contractWindow={() => setIsWindowExpanded(false)}
  64. />
  65. <button type="button" className="close" onClick={close} aria-label="Close">
  66. <span aria-hidden="true">&times;</span>
  67. </button>
  68. </div>
  69. ), [close, isWindowExpanded]);
  70. if (status == null) {
  71. return <></>;
  72. }
  73. const { isOpened } = status;
  74. return (
  75. <Modal
  76. size="xl"
  77. isOpen={isOpened}
  78. toggle={close}
  79. data-testid="descendants-page-list-modal"
  80. className={`grw-descendants-page-list-modal ${styles['grw-descendants-page-list-modal']} ${isWindowExpanded ? 'grw-modal-expanded' : ''} `}
  81. >
  82. <ModalHeader className="p-0" toggle={close} close={buttons}>
  83. <CustomNavTab
  84. activeTab={activeTab}
  85. navTabMapping={navTabMapping}
  86. breakpointToHideInactiveTabsDown="md"
  87. onNavSelected={v => setActiveTab(v)}
  88. hideBorderBottom
  89. />
  90. </ModalHeader>
  91. <ModalBody>
  92. <CustomTabContent activeTab={activeTab} navTabMapping={navTabMapping} />
  93. </ModalBody>
  94. </Modal>
  95. );
  96. };