ItemsTree.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. IPageForPageDeleteModal, usePageDuplicateModalStatus, usePageRenameModalStatus, usePageDeleteModal,
  11. OnDeletedFunction,
  12. } from '~/stores/ui';
  13. import { smoothScrollIntoView } from '~/client/util/smooth-scroll';
  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. onClickDuplicateMenuItem?: (pageId: string, path: string) => void,
  57. onClickRenameMenuItem?: (pageId: string, revisionId: string, path: string) => void,
  58. onClickDeleteMenuItem?: (pageToDelete: IPageForPageDeleteModal | null) => void,
  59. ): JSX.Element => {
  60. return (
  61. <ul className="grw-pagetree list-group p-3">
  62. <Item
  63. key={initialNode.page.path}
  64. targetPathOrId={targetPathOrId}
  65. itemNode={initialNode}
  66. isOpen
  67. isEnableActions={isEnableActions}
  68. onClickDuplicateMenuItem={onClickDuplicateMenuItem}
  69. onClickRenameMenuItem={onClickRenameMenuItem}
  70. onClickDeleteMenuItem={onClickDeleteMenuItem}
  71. />
  72. </ul>
  73. );
  74. };
  75. /*
  76. * ItemsTree
  77. */
  78. const ItemsTree: FC<ItemsTreeProps> = (props: ItemsTreeProps) => {
  79. const {
  80. targetPath, targetPathOrId, targetAndAncestorsData, isEnableActions,
  81. } = props;
  82. const { t } = useTranslation();
  83. const { data: ancestorsChildrenData, error: error1 } = useSWRxPageAncestorsChildren(targetPath);
  84. const { data: rootPageData, error: error2 } = useSWRxRootPage();
  85. const { open: openDuplicateModal } = usePageDuplicateModalStatus();
  86. const { open: openRenameModal } = usePageRenameModalStatus();
  87. const { open: openDeleteModal } = usePageDeleteModal();
  88. useEffect(() => {
  89. const startFrom = document.getElementById('grw-sidebar-contents-scroll-target');
  90. const targetElem = document.getElementsByClassName('grw-pagetree-is-target');
  91. // targetElem is HTML collection but only one HTML element in it all the time
  92. if (targetElem[0] != null && startFrom != null) {
  93. smoothScrollIntoView(targetElem[0] as HTMLElement, 0, startFrom);
  94. }
  95. }, [ancestorsChildrenData]);
  96. const onClickDuplicateMenuItem = (pageId: string, path: string) => {
  97. openDuplicateModal(pageId, path);
  98. };
  99. const onClickRenameMenuItem = (pageId: string, revisionId: string, path: string) => {
  100. openRenameModal(pageId, revisionId, path);
  101. };
  102. const onDeletedHandler: OnDeletedFunction = (pathOrPathsToDelete, isRecursively, isCompletely) => {
  103. if (typeof pathOrPathsToDelete !== 'string') {
  104. return;
  105. }
  106. const path = pathOrPathsToDelete;
  107. if (isRecursively) {
  108. if (isCompletely) {
  109. toastSuccess(t('deleted_single_page_recursively_completely', { path }));
  110. }
  111. else {
  112. toastSuccess(t('deleted_single_page_recursively', { path }));
  113. }
  114. }
  115. else {
  116. // eslint-disable-next-line no-lonely-if
  117. if (isCompletely) {
  118. toastSuccess(t('deleted_single_page_completely', { path }));
  119. }
  120. else {
  121. toastSuccess(t('deleted_single_page', { path }));
  122. }
  123. }
  124. };
  125. const onClickDeleteMenuItem = (pageToDelete: IPageForPageDeleteModal) => {
  126. openDeleteModal([pageToDelete], onDeletedHandler);
  127. };
  128. if (error1 != null || error2 != null) {
  129. // TODO: improve message
  130. toastError('Error occurred while fetching pages to render PageTree');
  131. return null;
  132. }
  133. /*
  134. * Render completely
  135. */
  136. if (ancestorsChildrenData != null && rootPageData != null) {
  137. const initialNode = generateInitialNodeAfterResponse(ancestorsChildrenData.ancestorsChildren, new ItemNode(rootPageData.rootPage));
  138. return renderByInitialNode(initialNode, isEnableActions, targetPathOrId, onClickDuplicateMenuItem, onClickRenameMenuItem, onClickDeleteMenuItem);
  139. }
  140. /*
  141. * Before swr response comes back
  142. */
  143. if (targetAndAncestorsData != null) {
  144. const initialNode = generateInitialNodeBeforeResponse(targetAndAncestorsData.targetAndAncestors);
  145. return renderByInitialNode(initialNode, isEnableActions, targetPathOrId, onClickDuplicateMenuItem, onClickRenameMenuItem, onClickDeleteMenuItem);
  146. }
  147. return null;
  148. };
  149. export default ItemsTree;