TreeItemLayout.tsx 6.8 KB

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