TreeItemLayout.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import React, {
  2. useCallback, useState, useEffect, useMemo,
  3. type FC, type RefObject, type RefCallback, type MouseEvent,
  4. } from 'react';
  5. import { useSWRxPageChildren } from '~/stores/page-listing';
  6. import { usePageTreeDescCountMap } from '~/stores/ui';
  7. import { ItemNode } from './ItemNode';
  8. import { SimpleItemContent } from './SimpleItemContent';
  9. import type { TreeItemProps, TreeItemToolProps } from './interfaces';
  10. import styles from './TreeItemLayout.module.scss';
  11. const moduleClass = styles['tree-item-layout'] ?? '';
  12. type TreeItemLayoutProps = TreeItemProps & {
  13. className?: string,
  14. itemRef?: RefObject<any> | RefCallback<any>,
  15. indentSize?: number,
  16. }
  17. export const TreeItemLayout = (props: TreeItemLayoutProps): JSX.Element => {
  18. const {
  19. className, itemClassName,
  20. indentSize = 10,
  21. itemLevel: baseItemLevel = 1,
  22. itemNode, targetPath, targetPathOrId, isOpen: _isOpen = false,
  23. onRenamed, onClick, onClickDuplicateMenuItem, onClickDeleteMenuItem, onWheelClick,
  24. isEnableActions, isReadOnlyUser, isWipPageShown = true,
  25. itemRef, itemClass,
  26. showAlternativeContent,
  27. } = props;
  28. const { page } = itemNode;
  29. const [currentChildren, setCurrentChildren] = useState<ItemNode[]>([]);
  30. const [isOpen, setIsOpen] = useState(_isOpen);
  31. const { data } = useSWRxPageChildren(isOpen ? page._id : null);
  32. const itemClickHandler = useCallback((e: MouseEvent) => {
  33. // DO NOT handle the event when e.currentTarget and e.target is different
  34. if (e.target !== e.currentTarget) {
  35. return;
  36. }
  37. onClick?.(page);
  38. }, [onClick, page]);
  39. const itemMouseupHandler = useCallback((e: MouseEvent) => {
  40. // DO NOT handle the event when e.currentTarget and e.target is different
  41. if (e.target !== e.currentTarget) {
  42. return;
  43. }
  44. if (e.button === 1) {
  45. e.preventDefault();
  46. onWheelClick?.(page);
  47. }
  48. }, [onWheelClick, page]);
  49. // descendantCount
  50. const { getDescCount } = usePageTreeDescCountMap();
  51. const descendantCount = getDescCount(page._id) || page.descendantCount || 0;
  52. // hasDescendants flag
  53. const isChildrenLoaded = currentChildren?.length > 0;
  54. const hasDescendants = descendantCount > 0 || isChildrenLoaded;
  55. const hasChildren = useCallback((): boolean => {
  56. return currentChildren != null && currentChildren.length > 0;
  57. }, [currentChildren]);
  58. const onClickLoadChildren = useCallback(() => {
  59. setIsOpen(!isOpen);
  60. }, [isOpen]);
  61. // didMount
  62. useEffect(() => {
  63. const isPathToTarget = page.path != null && targetPath.startsWith(page.path) && targetPath !== page.path; // Target Page does not need to be opened
  64. if (isPathToTarget) setIsOpen(true);
  65. }, [targetPath, page.path]);
  66. /*
  67. * When swr fetch succeeded
  68. */
  69. useEffect(() => {
  70. if (isOpen && data != null) {
  71. const newChildren = ItemNode.generateNodesFromPages(data.children);
  72. setCurrentChildren(newChildren);
  73. }
  74. }, [data, isOpen, targetPathOrId]);
  75. const isSelected = useMemo(() => {
  76. return page._id === targetPathOrId || page.path === targetPathOrId;
  77. }, [page, targetPathOrId]);
  78. const ItemClassFixed = itemClass ?? TreeItemLayout;
  79. const baseProps: Omit<TreeItemProps, 'itemLevel' | 'itemNode'> = {
  80. isEnableActions,
  81. isReadOnlyUser,
  82. isOpen: false,
  83. isWipPageShown,
  84. targetPath,
  85. targetPathOrId,
  86. onRenamed,
  87. onClickDuplicateMenuItem,
  88. onClickDeleteMenuItem,
  89. };
  90. const toolProps: TreeItemToolProps = {
  91. ...baseProps,
  92. itemLevel: baseItemLevel,
  93. itemNode,
  94. stateHandlers: {
  95. setIsOpen,
  96. },
  97. };
  98. const EndComponents = props.customEndComponents;
  99. const HoveredEndComponents = props.customHoveredEndComponents;
  100. const HeadObChildrenComponents = props.customHeadOfChildrenComponents;
  101. const AlternativeComponents = props.customAlternativeComponents;
  102. if (!isWipPageShown && page.wip) {
  103. return <></>;
  104. }
  105. return (
  106. <div
  107. id={`tree-item-layout-${page._id}`}
  108. data-testid="grw-pagetree-item-container"
  109. className={`${moduleClass} ${className} level-${baseItemLevel}`}
  110. style={{ paddingLeft: `${baseItemLevel > 1 ? indentSize : 0}px` }}
  111. >
  112. <li
  113. ref={itemRef}
  114. role="button"
  115. className={`list-group-item list-group-item-action ${itemClassName}
  116. border-0 py-0 ps-0 d-flex align-items-center rounded-1`}
  117. id={`grw-pagetree-list-${page._id}`}
  118. onClick={itemClickHandler}
  119. onMouseUp={itemMouseupHandler}
  120. aria-current={isSelected ? true : undefined}
  121. >
  122. <div className="btn-triangle-container d-flex justify-content-center">
  123. {hasDescendants && (
  124. <button
  125. type="button"
  126. className={`btn btn-triangle p-0 ${isOpen ? 'open' : ''}`}
  127. onClick={onClickLoadChildren}
  128. >
  129. <div className="d-flex justify-content-center">
  130. <span className="material-symbols-outlined fs-5">arrow_right</span>
  131. </div>
  132. </button>
  133. )}
  134. </div>
  135. { showAlternativeContent && AlternativeComponents != null
  136. ? (
  137. AlternativeComponents.map((AlternativeContent, index) => (
  138. // eslint-disable-next-line react/no-array-index-key
  139. <AlternativeContent key={index} {...toolProps} />
  140. ))
  141. )
  142. : (
  143. <>
  144. <SimpleItemContent page={page} />
  145. <div className="d-hover-none">
  146. {EndComponents?.map((EndComponent, index) => (
  147. // eslint-disable-next-line react/no-array-index-key
  148. <EndComponent key={index} {...toolProps} />
  149. ))}
  150. </div>
  151. <div className="d-none d-hover-flex">
  152. {HoveredEndComponents?.map((HoveredEndContent, index) => (
  153. // eslint-disable-next-line react/no-array-index-key
  154. <HoveredEndContent key={index} {...toolProps} />
  155. ))}
  156. </div>
  157. </>
  158. )
  159. }
  160. </li>
  161. { isOpen && (
  162. <div className={`tree-item-layout-children level-${baseItemLevel + 1}`}>
  163. {HeadObChildrenComponents?.map((HeadObChildrenContents, index) => (
  164. // eslint-disable-next-line react/no-array-index-key
  165. <HeadObChildrenContents key={index} {...toolProps} itemLevel={baseItemLevel + 1} />
  166. ))}
  167. { hasChildren() && currentChildren.map((node) => {
  168. const itemProps = {
  169. ...baseProps,
  170. className,
  171. itemLevel: baseItemLevel + 1,
  172. itemNode: node,
  173. itemClass,
  174. itemClassName,
  175. onClick,
  176. };
  177. return (
  178. <ItemClassFixed key={node.page._id} {...itemProps} />
  179. );
  180. }) }
  181. </div>
  182. ) }
  183. </div>
  184. );
  185. };