DescendantsPageListModal.tsx 3.7 KB

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