SimpleItem.tsx 7.8 KB

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