SimpleItem.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import React, {
  2. useCallback, useState, useEffect,
  3. type FC, type RefObject, type RefCallback, type MouseEvent,
  4. } from 'react';
  5. import nodePath from 'path';
  6. import type { Nullable } from '@growi/core';
  7. import { useTranslation } from 'next-i18next';
  8. import { UncontrolledTooltip } from 'reactstrap';
  9. import { useSWRxPageChildren } from '~/stores/page-listing';
  10. import { usePageTreeDescCountMap } from '~/stores/ui';
  11. import { shouldRecoverPagePaths } from '~/utils/page-operation';
  12. import CountBadge from '../Common/CountBadge';
  13. import { ItemNode } from './ItemNode';
  14. import { useNewPageInput } from './NewPageInput';
  15. import type { TreeItemProps, TreeItemToolProps } from './interfaces';
  16. // Utility to mark target
  17. const markTarget = (children: ItemNode[], targetPathOrId?: Nullable<string>): void => {
  18. if (targetPathOrId == null) {
  19. return;
  20. }
  21. children.forEach((node) => {
  22. if (node.page._id === targetPathOrId || node.page.path === targetPathOrId) {
  23. node.page.isTarget = true;
  24. }
  25. else {
  26. node.page.isTarget = false;
  27. }
  28. return node;
  29. });
  30. };
  31. const SimpleItemContent: FC<TreeItemToolProps> = (props) => {
  32. const { t } = useTranslation();
  33. const { onClick } = props;
  34. const { page } = props.itemNode;
  35. const pageName = nodePath.basename(page.path ?? '') || '/';
  36. const shouldShowAttentionIcon = page.processData != null ? shouldRecoverPagePaths(page.processData) : false;
  37. const clickHandler = (e: MouseEvent<HTMLParagraphElement>) => {
  38. if (onClick == null) {
  39. return;
  40. }
  41. e.preventDefault();
  42. onClick(page);
  43. };
  44. return (
  45. <>
  46. {shouldShowAttentionIcon && (
  47. <>
  48. <i id="path-recovery" className="fa fa-warning mr-2 text-warning"></i>
  49. <UncontrolledTooltip placement="top" target="path-recovery" fade={false}>
  50. {t('tooltip.operation.attention.rename')}
  51. </UncontrolledTooltip>
  52. </>
  53. )}
  54. {page != null && page.path != null && page._id != null && (
  55. <div className="grw-pagetree-title-anchor flex-grow-1">
  56. <p onClick={clickHandler} className={`text-truncate m-auto ${page.isEmpty && 'grw-sidebar-text-muted'}`}>{pageName}</p>
  57. </div>
  58. )}
  59. </>
  60. );
  61. };
  62. export const SimpleItemTool: FC<TreeItemToolProps> = (props) => {
  63. const { getDescCount } = usePageTreeDescCountMap();
  64. const { page } = props.itemNode;
  65. const descendantCount = getDescCount(page._id) || page.descendantCount || 0;
  66. return (
  67. <>
  68. {descendantCount > 0 && (
  69. <div className="grw-pagetree-count-wrapper">
  70. <CountBadge count={descendantCount} />
  71. </div>
  72. )}
  73. </>
  74. );
  75. };
  76. type SimpleItemProps = TreeItemProps & {
  77. itemRef?: RefObject<any> | RefCallback<any>,
  78. }
  79. export const SimpleItem: FC<SimpleItemProps> = (props) => {
  80. const {
  81. itemNode, targetPathOrId, isOpen: _isOpen = false,
  82. onRenamed, onClick, onClickDuplicateMenuItem, onClickDeleteMenuItem, isEnableActions, isReadOnlyUser,
  83. itemRef, itemClass, mainClassName,
  84. } = props;
  85. const { page, children } = itemNode;
  86. const { isProcessingSubmission } = useNewPageInput();
  87. const [currentChildren, setCurrentChildren] = useState(children);
  88. const [isOpen, setIsOpen] = useState(_isOpen);
  89. const { data } = useSWRxPageChildren(isOpen ? page._id : null);
  90. // descendantCount
  91. const { getDescCount } = usePageTreeDescCountMap();
  92. const descendantCount = getDescCount(page._id) || page.descendantCount || 0;
  93. // hasDescendants flag
  94. const isChildrenLoaded = currentChildren?.length > 0;
  95. const hasDescendants = descendantCount > 0 || isChildrenLoaded;
  96. const hasChildren = useCallback((): boolean => {
  97. return currentChildren != null && currentChildren.length > 0;
  98. }, [currentChildren]);
  99. const onClickLoadChildren = useCallback(async() => {
  100. setIsOpen(!isOpen);
  101. }, [isOpen]);
  102. // didMount
  103. useEffect(() => {
  104. if (hasChildren()) setIsOpen(true);
  105. }, [hasChildren]);
  106. /*
  107. * Make sure itemNode.children and currentChildren are synced
  108. */
  109. useEffect(() => {
  110. if (children.length > currentChildren.length) {
  111. markTarget(children, targetPathOrId);
  112. setCurrentChildren(children);
  113. }
  114. }, [children, currentChildren.length, targetPathOrId]);
  115. /*
  116. * When swr fetch succeeded
  117. */
  118. useEffect(() => {
  119. if (isOpen && data != null) {
  120. const newChildren = ItemNode.generateNodesFromPages(data.children);
  121. markTarget(newChildren, targetPathOrId);
  122. setCurrentChildren(newChildren);
  123. }
  124. }, [data, isOpen, targetPathOrId]);
  125. const ItemClassFixed = itemClass ?? SimpleItem;
  126. const EndComponents = props.customEndComponents ?? [SimpleItemTool];
  127. const baseProps: Omit<TreeItemProps, 'itemNode'> = {
  128. isEnableActions,
  129. isReadOnlyUser,
  130. isOpen: false,
  131. targetPathOrId,
  132. onRenamed,
  133. onClick,
  134. onClickDuplicateMenuItem,
  135. onClickDeleteMenuItem,
  136. };
  137. const toolProps: TreeItemToolProps = {
  138. ...baseProps,
  139. itemNode,
  140. };
  141. const CustomNextComponents = props.customNextComponents;
  142. return (
  143. <div
  144. id={`pagetree-item-${page._id}`}
  145. data-testid="grw-pagetree-item-container"
  146. className={`grw-pagetree-item-container ${mainClassName}`}
  147. >
  148. <li
  149. ref={itemRef}
  150. className={`list-group-item list-group-item-action border-0 py-0 pr-3 d-flex align-items-center
  151. ${page.isTarget ? 'grw-pagetree-current-page-item' : ''}`}
  152. id={page.isTarget ? 'grw-pagetree-current-page-item' : `grw-pagetree-list-${page._id}`}
  153. >
  154. <div className="grw-triangle-container d-flex justify-content-center">
  155. {hasDescendants && (
  156. <button
  157. type="button"
  158. className={`grw-pagetree-triangle-btn btn ${isOpen ? 'grw-pagetree-open' : ''}`}
  159. onClick={onClickLoadChildren}
  160. >
  161. <div className="d-flex justify-content-center">
  162. <span className="material-symbols-outlined">arrow_right</span>
  163. </div>
  164. </button>
  165. )}
  166. </div>
  167. <SimpleItemContent {...toolProps} />
  168. {EndComponents.map((EndComponent, index) => (
  169. // eslint-disable-next-line react/no-array-index-key
  170. <EndComponent key={index} {...toolProps} />
  171. ))}
  172. </li>
  173. {CustomNextComponents?.map((UnderItemContent, index) => (
  174. // eslint-disable-next-line react/no-array-index-key
  175. <UnderItemContent key={index} {...toolProps} />
  176. ))}
  177. {
  178. isOpen && hasChildren() && currentChildren.map((node, index) => {
  179. const itemProps = {
  180. ...baseProps,
  181. itemNode: node,
  182. itemClass,
  183. mainClassName,
  184. };
  185. return (
  186. <div key={node.page._id} className="grw-pagetree-item-children">
  187. <ItemClassFixed {...itemProps} />
  188. {isProcessingSubmission && (currentChildren.length - 1 === index) && (
  189. <div className="text-muted text-center">
  190. <i className="fa fa-spinner fa-pulse mr-1"></i>
  191. </div>
  192. )}
  193. </div>
  194. );
  195. })
  196. }
  197. </div>
  198. );
  199. };