SimpleItem.tsx 7.5 KB

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