ItemsTree.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import React, { FC } from 'react';
  2. import { IPageHasId } from '../../../interfaces/page';
  3. import { ItemNode } from './ItemNode';
  4. import Item from './Item';
  5. import { useSWRxPageAncestorsChildren, useSWRxRootPage } from '../../../stores/page-listing';
  6. import { TargetAndAncestors } from '~/interfaces/page-listing-results';
  7. import { toastError } from '~/client/util/apiNotification';
  8. import {
  9. IPageForPageDeleteModal, usePageDuplicateModalStatus, usePageRenameModalStatus, usePageDeleteModalStatus,
  10. } from '~/stores/ui';
  11. /*
  12. * Utility to generate initial node
  13. */
  14. const generateInitialNodeBeforeResponse = (targetAndAncestors: Partial<IPageHasId>[]): ItemNode => {
  15. const nodes = targetAndAncestors.map((page): ItemNode => {
  16. return new ItemNode(page, []);
  17. });
  18. // update children for each node
  19. const rootNode = nodes.reduce((child, parent) => {
  20. parent.children = [child];
  21. return parent;
  22. });
  23. return rootNode;
  24. };
  25. const generateInitialNodeAfterResponse = (ancestorsChildren: Record<string, Partial<IPageHasId>[]>, rootNode: ItemNode): ItemNode => {
  26. const paths = Object.keys(ancestorsChildren);
  27. let currentNode = rootNode;
  28. paths.every((path) => {
  29. // stop rendering when non-migrated pages found
  30. if (currentNode == null) {
  31. return false;
  32. }
  33. const childPages = ancestorsChildren[path];
  34. currentNode.children = ItemNode.generateNodesFromPages(childPages);
  35. const nextNode = currentNode.children.filter((node) => {
  36. return paths.includes(node.page.path as string);
  37. })[0];
  38. currentNode = nextNode;
  39. return true;
  40. });
  41. return rootNode;
  42. };
  43. type ItemsTreeProps = {
  44. isEnableActions: boolean
  45. targetPath: string
  46. targetPathOrId?: string
  47. targetAndAncestorsData?: TargetAndAncestors
  48. }
  49. const renderByInitialNode = (
  50. initialNode: ItemNode,
  51. isEnableActions: boolean,
  52. targetPathOrId?: string,
  53. onClickDuplicateMenuItem?: (pageId: string, path: string) => void,
  54. onClickRenameMenuItem?: (pageId: string, revisionId: string, path: string) => void,
  55. onClickDeleteByPage?: (pageToDelete: IPageForPageDeleteModal | null) => void,
  56. ): JSX.Element => {
  57. return (
  58. <ul className="grw-pagetree list-group p-3">
  59. <Item
  60. key={initialNode.page.path}
  61. targetPathOrId={targetPathOrId}
  62. itemNode={initialNode}
  63. isOpen
  64. isEnableActions={isEnableActions}
  65. onClickDuplicateMenuItem={onClickDuplicateMenuItem}
  66. onClickRenameMenuItem={onClickRenameMenuItem}
  67. onClickDeleteByPage={onClickDeleteByPage}
  68. />
  69. </ul>
  70. );
  71. };
  72. /*
  73. * ItemsTree
  74. */
  75. const ItemsTree: FC<ItemsTreeProps> = (props: ItemsTreeProps) => {
  76. const {
  77. targetPath, targetPathOrId, targetAndAncestorsData, isEnableActions,
  78. } = props;
  79. const { data: ancestorsChildrenData, error: error1 } = useSWRxPageAncestorsChildren(targetPath);
  80. const { data: rootPageData, error: error2 } = useSWRxRootPage();
  81. const { open: openDuplicateModal } = usePageDuplicateModalStatus();
  82. const { open: openRenameModal } = usePageRenameModalStatus();
  83. const { open: openDeleteModal } = usePageDeleteModalStatus();
  84. const onClickDuplicateMenuItem = (pageId: string, path: string) => {
  85. openDuplicateModal(pageId, path);
  86. };
  87. const onClickRenameMenuItem = (pageId: string, revisionId: string, path: string) => {
  88. openRenameModal(pageId, revisionId, path);
  89. };
  90. const onClickDeleteByPage = (pageToDelete: IPageForPageDeleteModal) => {
  91. openDeleteModal([pageToDelete]);
  92. };
  93. if (error1 != null || error2 != null) {
  94. // TODO: improve message
  95. toastError('Error occurred while fetching pages to render PageTree');
  96. return null;
  97. }
  98. /*
  99. * Render completely
  100. */
  101. if (ancestorsChildrenData != null && rootPageData != null) {
  102. const initialNode = generateInitialNodeAfterResponse(ancestorsChildrenData.ancestorsChildren, new ItemNode(rootPageData.rootPage));
  103. return renderByInitialNode(initialNode, isEnableActions, targetPathOrId, onClickDuplicateMenuItem, onClickRenameMenuItem, onClickDeleteByPage);
  104. }
  105. /*
  106. * Before swr response comes back
  107. */
  108. if (targetAndAncestorsData != null) {
  109. const initialNode = generateInitialNodeBeforeResponse(targetAndAncestorsData.targetAndAncestors);
  110. return renderByInitialNode(initialNode, isEnableActions, targetPathOrId, onClickDuplicateMenuItem, onClickRenameMenuItem, onClickDeleteByPage);
  111. }
  112. return null;
  113. };
  114. export default ItemsTree;