PageCreateModal.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import React, {
  2. useEffect, useState, useMemo,
  3. } from 'react';
  4. import { pagePathUtils, pathUtils } from '@growi/core';
  5. import { format } from 'date-fns';
  6. import { useTranslation } from 'next-i18next';
  7. import { useRouter } from 'next/router';
  8. import {
  9. Modal, ModalHeader, ModalBody, UncontrolledButtonDropdown, DropdownToggle, DropdownMenu, DropdownItem,
  10. } from 'reactstrap';
  11. import { debounce } from 'throttle-debounce';
  12. import { toastError } from '~/client/util/toastr';
  13. import { useCurrentUser, useIsSearchServiceReachable } from '~/stores/context';
  14. import { usePageCreateModal } from '~/stores/modal';
  15. import { EditorMode, useEditorMode } from '~/stores/ui';
  16. import PagePathAutoComplete from './PagePathAutoComplete';
  17. import styles from './PageCreateModal.module.scss';
  18. const {
  19. userPageRoot, isCreatablePage, generateEditorPath, isUsersHomePage,
  20. } = pagePathUtils;
  21. const PageCreateModal = () => {
  22. const { t } = useTranslation();
  23. const router = useRouter();
  24. const { data: currentUser } = useCurrentUser();
  25. const { data: pageCreateModalData, close: closeCreateModal } = usePageCreateModal();
  26. const { isOpened, path } = pageCreateModalData;
  27. const { data: isReachable } = useIsSearchServiceReachable();
  28. const pathname = path || '';
  29. const userPageRootPath = userPageRoot(currentUser);
  30. const isCreatable = isCreatablePage(pathname) || isUsersHomePage(pathname);
  31. const pageNameInputInitialValue = isCreatable ? pathUtils.addTrailingSlash(pathname) : '/';
  32. const now = format(new Date(), 'yyyy/MM/dd');
  33. const { mutate: mutateEditorMode } = useEditorMode();
  34. const [todayInput1, setTodayInput1] = useState(t('Memo'));
  35. const [todayInput2, setTodayInput2] = useState('');
  36. const [pageNameInput, setPageNameInput] = useState(pageNameInputInitialValue);
  37. const [template, setTemplate] = useState(null);
  38. const [isMatchedWithUserHomePagePath, setIsMatchedWithUserHomePagePath] = useState(false);
  39. // ensure pageNameInput is synced with selectedPagePath || currentPagePath
  40. useEffect(() => {
  41. if (isOpened) {
  42. setPageNameInput(isCreatable ? pathUtils.addTrailingSlash(pathname) : '/');
  43. }
  44. }, [isOpened, pathname, isCreatable]);
  45. useEffect(() => {
  46. setTodayInput1(t('Memo'));
  47. }, [t]);
  48. const checkIsUsersHomePageDebounce = useMemo(() => {
  49. const checkIsUsersHomePage = () => {
  50. setIsMatchedWithUserHomePagePath(isUsersHomePage(pageNameInput));
  51. };
  52. return debounce(1000, checkIsUsersHomePage);
  53. }, [pageNameInput]);
  54. useEffect(() => {
  55. if (isOpened) {
  56. checkIsUsersHomePageDebounce(pageNameInput);
  57. }
  58. }, [isOpened, checkIsUsersHomePageDebounce, pageNameInput]);
  59. function transitBySubmitEvent(e, transitHandler) {
  60. // prevent page transition by submit
  61. e.preventDefault();
  62. transitHandler();
  63. }
  64. /**
  65. * change todayInput1
  66. * @param {string} value
  67. */
  68. function onChangeTodayInput1Handler(value) {
  69. setTodayInput1(value);
  70. }
  71. /**
  72. * change todayInput2
  73. * @param {string} value
  74. */
  75. function onChangeTodayInput2Handler(value) {
  76. setTodayInput2(value);
  77. }
  78. /**
  79. * change template
  80. * @param {string} value
  81. */
  82. function onChangeTemplateHandler(value) {
  83. setTemplate(value);
  84. }
  85. /**
  86. * join path, check if creatable, then redirect
  87. * @param {string} paths
  88. */
  89. async function redirectToEditor(...paths) {
  90. try {
  91. const editorPath = generateEditorPath(...paths);
  92. await router.push(editorPath);
  93. mutateEditorMode(EditorMode.Editor);
  94. // close modal
  95. closeCreateModal();
  96. }
  97. catch (err) {
  98. toastError(err);
  99. }
  100. }
  101. /**
  102. * access today page
  103. */
  104. function createTodayPage() {
  105. let tmpTodayInput1 = todayInput1;
  106. if (tmpTodayInput1 === '') {
  107. tmpTodayInput1 = t('Memo');
  108. }
  109. redirectToEditor(userPageRootPath, tmpTodayInput1, now, todayInput2);
  110. }
  111. /**
  112. * access input page
  113. */
  114. function createInputPage() {
  115. redirectToEditor(pageNameInput);
  116. }
  117. function ppacSubmitHandler(input) {
  118. redirectToEditor(input);
  119. }
  120. /**
  121. * access template page
  122. */
  123. function createTemplatePage(e) {
  124. const pageName = (template === 'children') ? '_template' : '__template';
  125. redirectToEditor(pathname, pageName);
  126. }
  127. function renderCreateTodayForm() {
  128. if (!isOpened) {
  129. return <></>;
  130. }
  131. return (
  132. <div className="row">
  133. <fieldset className="col-12 mb-4">
  134. <h3 className="grw-modal-head pb-2">{t("Create today's")}</h3>
  135. <div className="d-sm-flex align-items-center justify-items-between">
  136. <div className="d-flex align-items-center flex-fill flex-wrap flex-lg-nowrap">
  137. <div className="d-flex align-items-center">
  138. <span>{userPageRootPath}/</span>
  139. <form onSubmit={e => transitBySubmitEvent(e, createTodayPage)}>
  140. <input
  141. type="text"
  142. className="page-today-input1 form-control text-center mx-2"
  143. value={todayInput1}
  144. onChange={e => onChangeTodayInput1Handler(e.target.value)}
  145. />
  146. </form>
  147. <span className="page-today-suffix">/{now}/</span>
  148. </div>
  149. <form className="mt-1 mt-lg-0 ml-lg-2 w-100" onSubmit={e => transitBySubmitEvent(e, createTodayPage)}>
  150. <input
  151. type="text"
  152. className="page-today-input2 form-control w-100"
  153. id="page-today-input2"
  154. placeholder={t('Input page name (optional)')}
  155. value={todayInput2}
  156. onChange={e => onChangeTodayInput2Handler(e.target.value)}
  157. />
  158. </form>
  159. </div>
  160. <div className="d-flex justify-content-end mt-1 mt-sm-0">
  161. <button
  162. type="button"
  163. data-testid="btn-create-memo"
  164. className="grw-btn-create-page btn btn-outline-primary rounded-pill text-nowrap ml-3"
  165. onClick={createTodayPage}
  166. >
  167. <i className="icon-fw icon-doc"></i>{t('Create')}
  168. </button>
  169. </div>
  170. </div>
  171. </fieldset>
  172. </div>
  173. );
  174. }
  175. function renderInputPageForm() {
  176. if (!isOpened) {
  177. return <></>;
  178. }
  179. return (
  180. <div className="row" data-testid="row-create-page-under-below">
  181. <fieldset className="col-12 mb-4">
  182. <h3 className="grw-modal-head pb-2">{t('Create under')}</h3>
  183. <div className="d-sm-flex align-items-center justify-items-between">
  184. <div className="flex-fill">
  185. {isReachable
  186. ? (
  187. <PagePathAutoComplete
  188. initializedPath={pageNameInputInitialValue}
  189. addTrailingSlash
  190. onSubmit={ppacSubmitHandler}
  191. onInputChange={value => setPageNameInput(value)}
  192. autoFocus
  193. />
  194. )
  195. : (
  196. <form onSubmit={e => transitBySubmitEvent(e, createInputPage)}>
  197. <input
  198. type="text"
  199. value={pageNameInput}
  200. className="form-control flex-fill"
  201. placeholder={t('Input page name')}
  202. onChange={e => setPageNameInput(e.target.value)}
  203. required
  204. />
  205. </form>
  206. )}
  207. </div>
  208. <div className="d-flex justify-content-end mt-1 mt-sm-0">
  209. <button
  210. type="button"
  211. data-testid="btn-create-page-under-below"
  212. className="grw-btn-create-page btn btn-outline-primary rounded-pill text-nowrap ml-3"
  213. onClick={createInputPage}
  214. disabled={isMatchedWithUserHomePagePath}
  215. >
  216. <i className="icon-fw icon-doc"></i>{t('Create')}
  217. </button>
  218. </div>
  219. </div>
  220. { isMatchedWithUserHomePagePath && (
  221. <p className="text-danger mt-2">Error: Cannot create page under /user page directory.</p>
  222. ) }
  223. </fieldset>
  224. </div>
  225. );
  226. }
  227. function renderTemplatePageForm() {
  228. if (!isOpened) {
  229. return <></>;
  230. }
  231. return (
  232. <div className="row">
  233. <fieldset className="col-12">
  234. <h3 className="grw-modal-head pb-2">
  235. {t('template.modal_label.Create template under')}<br />
  236. <code className="h6" data-testid="grw-page-create-modal-path-name">{pathname}</code>
  237. </h3>
  238. <div className="d-sm-flex align-items-center justify-items-between">
  239. <UncontrolledButtonDropdown id="dd-template-type" className='flex-fill text-center'>
  240. <DropdownToggle id="template-type" caret>
  241. {template == null && t('template.option_label.select')}
  242. {template === 'children' && t('template.children.label')}
  243. {template === 'decendants' && t('template.decendants.label')}
  244. </DropdownToggle>
  245. <DropdownMenu>
  246. <DropdownItem onClick={() => onChangeTemplateHandler('children')}>
  247. {t('template.children.label')} (_template)<br className="d-block d-md-none" />
  248. <small className="text-muted text-wrap">- {t('template.children.desc')}</small>
  249. </DropdownItem>
  250. <DropdownItem onClick={() => onChangeTemplateHandler('decendants')}>
  251. {t('template.decendants.label')} (__template) <br className="d-block d-md-none" />
  252. <small className="text-muted">- {t('template.decendants.desc')}</small>
  253. </DropdownItem>
  254. </DropdownMenu>
  255. </UncontrolledButtonDropdown>
  256. <div className="d-flex justify-content-end mt-1 mt-sm-0">
  257. <button
  258. data-testid="grw-btn-edit-page"
  259. type="button"
  260. className='grw-btn-create-page btn btn-outline-primary rounded-pill text-nowrap ml-3'
  261. onClick={createTemplatePage}
  262. disabled={template == null}
  263. >
  264. <i className="icon-fw icon-doc"></i>{t('Edit')}
  265. </button>
  266. </div>
  267. </div>
  268. </fieldset>
  269. </div>
  270. );
  271. }
  272. return (
  273. <Modal
  274. size="lg"
  275. isOpen={isOpened}
  276. toggle={() => closeCreateModal()}
  277. data-testid="page-create-modal"
  278. className={`grw-create-page ${styles['grw-create-page']}`}
  279. autoFocus={false}
  280. >
  281. <ModalHeader tag="h4" toggle={() => closeCreateModal()} className="bg-primary text-light">
  282. {t('New Page')}
  283. </ModalHeader>
  284. <ModalBody>
  285. {renderCreateTodayForm()}
  286. {renderInputPageForm()}
  287. {renderTemplatePageForm()}
  288. </ModalBody>
  289. </Modal>
  290. );
  291. };
  292. export default PageCreateModal;