ItemsTree.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import React, { FC, useEffect } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { IPageHasId } from '../../../interfaces/page';
  4. import { ItemNode } from './ItemNode';
  5. import Item from './Item';
  6. import { usePageTreeTermManager, useSWRxPageAncestorsChildren, useSWRxRootPage } from '~/stores/page-listing';
  7. import { TargetAndAncestors } from '~/interfaces/page-listing-results';
  8. import { OnDeletedFunction } from '~/interfaces/ui';
  9. import { toastError, toastSuccess } from '~/client/util/apiNotification';
  10. import {
  11. IPageForPageDeleteModal, usePageDuplicateModal, usePageRenameModal, usePageDeleteModal,
  12. } from '~/stores/modal';
  13. import { smoothScrollIntoView } from '~/client/util/smooth-scroll';
  14. import { useIsEnabledAttachTitleHeader } from '~/stores/context';
  15. import { useFullTextSearchTermManager } from '~/stores/search';
  16. import { useDescendantsPageListForCurrentPathTermManager } from '~/stores/page';
  17. /*
  18. * Utility to generate initial node
  19. */
  20. const generateInitialNodeBeforeResponse = (targetAndAncestors: Partial<IPageHasId>[]): ItemNode => {
  21. const nodes = targetAndAncestors.map((page): ItemNode => {
  22. return new ItemNode(page, []);
  23. });
  24. // update children for each node
  25. const rootNode = nodes.reduce((child, parent) => {
  26. parent.children = [child];
  27. return parent;
  28. });
  29. return rootNode;
  30. };
  31. const generateInitialNodeAfterResponse = (ancestorsChildren: Record<string, Partial<IPageHasId>[]>, rootNode: ItemNode): ItemNode => {
  32. const paths = Object.keys(ancestorsChildren);
  33. let currentNode = rootNode;
  34. paths.every((path) => {
  35. // stop rendering when non-migrated pages found
  36. if (currentNode == null) {
  37. return false;
  38. }
  39. const childPages = ancestorsChildren[path];
  40. currentNode.children = ItemNode.generateNodesFromPages(childPages);
  41. const nextNode = currentNode.children.filter((node) => {
  42. return paths.includes(node.page.path as string);
  43. })[0];
  44. currentNode = nextNode;
  45. return true;
  46. });
  47. return rootNode;
  48. };
  49. type ItemsTreeProps = {
  50. isEnableActions: boolean
  51. targetPath: string
  52. targetPathOrId?: string
  53. targetAndAncestorsData?: TargetAndAncestors
  54. }
  55. const renderByInitialNode = (
  56. initialNode: ItemNode,
  57. isEnableActions: boolean,
  58. targetPathOrId?: string,
  59. isEnabledAttachTitleHeader?: boolean,
  60. onClickDuplicateMenuItem?: (pageId: string, path: string) => void,
  61. onClickRenameMenuItem?: (pageId: string, revisionId: string, path: string) => void,
  62. onClickDeleteMenuItem?: (pageToDelete: IPageForPageDeleteModal) => void,
  63. ): JSX.Element => {
  64. return (
  65. <ul className="grw-pagetree list-group p-3">
  66. <Item
  67. key={initialNode.page.path}
  68. targetPathOrId={targetPathOrId}
  69. itemNode={initialNode}
  70. isOpen
  71. isEnabledAttachTitleHeader={isEnabledAttachTitleHeader}
  72. isEnableActions={isEnableActions}
  73. onClickDuplicateMenuItem={onClickDuplicateMenuItem}
  74. onClickRenameMenuItem={onClickRenameMenuItem}
  75. onClickDeleteMenuItem={onClickDeleteMenuItem}
  76. />
  77. </ul>
  78. );
  79. };
  80. /*
  81. * ItemsTree
  82. */
  83. const ItemsTree: FC<ItemsTreeProps> = (props: ItemsTreeProps) => {
  84. const {
  85. targetPath, targetPathOrId, targetAndAncestorsData, isEnableActions,
  86. } = props;
  87. const { t } = useTranslation();
  88. const { data: ancestorsChildrenData, error: error1 } = useSWRxPageAncestorsChildren(targetPath);
  89. const { data: rootPageData, error: error2 } = useSWRxRootPage();
  90. const { data: isEnabledAttachTitleHeader } = useIsEnabledAttachTitleHeader();
  91. const { open: openDuplicateModal } = usePageDuplicateModal();
  92. const { open: openRenameModal } = usePageRenameModal();
  93. const { open: openDeleteModal } = usePageDeleteModal();
  94. // for mutation
  95. const { advance: advancePt } = usePageTreeTermManager();
  96. const { advance: advanceFts } = useFullTextSearchTermManager();
  97. const { advance: advanceDpl } = useDescendantsPageListForCurrentPathTermManager();
  98. useEffect(() => {
  99. const startFrom = document.getElementById('grw-sidebar-contents-scroll-target');
  100. const targetElem = document.getElementsByClassName('grw-pagetree-is-target');
  101. // targetElem is HTML collection but only one HTML element in it all the time
  102. if (targetElem[0] != null && startFrom != null) {
  103. smoothScrollIntoView(targetElem[0] as HTMLElement, 0, startFrom);
  104. }
  105. }, [ancestorsChildrenData]);
  106. const onClickDuplicateMenuItem = (pageId: string, path: string) => {
  107. openDuplicateModal(pageId, path);
  108. };
  109. const onClickRenameMenuItem = (pageId: string, revisionId: string, path: string) => {
  110. openRenameModal(pageId, revisionId, path);
  111. };
  112. const onClickDeleteMenuItem = (pageToDelete: IPageForPageDeleteModal) => {
  113. const onDeletedHandler: OnDeletedFunction = (pathOrPathsToDelete, isRecursively, isCompletely) => {
  114. if (typeof pathOrPathsToDelete !== 'string') {
  115. return;
  116. }
  117. const path = pathOrPathsToDelete;
  118. if (isCompletely) {
  119. toastSuccess(t('deleted_pages_completely', { path }));
  120. }
  121. else {
  122. toastSuccess(t('deleted_pages', { path }));
  123. }
  124. advancePt();
  125. advanceFts();
  126. advanceDpl();
  127. };
  128. openDeleteModal([pageToDelete], { onDeleted: onDeletedHandler });
  129. };
  130. if (error1 != null || error2 != null) {
  131. // TODO: improve message
  132. toastError('Error occurred while fetching pages to render PageTree');
  133. return null;
  134. }
  135. /*
  136. * Render completely
  137. */
  138. if (ancestorsChildrenData != null && rootPageData != null) {
  139. const initialNode = generateInitialNodeAfterResponse(ancestorsChildrenData.ancestorsChildren, new ItemNode(rootPageData.rootPage));
  140. return renderByInitialNode(
  141. initialNode, isEnableActions, targetPathOrId, isEnabledAttachTitleHeader, onClickDuplicateMenuItem, onClickRenameMenuItem, onClickDeleteMenuItem,
  142. );
  143. }
  144. /*
  145. * Before swr response comes back
  146. */
  147. if (targetAndAncestorsData != null) {
  148. const initialNode = generateInitialNodeBeforeResponse(targetAndAncestorsData.targetAndAncestors);
  149. return renderByInitialNode(
  150. initialNode, isEnableActions, targetPathOrId, isEnabledAttachTitleHeader, onClickDuplicateMenuItem, onClickRenameMenuItem, onClickDeleteMenuItem,
  151. );
  152. }
  153. return null;
  154. };
  155. export default ItemsTree;