Item.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import React, {
  2. useCallback, useState, FC, useEffect, memo,
  3. } from 'react';
  4. import nodePath from 'path';
  5. import { useTranslation } from 'react-i18next';
  6. import { pagePathUtils } from '@growi/core';
  7. import { useDrag, useDrop } from 'react-dnd';
  8. import { toastWarning } from '~/client/util/apiNotification';
  9. import { ItemNode } from './ItemNode';
  10. import { IPageHasId } from '~/interfaces/page';
  11. import { useSWRxPageChildren } from '../../../stores/page-listing';
  12. import ClosableTextInput, { AlertInfo, AlertType } from '../../Common/ClosableTextInput';
  13. import PageItemControl from '../../Common/Dropdown/PageItemControl';
  14. import { IPageForPageDeleteModal } from '~/components/PageDeleteModal';
  15. import TriangleIcon from '~/components/Icons/TriangleIcon';
  16. const { isTopPage } = pagePathUtils;
  17. interface ItemProps {
  18. isEnableActions: boolean
  19. itemNode: ItemNode
  20. targetPathOrId?: string
  21. isOpen?: boolean
  22. onClickDeleteByPage?(page: IPageForPageDeleteModal): void
  23. }
  24. // Utility to mark target
  25. const markTarget = (children: ItemNode[], targetPathOrId?: string): void => {
  26. if (targetPathOrId == null) {
  27. return;
  28. }
  29. children.forEach((node) => {
  30. if (node.page._id === targetPathOrId || node.page.path === targetPathOrId) {
  31. node.page.isTarget = true;
  32. }
  33. return node;
  34. });
  35. };
  36. type ItemControlProps = {
  37. page: Partial<IPageHasId>
  38. isEnableActions: boolean
  39. isDeletable: boolean
  40. onClickDeleteButtonHandler?(): void
  41. onClickPlusButtonHandler?(): void
  42. }
  43. const ItemControl: FC<ItemControlProps> = memo((props: ItemControlProps) => {
  44. const onClickPlusButton = () => {
  45. if (props.onClickPlusButtonHandler == null) {
  46. return;
  47. }
  48. props.onClickPlusButtonHandler();
  49. };
  50. const onClickDeleteButton = () => {
  51. if (props.onClickDeleteButtonHandler == null) {
  52. return;
  53. }
  54. props.onClickDeleteButtonHandler();
  55. };
  56. if (props.page == null) {
  57. return <></>;
  58. }
  59. return (
  60. <>
  61. <PageItemControl page={props.page} onClickDeleteButton={onClickDeleteButton} isEnableActions={props.isEnableActions} isDeletable={props.isDeletable} />
  62. <button
  63. type="button"
  64. className="border-0 rounded grw-btn-page-management p-0"
  65. onClick={onClickPlusButton}
  66. >
  67. <i className="icon-plus text-muted d-block p-1" />
  68. </button>
  69. </>
  70. );
  71. });
  72. const ItemCount: FC = () => {
  73. return (
  74. <>
  75. <span className="grw-pagetree-count badge badge-pill badge-light text-muted">
  76. {/* TODO: consider to show the number of children pages */}
  77. 00
  78. </span>
  79. </>
  80. );
  81. };
  82. const Item: FC<ItemProps> = (props: ItemProps) => {
  83. const { t } = useTranslation();
  84. const {
  85. itemNode, targetPathOrId, isOpen: _isOpen = false, onClickDeleteByPage, isEnableActions,
  86. } = props;
  87. const { page, children } = itemNode;
  88. const [currentChildren, setCurrentChildren] = useState(children);
  89. const [isOpen, setIsOpen] = useState(_isOpen);
  90. const [isNewPageInputShown, setNewPageInputShown] = useState(false);
  91. const { data, error } = useSWRxPageChildren(isOpen ? page._id : null);
  92. const [{ isDragging }, drag] = useDrag(() => ({
  93. type: 'PAGE_TREE',
  94. item: { page },
  95. collect: monitor => ({
  96. isDragging: monitor.isDragging(),
  97. }),
  98. }));
  99. const pageItemDropHandler = () => {
  100. // TODO: hit an api to rename the page by 85175
  101. // eslint-disable-next-line no-console
  102. console.log('pageItem was droped!!');
  103. };
  104. const [{ isOver }, drop] = useDrop(() => ({
  105. accept: 'PAGE_TREE',
  106. drop: pageItemDropHandler,
  107. hover: (item, monitor) => {
  108. // when a drag item is overlapped more than 1 sec, the drop target item will be opened.
  109. if (monitor.isOver()) {
  110. setTimeout(() => {
  111. if (monitor.isOver()) {
  112. setIsOpen(true);
  113. }
  114. }, 1000);
  115. }
  116. },
  117. collect: monitor => ({
  118. isOver: monitor.isOver(),
  119. }),
  120. }));
  121. const hasChildren = useCallback((): boolean => {
  122. return currentChildren != null && currentChildren.length > 0;
  123. }, [currentChildren]);
  124. const onClickLoadChildren = useCallback(async() => {
  125. setIsOpen(!isOpen);
  126. }, [isOpen]);
  127. const onClickDeleteButtonHandler = useCallback(() => {
  128. if (onClickDeleteByPage == null) {
  129. return;
  130. }
  131. const { _id: pageId, revision: revisionId, path } = page;
  132. if (pageId == null || revisionId == null || path == null) {
  133. throw Error('Any of _id, revision, and path must not be null.');
  134. }
  135. const pageToDelete: IPageForPageDeleteModal = {
  136. pageId,
  137. revisionId: revisionId as string,
  138. path,
  139. };
  140. onClickDeleteByPage(pageToDelete);
  141. }, [page, onClickDeleteByPage]);
  142. const inputValidator = (title: string | null): AlertInfo | null => {
  143. if (title == null || title === '') {
  144. return {
  145. type: AlertType.WARNING,
  146. message: t('form_validation.title_required'),
  147. };
  148. }
  149. return null;
  150. };
  151. // TODO: go to create page page
  152. const onPressEnterHandler = () => {
  153. toastWarning(t('search_result.currently_not_implemented'));
  154. };
  155. // didMount
  156. useEffect(() => {
  157. if (hasChildren()) setIsOpen(true);
  158. }, []);
  159. /*
  160. * Make sure itemNode.children and currentChildren are synced
  161. */
  162. useEffect(() => {
  163. if (children.length > currentChildren.length) {
  164. markTarget(children, targetPathOrId);
  165. setCurrentChildren(children);
  166. }
  167. }, []);
  168. /*
  169. * When swr fetch succeeded
  170. */
  171. useEffect(() => {
  172. if (isOpen && error == null && data != null) {
  173. const newChildren = ItemNode.generateNodesFromPages(data.children);
  174. markTarget(newChildren, targetPathOrId);
  175. setCurrentChildren(newChildren);
  176. }
  177. }, [data, isOpen]);
  178. return (
  179. <div className={`grw-pagetree-item-container ${isOver ? 'grw-pagetree-is-over' : ''}`}>
  180. <div
  181. ref={(c) => { drag(c); drop(c) }}
  182. className={`grw-pagetree-item d-flex align-items-center pr-1 ${page.isTarget ? 'grw-pagetree-is-target' : ''}`}
  183. >
  184. <button
  185. type="button"
  186. className={`grw-pagetree-button btn ${isOpen ? 'grw-pagetree-open' : ''}`}
  187. onClick={onClickLoadChildren}
  188. >
  189. <div className="grw-triangle-icon">
  190. <TriangleIcon />
  191. </div>
  192. </button>
  193. <a href={page._id} className="grw-pagetree-title-anchor flex-grow-1">
  194. <p className={`text-truncate m-auto ${page.isEmpty && 'text-muted'}`}>{nodePath.basename(page.path as string) || '/'}</p>
  195. </a>
  196. <div className="grw-pagetree-count-wrapper">
  197. <ItemCount />
  198. </div>
  199. <div className="grw-pagetree-control d-none">
  200. <ItemControl
  201. page={page}
  202. onClickDeleteButtonHandler={onClickDeleteButtonHandler}
  203. onClickPlusButtonHandler={() => { setNewPageInputShown(true) }}
  204. isEnableActions={isEnableActions}
  205. isDeletable={!page.isEmpty && !isTopPage(page.path as string)}
  206. />
  207. </div>
  208. </div>
  209. {isEnableActions && (
  210. <ClosableTextInput
  211. isShown={isNewPageInputShown}
  212. placeholder={t('Input page name')}
  213. onClickOutside={() => { setNewPageInputShown(false) }}
  214. onPressEnter={onPressEnterHandler}
  215. inputValidator={inputValidator}
  216. />
  217. )}
  218. {
  219. isOpen && hasChildren() && currentChildren.map(node => (
  220. <div key={node.page._id} className="grw-pagetree-item-children">
  221. <Item
  222. isEnableActions={isEnableActions}
  223. itemNode={node}
  224. isOpen={false}
  225. targetPathOrId={targetPathOrId}
  226. onClickDeleteByPage={onClickDeleteByPage}
  227. />
  228. </div>
  229. ))
  230. }
  231. </div>
  232. );
  233. };
  234. export default Item;