Item.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 { ItemNode } from './ItemNode';
  8. import { IPageHasId } from '~/interfaces/page';
  9. import { useSWRxPageChildren } from '../../../stores/page-listing';
  10. import ClosableTextInput, { AlertInfo, AlertType } from '../../Common/ClosableTextInput';
  11. import PageItemControl from '../../Common/Dropdown/PageItemControl';
  12. import { IPageForPageDeleteModal } from '~/components/PageDeleteModal';
  13. import TriangleIcon from '~/components/Icons/TriangleIcon';
  14. const { isTopPage } = pagePathUtils;
  15. interface ItemProps {
  16. isEnableActions: boolean
  17. itemNode: ItemNode
  18. targetId?: string
  19. isOpen?: boolean
  20. onClickDeleteByPage?(page: IPageForPageDeleteModal): void
  21. }
  22. // Utility to mark target
  23. const markTarget = (children: ItemNode[], targetId?: string): void => {
  24. if (targetId == null) {
  25. return;
  26. }
  27. children.forEach((node) => {
  28. if (node.page._id === targetId) {
  29. node.page.isTarget = true;
  30. }
  31. return node;
  32. });
  33. };
  34. type ItemControlProps = {
  35. page: Partial<IPageHasId>
  36. isEnableActions: boolean
  37. isDeletable: boolean
  38. onClickDeleteButtonHandler?(): void
  39. onClickPlusButtonHandler?(): void
  40. }
  41. const ItemControl: FC<ItemControlProps> = memo((props: ItemControlProps) => {
  42. const onClickPlusButton = () => {
  43. if (props.onClickPlusButtonHandler == null) {
  44. return;
  45. }
  46. props.onClickPlusButtonHandler();
  47. };
  48. const onClickDeleteButton = () => {
  49. if (props.onClickDeleteButtonHandler == null) {
  50. return;
  51. }
  52. props.onClickDeleteButtonHandler();
  53. };
  54. if (props.page == null) {
  55. return <></>;
  56. }
  57. return (
  58. <>
  59. <PageItemControl page={props.page} onClickDeleteButton={onClickDeleteButton} isEnableActions={props.isEnableActions} isDeletable={props.isDeletable} />
  60. <button
  61. type="button"
  62. className="border-0 rounded grw-btn-page-management p-0"
  63. onClick={onClickPlusButton}
  64. >
  65. <i className="icon-plus text-muted d-block p-1" />
  66. </button>
  67. </>
  68. );
  69. });
  70. const ItemCount: FC = () => {
  71. return (
  72. <>
  73. <span className="grw-pagetree-count badge badge-pill badge-light">
  74. {/* TODO: consider to show the number of children pages */}
  75. </span>
  76. </>
  77. );
  78. };
  79. const Item: FC<ItemProps> = (props: ItemProps) => {
  80. const { t } = useTranslation();
  81. const {
  82. itemNode, targetId, isOpen: _isOpen = false, onClickDeleteByPage, isEnableActions,
  83. } = props;
  84. const { page, children } = itemNode;
  85. const [currentChildren, setCurrentChildren] = useState(children);
  86. const [isOpen, setIsOpen] = useState(_isOpen);
  87. const [isNewPageInputShown, setNewPageInputShown] = useState(false);
  88. const { data, error } = useSWRxPageChildren(isOpen ? page._id : null);
  89. const hasChildren = useCallback((): boolean => {
  90. return currentChildren != null && currentChildren.length > 0;
  91. }, [currentChildren]);
  92. const onClickLoadChildren = useCallback(async() => {
  93. setIsOpen(!isOpen);
  94. }, [isOpen]);
  95. const onClickDeleteButtonHandler = useCallback(() => {
  96. if (onClickDeleteByPage == null) {
  97. return;
  98. }
  99. const { _id: pageId, revision: revisionId, path } = page;
  100. if (pageId == null || revisionId == null || path == null) {
  101. throw Error('Any of _id, revision, and path must not be null.');
  102. }
  103. const pageToDelete: IPageForPageDeleteModal = {
  104. pageId,
  105. revisionId: revisionId as string,
  106. path,
  107. };
  108. onClickDeleteByPage(pageToDelete);
  109. }, [page, onClickDeleteByPage]);
  110. const inputValidator = (title: string | null): AlertInfo | null => {
  111. if (title == null || title === '') {
  112. return {
  113. type: AlertType.ERROR,
  114. message: t('Page title is required'),
  115. };
  116. return;
  117. }
  118. return null;
  119. };
  120. // TODO: go to create page page
  121. const onPressEnterHandler = () => {
  122. console.log('Enter key was pressed!');
  123. };
  124. // didMount
  125. useEffect(() => {
  126. if (hasChildren()) setIsOpen(true);
  127. }, []);
  128. /*
  129. * Make sure itemNode.children and currentChildren are synced
  130. */
  131. useEffect(() => {
  132. if (children.length > currentChildren.length) {
  133. markTarget(children, targetId);
  134. setCurrentChildren(children);
  135. }
  136. }, []);
  137. /*
  138. * When swr fetch succeeded
  139. */
  140. useEffect(() => {
  141. if (isOpen && error == null && data != null) {
  142. const newChildren = ItemNode.generateNodesFromPages(data.children);
  143. markTarget(newChildren, targetId);
  144. setCurrentChildren(newChildren);
  145. }
  146. }, [data, isOpen]);
  147. return (
  148. <>
  149. <div className={`grw-pagetree-item d-flex align-items-center pr-1 ${page.isTarget ? 'grw-pagetree-is-target' : ''}`}>
  150. <button
  151. type="button"
  152. className={`grw-pagetree-button btn ${isOpen ? 'grw-pagetree-open' : ''}`}
  153. onClick={onClickLoadChildren}
  154. >
  155. <div className="grw-triangle-icon">
  156. <TriangleIcon />
  157. </div>
  158. </button>
  159. <a href={page._id} className="grw-pagetree-title-anchor flex-grow-1">
  160. <p className={`grw-pagetree-title m-auto ${page.isEmpty && 'text-muted'}`}>{nodePath.basename(page.path as string) || '/'}</p>
  161. </a>
  162. <div className="grw-pagetree-count-wrapper">
  163. <ItemCount />
  164. </div>
  165. <div className="grw-pagetree-control d-none">
  166. <ItemControl
  167. page={page}
  168. onClickDeleteButtonHandler={onClickDeleteButtonHandler}
  169. onClickPlusButtonHandler={() => { setNewPageInputShown(true) }}
  170. isEnableActions={isEnableActions}
  171. isDeletable={!page.isEmpty && !isTopPage(page.path as string)}
  172. />
  173. </div>
  174. </div>
  175. {isEnableActions && (
  176. <ClosableTextInput
  177. isShown={isNewPageInputShown}
  178. placeholder={t('Input title')}
  179. onClickOutside={() => { setNewPageInputShown(false) }}
  180. onPressEnter={onPressEnterHandler}
  181. inputValidator={inputValidator}
  182. />
  183. )}
  184. {
  185. isOpen && hasChildren() && currentChildren.map(node => (
  186. <div key={node.page._id} className="ml-3 mt-2">
  187. <Item
  188. isEnableActions={isEnableActions}
  189. itemNode={node}
  190. isOpen={false}
  191. targetId={targetId}
  192. onClickDeleteByPage={onClickDeleteByPage}
  193. />
  194. </div>
  195. ))
  196. }
  197. </>
  198. );
  199. };
  200. export default Item;