ItemsTree.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /*
  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, isAbleToDeleteCompletely: boolean) => 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 { mutate: mutateChildren } = useSWRxPageChildren(targetPathOrId);
  84. const { data: rootPageData, error: error2 } = useSWRxRootPage();
  85. const { open: openDuplicateModal } = usePageDuplicateModal();
  86. const { open: openRenameModal } = usePageRenameModal();
  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. mutateChildren();
  107. const path = pathOrPathsToDelete;
  108. if (isCompletely) {
  109. toastSuccess(t('deleted_pages_completely', { path }));
  110. }
  111. else {
  112. toastSuccess(t('deleted_pages', { path }));
  113. }
  114. };
  115. const onClickDeleteMenuItem = (pageToDelete: IPageForPageDeleteModal, isAbleToDeleteCompletely) => {
  116. openDeleteModal([pageToDelete], onDeletedHandler, isAbleToDeleteCompletely);
  117. };
  118. if (error1 != null || error2 != null) {
  119. // TODO: improve message
  120. toastError('Error occurred while fetching pages to render PageTree');
  121. return null;
  122. }
  123. /*
  124. * Render completely
  125. */
  126. if (ancestorsChildrenData != null && rootPageData != null) {
  127. const initialNode = generateInitialNodeAfterResponse(ancestorsChildrenData.ancestorsChildren, new ItemNode(rootPageData.rootPage));
  128. return renderByInitialNode(initialNode, isEnableActions, targetPathOrId, onClickDuplicateMenuItem, onClickRenameMenuItem, onClickDeleteMenuItem);
  129. }
  130. /*
  131. * Before swr response comes back
  132. */
  133. if (targetAndAncestorsData != null) {
  134. const initialNode = generateInitialNodeBeforeResponse(targetAndAncestorsData.targetAndAncestors);
  135. return renderByInitialNode(initialNode, isEnableActions, targetPathOrId, onClickDuplicateMenuItem, onClickRenameMenuItem, onClickDeleteMenuItem);
  136. }
  137. return null;
  138. };
  139. export default ItemsTree;