Item.tsx 6.3 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 { 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]);
  143. // TODO: improve style
  144. const opacityStyle = { opacity: 1.0 };
  145. if (page.isTarget) opacityStyle.opacity = 0.7;
  146. const buttonClass = isOpen ? 'rotate' : '';
  147. return (
  148. <>
  149. <div style={opacityStyle} className="grw-pagetree-item d-flex align-items-center">
  150. <button
  151. type="button"
  152. className={`grw-pagetree-button btn ${buttonClass}`}
  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. />
  172. </div>
  173. </div>
  174. {!isEnableActions && (
  175. <ClosableTextInput
  176. isShown={isNewPageInputShown}
  177. placeholder={t('Input title')}
  178. onClickOutside={() => { setNewPageInputShown(false) }}
  179. onPressEnter={onPressEnterHandler}
  180. inputValidator={inputValidator}
  181. />
  182. )}
  183. {
  184. isOpen && hasChildren() && currentChildren.map(node => (
  185. <div className="ml-3 mt-3">
  186. <Item
  187. key={node.page._id}
  188. isEnableActions={isEnableActions}
  189. itemNode={node}
  190. isOpen={false}
  191. onClickDeleteByPage={onClickDeleteByPage}
  192. />
  193. </div>
  194. ))
  195. }
  196. </>
  197. );
  198. };
  199. export default Item;