ItemsTree.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import React, { FC } from 'react';
  2. import { pagePathUtils } from '@growi/core';
  3. import { IPage } from '../../../interfaces/page';
  4. import { ItemNode } from './ItemNode';
  5. import Item from './Item';
  6. import { useSWRxPageAncestorsChildren } from '../../../stores/page-listing';
  7. import { useTargetAndAncestors } from '../../../stores/context';
  8. import { HasObjectId } from '../../../interfaces/has-object-id';
  9. const { isTopPage } = pagePathUtils;
  10. /*
  11. * Utility to generate initial node
  12. */
  13. const generateInitialNodeBeforeResponse = (targetAndAncestors: Partial<IPage>[]): ItemNode => {
  14. const rootPage = targetAndAncestors[targetAndAncestors.length - 1]; // the last item is the root
  15. if (!isTopPage(rootPage?.path as string)) throw new Error('/ not exist in ancestors');
  16. const nodes = targetAndAncestors.map((page): ItemNode => {
  17. return new ItemNode(page, []);
  18. });
  19. // update children for each node
  20. const rootNode = nodes.reduce((child, parent) => {
  21. parent.children = [child];
  22. return parent;
  23. });
  24. return rootNode;
  25. };
  26. const generateInitialNodeAfterResponse = (ancestorsChildren: Record<string, Partial<IPage & HasObjectId>[]>, rootNode: ItemNode): ItemNode => {
  27. const paths = Object.keys(ancestorsChildren);
  28. const rootPath = paths[paths.length - 1]; // the last item is the root
  29. if (!isTopPage(rootPath)) throw new Error('rootPath must be "/"');
  30. let currentNode = rootNode;
  31. paths.reverse().forEach((path) => {
  32. const childPages = ancestorsChildren[path];
  33. currentNode.children = ItemNode.generateNodesFromPages(childPages);
  34. const nextNode = currentNode.children.filter((node) => {
  35. return paths.includes(node.page.path as string);
  36. })[0];
  37. currentNode = nextNode;
  38. });
  39. return rootNode;
  40. };
  41. /*
  42. * ItemsTree
  43. */
  44. const ItemsTree: FC = () => {
  45. // TODO: get from static SWR
  46. const path = '/Sandbox';
  47. const { data: targetAndAncestors, error } = useTargetAndAncestors();
  48. const { data: ancestorsChildrenData, error: error2 } = useSWRxPageAncestorsChildren(path);
  49. if (error != null || error2 != null) {
  50. return null;
  51. }
  52. if (targetAndAncestors == null) {
  53. return null;
  54. }
  55. let initialNode: ItemNode;
  56. /*
  57. * Before swr response comes back
  58. */
  59. if (ancestorsChildrenData == null) {
  60. initialNode = generateInitialNodeBeforeResponse(targetAndAncestors);
  61. }
  62. /*
  63. * When swr request finishes
  64. */
  65. else {
  66. const { ancestorsChildren } = ancestorsChildrenData;
  67. const rootPage = targetAndAncestors[targetAndAncestors.length - 1];
  68. const rootNode = new ItemNode(rootPage);
  69. initialNode = generateInitialNodeAfterResponse(ancestorsChildren, rootNode);
  70. }
  71. const isOpen = true;
  72. return (
  73. <>
  74. <Item key={(initialNode as ItemNode).page.path} itemNode={(initialNode as ItemNode)} isOpen={isOpen} />
  75. </>
  76. );
  77. };
  78. export default ItemsTree;