ItemsTree.tsx 5.8 KB

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