PageTreeSubstance.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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" onClick={onWipPageShownChange}>
  46. <div className="form-check form-switch">
  47. <input
  48. className="form-check-input pe-none"
  49. type="checkbox"
  50. checked={isWipPageShown}
  51. onChange={() => {}}
  52. />
  53. <label className="form-check-label pe-none">
  54. {t('sidebar_header.show_wip_page')}
  55. </label>
  56. </div>
  57. </li>
  58. </ul>
  59. </div>
  60. </>
  61. );
  62. });
  63. PageTreeHeader.displayName = 'PageTreeHeader';
  64. const PageTreeUnavailable = () => {
  65. const { t } = useTranslation();
  66. // TODO : improve design
  67. // Story : https://redmine.weseek.co.jp/issues/83755
  68. return (
  69. <div className="mt-5 mx-2 text-center">
  70. <h3 className="text-gray">{t('v5_page_migration.page_tree_not_avaliable')}</h3>
  71. <a href="/admin">{t('v5_page_migration.go_to_settings')}</a>
  72. </div>
  73. );
  74. };
  75. type PageTreeContentProps = {
  76. isWipPageShown: boolean,
  77. }
  78. export const PageTreeContent = memo(({ isWipPageShown }: PageTreeContentProps) => {
  79. const { data: isGuestUser } = useIsGuestUser();
  80. const { data: isReadOnlyUser } = useIsReadOnlyUser();
  81. const { data: currentPath } = useCurrentPagePath();
  82. const { data: targetId } = useCurrentPageId();
  83. const { data: targetAndAncestorsData } = useTargetAndAncestors();
  84. const { data: migrationStatus } = useSWRxV5MigrationStatus({ suspense: true });
  85. const targetPathOrId = targetId || currentPath;
  86. const path = currentPath || '/';
  87. const { data: ancestorsChildrenResult } = useSWRxPageAncestorsChildren(path, { suspense: true });
  88. const { data: rootPageResult } = useSWRxRootPage({ suspense: true });
  89. const { data: sidebarScrollerRef } = useSidebarScrollerRef();
  90. const [isInitialScrollCompleted, setIsInitialScrollCompleted] = useState(false);
  91. const rootElemRef = useRef<HTMLDivElement>(null);
  92. // *************************** Scroll on init ***************************
  93. const scrollOnInit = useCallback(() => {
  94. const rootElement = rootElemRef.current;
  95. const scrollElement = sidebarScrollerRef?.current;
  96. if (rootElement == null || scrollElement == null) {
  97. return;
  98. }
  99. const scrollTargetElement = rootElement.querySelector<HTMLElement>('[aria-current]');
  100. if (scrollTargetElement == null) {
  101. return;
  102. }
  103. logger.debug('scrollOnInit has invoked');
  104. // NOTE: could not use scrollIntoView
  105. // https://stackoverflow.com/questions/11039885/scrollintoview-causing-the-whole-page-to-move
  106. // calculate the center point
  107. const scrollTop = scrollTargetElement.offsetTop - scrollElement.getBoundingClientRect().height / 2;
  108. scrollElement.scrollTo({ top: scrollTop });
  109. setIsInitialScrollCompleted(true);
  110. }, [sidebarScrollerRef]);
  111. const scrollOnInitDebounced = useMemo(() => debounce(500, scrollOnInit), [scrollOnInit]);
  112. useEffect(() => {
  113. if (isInitialScrollCompleted || ancestorsChildrenResult == null || rootPageResult == null) {
  114. return;
  115. }
  116. const rootElement = rootElemRef.current as HTMLElement | null;
  117. if (rootElement == null) {
  118. return;
  119. }
  120. const observerCallback = (mutationRecords: MutationRecord[]) => {
  121. mutationRecords.forEach(() => scrollOnInitDebounced());
  122. };
  123. const observer = new MutationObserver(observerCallback);
  124. observer.observe(rootElement, { childList: true, subtree: true });
  125. // first call for the situation that all rendering is complete at this point
  126. scrollOnInitDebounced();
  127. return () => {
  128. observer.disconnect();
  129. };
  130. }, [isInitialScrollCompleted, scrollOnInitDebounced, ancestorsChildrenResult, rootPageResult]);
  131. // ******************************* end *******************************
  132. if (!migrationStatus?.isV5Compatible) {
  133. return <PageTreeUnavailable />;
  134. }
  135. /*
  136. * dependencies
  137. */
  138. if (isGuestUser == null) {
  139. return null;
  140. }
  141. return (
  142. <div ref={rootElemRef} className="pt-4">
  143. <ItemsTree
  144. isEnableActions={!isGuestUser}
  145. isReadOnlyUser={!!isReadOnlyUser}
  146. isWipPageShown={isWipPageShown}
  147. targetPath={path}
  148. targetPathOrId={targetPathOrId}
  149. targetAndAncestorsData={targetAndAncestorsData}
  150. CustomTreeItem={PageTreeItem}
  151. />
  152. {!isGuestUser && !isReadOnlyUser && migrationStatus?.migratablePagesCount != null && migrationStatus.migratablePagesCount !== 0 && (
  153. <div className="grw-pagetree-footer border-top mt-4 py-2 w-100">
  154. <div className="private-legacy-pages-link px-3 py-2">
  155. <PrivateLegacyPagesLink />
  156. </div>
  157. </div>
  158. )}
  159. </div>
  160. );
  161. });
  162. PageTreeContent.displayName = 'PageTreeContent';