PageTreeSubstance.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import React, {
  2. memo, useCallback, useEffect, useMemo, useRef, useState,
  3. } from 'react';
  4. import { useTranslation } from 'next-i18next';
  5. import { debounce } from 'throttle-debounce';
  6. import { useTargetAndAncestors, useIsGuestUser, useIsReadOnlyUser } from '~/stores/context';
  7. import { useCurrentPagePath, useCurrentPageId } from '~/stores/page';
  8. import {
  9. mutatePageTree, useSWRxPageAncestorsChildren, useSWRxRootPage, useSWRxV5MigrationStatus,
  10. } from '~/stores/page-listing';
  11. import { useSidebarScrollerRef } from '~/stores/ui';
  12. import loggerFactory from '~/utils/logger';
  13. import { ItemsTree } from '../../ItemsTree/ItemsTree';
  14. import { PageTreeItem } from '../PageTreeItem';
  15. import { SidebarHeaderReloadButton } from '../SidebarHeaderReloadButton';
  16. import { PrivateLegacyPagesLink } from './PrivateLegacyPagesLink';
  17. const logger = loggerFactory('growi:cli:PageTreeSubstance');
  18. type HeaderProps = {
  19. isWipPageShown: boolean,
  20. onWipPageShownChange?: () => void
  21. }
  22. export const PageTreeHeader = memo(({ isWipPageShown, onWipPageShownChange }: HeaderProps) => {
  23. const { t } = useTranslation();
  24. const { mutate: mutateRootPage } = useSWRxRootPage({ suspense: true });
  25. useSWRxV5MigrationStatus({ suspense: true });
  26. const mutate = useCallback(() => {
  27. mutateRootPage();
  28. mutatePageTree();
  29. }, [mutateRootPage]);
  30. return (
  31. <>
  32. <SidebarHeaderReloadButton onClick={() => mutate()} />
  33. <div className="me-1">
  34. <button
  35. color="transparent"
  36. className="btn p-0 border-0"
  37. type="button"
  38. data-bs-toggle="dropdown"
  39. data-bs-auto-close="outside"
  40. aria-expanded="false"
  41. >
  42. <span className="material-symbols-outlined">more_horiz</span>
  43. </button>
  44. <ul className="dropdown-menu">
  45. <li className="dropdown-item">
  46. <div className="form-check form-switch flex-fill d-flex">
  47. <input
  48. id="show-wip-page-checkbox"
  49. className="form-check-input"
  50. type="checkbox"
  51. defaultChecked={isWipPageShown}
  52. onChange={onWipPageShownChange}
  53. />
  54. <label className="form-check-label flex-grow-1 ms-2" htmlFor="show-wip-page-checkbox">
  55. {t('sidebar_header.show_wip_page')}
  56. </label>
  57. </div>
  58. </li>
  59. </ul>
  60. </div>
  61. </>
  62. );
  63. });
  64. PageTreeHeader.displayName = 'PageTreeHeader';
  65. const PageTreeUnavailable = () => {
  66. const { t } = useTranslation();
  67. // TODO : improve design
  68. // Story : https://redmine.weseek.co.jp/issues/83755
  69. return (
  70. <div className="mt-5 mx-2 text-center">
  71. <h3 className="text-gray">{t('v5_page_migration.page_tree_not_avaliable')}</h3>
  72. <a href="/admin">{t('v5_page_migration.go_to_settings')}</a>
  73. </div>
  74. );
  75. };
  76. type PageTreeContentProps = {
  77. isWipPageShown: boolean,
  78. }
  79. export const PageTreeContent = memo(({ isWipPageShown }: PageTreeContentProps) => {
  80. const { data: isGuestUser } = useIsGuestUser();
  81. const { data: isReadOnlyUser } = useIsReadOnlyUser();
  82. const { data: currentPath } = useCurrentPagePath();
  83. const { data: targetId } = useCurrentPageId();
  84. const { data: targetAndAncestorsData } = useTargetAndAncestors();
  85. const { data: migrationStatus } = useSWRxV5MigrationStatus({ suspense: true });
  86. const targetPathOrId = targetId || currentPath;
  87. const path = currentPath || '/';
  88. const { data: ancestorsChildrenResult } = useSWRxPageAncestorsChildren(path, { suspense: true });
  89. const { data: rootPageResult } = useSWRxRootPage({ suspense: true });
  90. const { data: sidebarScrollerRef } = useSidebarScrollerRef();
  91. const [isInitialScrollCompleted, setIsInitialScrollCompleted] = useState(false);
  92. const rootElemRef = useRef(null);
  93. // *************************** Scroll on init ***************************
  94. const scrollOnInit = useCallback(() => {
  95. const scrollTargetElement = document.getElementById('grw-pagetree-current-page-item');
  96. if (sidebarScrollerRef?.current == null || scrollTargetElement == null) {
  97. return;
  98. }
  99. logger.debug('scrollOnInit has invoked');
  100. const scrollElement = sidebarScrollerRef.current;
  101. // NOTE: could not use scrollIntoView
  102. // https://stackoverflow.com/questions/11039885/scrollintoview-causing-the-whole-page-to-move
  103. // calculate the center point
  104. const scrollTop = scrollTargetElement.offsetTop - scrollElement.getBoundingClientRect().height / 2;
  105. scrollElement.scrollTo({ top: scrollTop });
  106. setIsInitialScrollCompleted(true);
  107. }, [sidebarScrollerRef]);
  108. const scrollOnInitDebounced = useMemo(() => debounce(500, scrollOnInit), [scrollOnInit]);
  109. useEffect(() => {
  110. if (isInitialScrollCompleted || ancestorsChildrenResult == null || rootPageResult == null) {
  111. return;
  112. }
  113. const rootElement = rootElemRef.current as HTMLElement | null;
  114. if (rootElement == null) {
  115. return;
  116. }
  117. const observerCallback = (mutationRecords: MutationRecord[]) => {
  118. mutationRecords.forEach(() => scrollOnInitDebounced());
  119. };
  120. const observer = new MutationObserver(observerCallback);
  121. observer.observe(rootElement, { childList: true, subtree: true });
  122. // first call for the situation that all rendering is complete at this point
  123. scrollOnInitDebounced();
  124. return () => {
  125. observer.disconnect();
  126. };
  127. }, [isInitialScrollCompleted, scrollOnInitDebounced, ancestorsChildrenResult, rootPageResult]);
  128. // ******************************* end *******************************
  129. if (!migrationStatus?.isV5Compatible) {
  130. return <PageTreeUnavailable />;
  131. }
  132. /*
  133. * dependencies
  134. */
  135. if (isGuestUser == null) {
  136. return null;
  137. }
  138. return (
  139. <div ref={rootElemRef} className="pt-4">
  140. <ItemsTree
  141. isEnableActions={!isGuestUser}
  142. isReadOnlyUser={!!isReadOnlyUser}
  143. isWipPageShown={isWipPageShown}
  144. targetPath={path}
  145. targetPathOrId={targetPathOrId}
  146. targetAndAncestorsData={targetAndAncestorsData}
  147. CustomTreeItem={PageTreeItem}
  148. />
  149. {!isGuestUser && !isReadOnlyUser && migrationStatus?.migratablePagesCount != null && migrationStatus.migratablePagesCount !== 0 && (
  150. <div className="grw-pagetree-footer border-top mt-4 py-2 w-100">
  151. <div className="private-legacy-pages-link px-3 py-2">
  152. <PrivateLegacyPagesLink />
  153. </div>
  154. </div>
  155. )}
  156. </div>
  157. );
  158. });
  159. PageTreeContent.displayName = 'PageTreeContent';