Item.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. onClickPlusButton?(): void
  41. onClickDeleteButton?(): void
  42. onClickRenameButton?(): void
  43. }
  44. const ItemControl: FC<ItemControlProps> = memo((props: ItemControlProps) => {
  45. const onClickPlusButton = () => {
  46. console.log('プラスボタン!');
  47. if (props.onClickPlusButton == null) {
  48. return;
  49. }
  50. props.onClickPlusButton();
  51. };
  52. const onClickDeleteButtonHandler = () => {
  53. if (props.onClickDeleteButton == null) {
  54. return;
  55. }
  56. props.onClickDeleteButton();
  57. };
  58. const onClickRenameButtonHandler = () => {
  59. if (props.onClickRenameButton == null) {
  60. return;
  61. }
  62. props.onClickRenameButton();
  63. };
  64. if (props.page == null) {
  65. return <></>;
  66. }
  67. return (
  68. <>
  69. <PageItemControl
  70. page={props.page}
  71. onClickDeleteButtonHandler={onClickDeleteButtonHandler}
  72. isEnableActions={props.isEnableActions}
  73. isDeletable={props.isDeletable}
  74. onClickRenameButtonHandler={onClickRenameButtonHandler}
  75. />
  76. <button
  77. type="button"
  78. className="border-0 rounded grw-btn-page-management p-0"
  79. onClick={onClickPlusButton}
  80. >
  81. <i className="icon-plus text-muted d-block p-1" />
  82. </button>
  83. </>
  84. );
  85. });
  86. const ItemCount: FC = () => {
  87. return (
  88. <>
  89. <span className="grw-pagetree-count badge badge-pill badge-light text-muted">
  90. {/* TODO: consider to show the number of children pages */}
  91. 00
  92. </span>
  93. </>
  94. );
  95. };
  96. const Item: FC<ItemProps> = (props: ItemProps) => {
  97. const { t } = useTranslation();
  98. const {
  99. itemNode, targetPathOrId, isOpen: _isOpen = false, onClickDeleteByPage, isEnableActions,
  100. } = props;
  101. const { page, children } = itemNode;
  102. const [currentChildren, setCurrentChildren] = useState(children);
  103. const [isOpen, setIsOpen] = useState(_isOpen);
  104. const [isNewPageInputShown, setNewPageInputShown] = useState(false);
  105. const [isRenameInputShown, setRenameInputShown] = useState(false);
  106. const { data, error } = useSWRxPageChildren(isOpen ? page._id : null);
  107. const [{ isDragging }, drag] = useDrag(() => ({
  108. type: 'PAGE_TREE',
  109. item: { page },
  110. collect: monitor => ({
  111. isDragging: monitor.isDragging(),
  112. }),
  113. }));
  114. const pageItemDropHandler = () => {
  115. // TODO: hit an api to rename the page by 85175
  116. // eslint-disable-next-line no-console
  117. console.log('pageItem was droped!!');
  118. };
  119. const [{ isOver }, drop] = useDrop(() => ({
  120. accept: 'PAGE_TREE',
  121. drop: pageItemDropHandler,
  122. hover: (item, monitor) => {
  123. // when a drag item is overlapped more than 1 sec, the drop target item will be opened.
  124. if (monitor.isOver()) {
  125. setTimeout(() => {
  126. if (monitor.isOver()) {
  127. setIsOpen(true);
  128. }
  129. }, 1000);
  130. }
  131. },
  132. collect: monitor => ({
  133. isOver: monitor.isOver(),
  134. }),
  135. }));
  136. const hasChildren = useCallback((): boolean => {
  137. return currentChildren != null && currentChildren.length > 0;
  138. }, [currentChildren]);
  139. const onClickLoadChildren = useCallback(async() => {
  140. setIsOpen(!isOpen);
  141. }, [isOpen]);
  142. const onClickPlusButton = useCallback(() => {
  143. setNewPageInputShown(true);
  144. }, []);
  145. const onClickDeleteButton = useCallback(() => {
  146. if (onClickDeleteByPage == null) {
  147. return;
  148. }
  149. const { _id: pageId, revision: revisionId, path } = page;
  150. if (pageId == null || revisionId == null || path == null) {
  151. throw Error('Any of _id, revision, and path must not be null.');
  152. }
  153. const pageToDelete: IPageForPageDeleteModal = {
  154. pageId,
  155. revisionId: revisionId as string,
  156. path,
  157. };
  158. onClickDeleteByPage(pageToDelete);
  159. }, [page, onClickDeleteByPage]);
  160. const onClickRenameButton = useCallback(() => {
  161. setRenameInputShown(true);
  162. }, []);
  163. const onPressEnterForRenameHandler = () => {
  164. toastWarning(t('search_result.currently_not_implemented'));
  165. setRenameInputShown(false);
  166. };
  167. const inputValidator = (title: string | null): AlertInfo | null => {
  168. if (title == null || title === '') {
  169. return {
  170. type: AlertType.WARNING,
  171. message: t('form_validation.title_required'),
  172. };
  173. }
  174. return null;
  175. };
  176. // TODO: go to create page page
  177. const onPressEnterForCreateHandler = () => {
  178. toastWarning(t('search_result.currently_not_implemented'));
  179. };
  180. // didMount
  181. useEffect(() => {
  182. if (hasChildren()) setIsOpen(true);
  183. }, []);
  184. /*
  185. * Make sure itemNode.children and currentChildren are synced
  186. */
  187. useEffect(() => {
  188. if (children.length > currentChildren.length) {
  189. markTarget(children, targetPathOrId);
  190. setCurrentChildren(children);
  191. }
  192. }, []);
  193. /*
  194. * When swr fetch succeeded
  195. */
  196. useEffect(() => {
  197. if (isOpen && error == null && data != null) {
  198. const newChildren = ItemNode.generateNodesFromPages(data.children);
  199. markTarget(newChildren, targetPathOrId);
  200. setCurrentChildren(newChildren);
  201. }
  202. }, [data, isOpen]);
  203. return (
  204. <div className={`grw-pagetree-item-container ${isOver ? 'grw-pagetree-is-over' : ''}`}>
  205. <li
  206. ref={(c) => { drag(c); drop(c) }}
  207. className={`list-group-item list-group-item-action border-0 py-1 d-flex align-items-center ${page.isTarget ? 'grw-pagetree-is-target' : ''}`}
  208. >
  209. <button
  210. type="button"
  211. className={`grw-pagetree-button btn ${isOpen ? 'grw-pagetree-open' : ''}`}
  212. onClick={onClickLoadChildren}
  213. >
  214. <div className="grw-triangle-icon">
  215. <TriangleIcon />
  216. </div>
  217. </button>
  218. { isRenameInputShown && (
  219. <ClosableTextInput
  220. isShown
  221. placeholder={t('Input page name')}
  222. onClickOutside={() => { setRenameInputShown(false) }}
  223. onPressEnter={onPressEnterForRenameHandler}
  224. inputValidator={inputValidator}
  225. />
  226. )}
  227. { !isRenameInputShown && (
  228. <a href={page._id} className="grw-pagetree-title-anchor flex-grow-1">
  229. <p className={`text-truncate m-auto ${page.isEmpty && 'text-muted'}`}>{nodePath.basename(page.path as string) || '/'}</p>
  230. </a>
  231. )}
  232. <div className="grw-pagetree-count-wrapper">
  233. <ItemCount />
  234. </div>
  235. <div className="grw-pagetree-control d-none">
  236. <ItemControl
  237. page={page}
  238. onClickPlusButton={onClickPlusButton}
  239. onClickDeleteButton={onClickDeleteButton}
  240. onClickRenameButton={onClickRenameButton}
  241. isEnableActions={isEnableActions}
  242. isDeletable={!page.isEmpty && !isTopPage(page.path as string)}
  243. />
  244. </div>
  245. </li>
  246. {isEnableActions && (
  247. <ClosableTextInput
  248. isShown={isNewPageInputShown}
  249. placeholder={t('Input page name')}
  250. onClickOutside={() => { setNewPageInputShown(false) }}
  251. onPressEnter={onPressEnterForCreateHandler}
  252. inputValidator={inputValidator}
  253. />
  254. )}
  255. {
  256. isOpen && hasChildren() && currentChildren.map(node => (
  257. <div key={node.page._id} className="grw-pagetree-item-children">
  258. <Item
  259. isEnableActions={isEnableActions}
  260. itemNode={node}
  261. isOpen={false}
  262. targetPathOrId={targetPathOrId}
  263. onClickDeleteByPage={onClickDeleteByPage}
  264. />
  265. </div>
  266. ))
  267. }
  268. </div>
  269. );
  270. };
  271. export default Item;