TreeItemLayout.tsx 6.8 KB

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