Item.tsx 6.2 KB

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