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