DescendantsPageListModal.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import React, {
  2. useState, useMemo, useEffect, type JSX,
  3. } from 'react';
  4. import { useTranslation } from 'next-i18next';
  5. import dynamic from 'next/dynamic';
  6. import { useRouter } from 'next/router';
  7. import {
  8. Modal, ModalHeader, ModalBody,
  9. } from 'reactstrap';
  10. import { useIsSharedUser } from '~/stores-universal/context';
  11. import { useDescendantsPageListModal } from '~/stores/modal';
  12. import { useIsDeviceLargerThanLg } from '~/stores/ui';
  13. import { CustomNavDropdown, CustomNavTab } from './CustomNavigation/CustomNav';
  14. import CustomTabContent from './CustomNavigation/CustomTabContent';
  15. import type { DescendantsPageListProps } from './DescendantsPageList';
  16. import ExpandOrContractButton from './ExpandOrContractButton';
  17. import styles from './DescendantsPageListModal.module.scss';
  18. const DescendantsPageList = dynamic<DescendantsPageListProps>(() => import('./DescendantsPageList').then(mod => mod.DescendantsPageList), { ssr: false });
  19. const PageTimeline = dynamic(() => import('./PageTimeline').then(mod => mod.PageTimeline), { ssr: false });
  20. export const DescendantsPageListModal = (): JSX.Element => {
  21. const { t } = useTranslation();
  22. const [activeTab, setActiveTab] = useState('pagelist');
  23. const [isWindowExpanded, setIsWindowExpanded] = useState(false);
  24. const { data: isSharedUser } = useIsSharedUser();
  25. const { data: status, close } = useDescendantsPageListModal();
  26. const { events } = useRouter();
  27. const { data: isDeviceLargerThanLg } = useIsDeviceLargerThanLg();
  28. useEffect(() => {
  29. events.on('routeChangeStart', close);
  30. return () => {
  31. events.off('routeChangeStart', close);
  32. };
  33. }, [close, events]);
  34. const navTabMapping = useMemo(() => {
  35. return {
  36. pagelist: {
  37. Icon: () => <span className="material-symbols-outlined">subject</span>,
  38. Content: () => {
  39. if (status == null || status.path == null || !status.isOpened) {
  40. return <></>;
  41. }
  42. return <DescendantsPageList path={status.path} />;
  43. },
  44. i18n: t('page_list'),
  45. isLinkEnabled: () => !isSharedUser,
  46. },
  47. timeline: {
  48. Icon: () => <span data-testid="timeline-tab-button" className="material-symbols-outlined">timeline</span>,
  49. Content: () => {
  50. if (status == null || !status.isOpened) {
  51. return <></>;
  52. }
  53. return <PageTimeline />;
  54. },
  55. i18n: t('Timeline View'),
  56. isLinkEnabled: () => !isSharedUser,
  57. },
  58. };
  59. }, [isSharedUser, status, t]);
  60. const buttons = useMemo(() => (
  61. <span className="me-3">
  62. <ExpandOrContractButton
  63. isWindowExpanded={isWindowExpanded}
  64. expandWindow={() => setIsWindowExpanded(true)}
  65. contractWindow={() => setIsWindowExpanded(false)}
  66. />
  67. <button type="button" className="btn btn-close ms-2" onClick={close} aria-label="Close"></button>
  68. </span>
  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={isDeviceLargerThanLg ? 'p-0' : ''} toggle={close} close={buttons}>
  83. {isDeviceLargerThanLg && (
  84. <CustomNavTab
  85. activeTab={activeTab}
  86. navTabMapping={navTabMapping}
  87. breakpointToHideInactiveTabsDown="md"
  88. onNavSelected={v => setActiveTab(v)}
  89. hideBorderBottom
  90. />
  91. )}
  92. </ModalHeader>
  93. <ModalBody>
  94. {!isDeviceLargerThanLg && (
  95. <CustomNavDropdown
  96. activeTab={activeTab}
  97. navTabMapping={navTabMapping}
  98. onNavSelected={v => setActiveTab(v)}
  99. />
  100. )}
  101. <CustomTabContent
  102. activeTab={activeTab}
  103. navTabMapping={navTabMapping}
  104. additionalClassNames={!isDeviceLargerThanLg ? ['grw-tab-content-style-md-down'] : undefined}
  105. />
  106. </ModalBody>
  107. </Modal>
  108. );
  109. };