TreeItemLayout.tsx 7.5 KB

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