PageTreeSubstance.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 { useIsGuestUser, useIsReadOnlyUser } from '~/stores-universal/context';
  7. import { useCurrentPagePath, useCurrentPageId } from '~/stores/page';
  8. import {
  9. mutatePageTree, mutateRecentlyUpdated, 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: migrationStatus } = useSWRxV5MigrationStatus({ suspense: true });
  85. const targetPathOrId = targetId || currentPath;
  86. const path = currentPath || '/';
  87. const { data: rootPageResult } = useSWRxRootPage({ suspense: true });
  88. const { data: sidebarScrollerRef } = useSidebarScrollerRef();
  89. const [isInitialScrollCompleted, setIsInitialScrollCompleted] = useState(false);
  90. const rootElemRef = useRef<HTMLDivElement>(null);
  91. // *************************** Scroll on init ***************************
  92. const scrollOnInit = useCallback(() => {
  93. const rootElement = rootElemRef.current;
  94. const scrollElement = sidebarScrollerRef?.current;
  95. if (rootElement == null || scrollElement == null) {
  96. return;
  97. }
  98. const scrollTargetElement = rootElement.querySelector<HTMLElement>('[aria-current]');
  99. if (scrollTargetElement == null) {
  100. return;
  101. }
  102. logger.debug('scrollOnInit has invoked');
  103. // NOTE: could not use scrollIntoView
  104. // https://stackoverflow.com/questions/11039885/scrollintoview-causing-the-whole-page-to-move
  105. // calculate the center point
  106. const scrollTop = scrollTargetElement.offsetTop - scrollElement.getBoundingClientRect().height / 2;
  107. scrollElement.scrollTo({ top: scrollTop });
  108. setIsInitialScrollCompleted(true);
  109. }, [sidebarScrollerRef]);
  110. const scrollOnInitDebounced = useMemo(() => debounce(500, scrollOnInit), [scrollOnInit]);
  111. useEffect(() => {
  112. if (isInitialScrollCompleted || rootPageResult == null) {
  113. return;
  114. }
  115. const rootElement = rootElemRef.current as HTMLElement | null;
  116. if (rootElement == null) {
  117. return;
  118. }
  119. const observerCallback = (mutationRecords: MutationRecord[]) => {
  120. mutationRecords.forEach(() => scrollOnInitDebounced());
  121. };
  122. const observer = new MutationObserver(observerCallback);
  123. observer.observe(rootElement, { childList: true, subtree: true });
  124. // first call for the situation that all rendering is complete at this point
  125. scrollOnInitDebounced();
  126. return () => {
  127. observer.disconnect();
  128. };
  129. }, [isInitialScrollCompleted, scrollOnInitDebounced, rootPageResult]);
  130. // ******************************* end *******************************
  131. if (!migrationStatus?.isV5Compatible) {
  132. return <PageTreeUnavailable />;
  133. }
  134. /*
  135. * dependencies
  136. */
  137. if (isGuestUser == null) {
  138. return null;
  139. }
  140. return (
  141. <div ref={rootElemRef} className="pt-4">
  142. <ItemsTree
  143. isEnableActions={!isGuestUser}
  144. isReadOnlyUser={!!isReadOnlyUser}
  145. isWipPageShown={isWipPageShown}
  146. targetPath={path}
  147. targetPathOrId={targetPathOrId}
  148. CustomTreeItem={PageTreeItem}
  149. />
  150. {!isGuestUser && !isReadOnlyUser && migrationStatus?.migratablePagesCount != null && migrationStatus.migratablePagesCount !== 0 && (
  151. <div className="grw-pagetree-footer border-top mt-4 py-2 w-100">
  152. <div className="private-legacy-pages-link px-3 py-2">
  153. <PrivateLegacyPagesLink />
  154. </div>
  155. </div>
  156. )}
  157. </div>
  158. );
  159. });
  160. PageTreeContent.displayName = 'PageTreeContent';