SimpleItem.tsx 7.6 KB

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