PageTreeSubstance.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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-universal/context';
  7. import { useCurrentPagePath, useCurrentPageId } from '~/stores/page';
  8. import {
  9. mutatePageTree, mutateRecentlyUpdated, 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. mutateRecentlyUpdated();
  30. }, [mutateRootPage]);
  31. return (
  32. <>
  33. <SidebarHeaderReloadButton onClick={() => mutate()} />
  34. <div className="me-1">
  35. <button
  36. color="transparent"
  37. className="btn p-0 border-0"
  38. type="button"
  39. data-bs-toggle="dropdown"
  40. data-bs-auto-close="outside"
  41. aria-expanded="false"
  42. >
  43. <span className="material-symbols-outlined">more_horiz</span>
  44. </button>
  45. <ul className="dropdown-menu">
  46. <li className="dropdown-item" onClick={onWipPageShownChange}>
  47. <div className="form-check form-switch">
  48. <input
  49. className="form-check-input pe-none"
  50. type="checkbox"
  51. checked={isWipPageShown}
  52. onChange={() => {}}
  53. />
  54. <label className="form-check-label pe-none">
  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<HTMLDivElement>(null);
  93. // *************************** Scroll on init ***************************
  94. const scrollOnInit = useCallback(() => {
  95. const rootElement = rootElemRef.current;
  96. const scrollElement = sidebarScrollerRef?.current;
  97. if (rootElement == null || scrollElement == null) {
  98. return;
  99. }
  100. const scrollTargetElement = rootElement.querySelector<HTMLElement>('[aria-current]');
  101. if (scrollTargetElement == null) {
  102. return;
  103. }
  104. logger.debug('scrollOnInit has invoked');
  105. // NOTE: could not use scrollIntoView
  106. // https://stackoverflow.com/questions/11039885/scrollintoview-causing-the-whole-page-to-move
  107. // calculate the center point
  108. const scrollTop = scrollTargetElement.offsetTop - scrollElement.getBoundingClientRect().height / 2;
  109. scrollElement.scrollTo({ top: scrollTop });
  110. setIsInitialScrollCompleted(true);
  111. }, [sidebarScrollerRef]);
  112. const scrollOnInitDebounced = useMemo(() => debounce(500, scrollOnInit), [scrollOnInit]);
  113. useEffect(() => {
  114. if (isInitialScrollCompleted || ancestorsChildrenResult == null || rootPageResult == null) {
  115. return;
  116. }
  117. const rootElement = rootElemRef.current as HTMLElement | null;
  118. if (rootElement == null) {
  119. return;
  120. }
  121. const observerCallback = (mutationRecords: MutationRecord[]) => {
  122. mutationRecords.forEach(() => scrollOnInitDebounced());
  123. };
  124. const observer = new MutationObserver(observerCallback);
  125. observer.observe(rootElement, { childList: true, subtree: true });
  126. // first call for the situation that all rendering is complete at this point
  127. scrollOnInitDebounced();
  128. return () => {
  129. observer.disconnect();
  130. };
  131. }, [isInitialScrollCompleted, scrollOnInitDebounced, ancestorsChildrenResult, rootPageResult]);
  132. // ******************************* end *******************************
  133. if (!migrationStatus?.isV5Compatible) {
  134. return <PageTreeUnavailable />;
  135. }
  136. /*
  137. * dependencies
  138. */
  139. if (isGuestUser == null) {
  140. return null;
  141. }
  142. return (
  143. <div ref={rootElemRef} className="pt-4">
  144. <ItemsTree
  145. isEnableActions={!isGuestUser}
  146. isReadOnlyUser={!!isReadOnlyUser}
  147. isWipPageShown={isWipPageShown}
  148. targetPath={path}
  149. targetPathOrId={targetPathOrId}
  150. targetAndAncestorsData={targetAndAncestorsData}
  151. CustomTreeItem={PageTreeItem}
  152. />
  153. {!isGuestUser && !isReadOnlyUser && migrationStatus?.migratablePagesCount != null && migrationStatus.migratablePagesCount !== 0 && (
  154. <div className="grw-pagetree-footer border-top mt-4 py-2 w-100">
  155. <div className="private-legacy-pages-link px-3 py-2">
  156. <PrivateLegacyPagesLink />
  157. </div>
  158. </div>
  159. )}
  160. </div>
  161. );
  162. });
  163. PageTreeContent.displayName = 'PageTreeContent';