import React, { useEffect, useState, useMemo, useCallback, } from 'react'; import { pagePathUtils, pathUtils } from '@growi/core'; import { format } from 'date-fns'; import PropTypes from 'prop-types'; import { useTranslation } from 'react-i18next'; import { Modal, ModalHeader, ModalBody } from 'reactstrap'; import { debounce } from 'throttle-debounce'; import AppContainer from '~/client/services/AppContainer'; import { toastError } from '~/client/util/apiNotification'; import { useCurrentUser } from '~/stores/context'; import { usePageCreateModal } from '~/stores/modal'; import PagePathAutoComplete from './PagePathAutoComplete'; import { withUnstatedContainers } from './UnstatedUtils'; const { userPageRoot, isCreatablePage, generateEditorPath, isUsersHomePage, } = pagePathUtils; const PageCreateModal = (props) => { const { t } = useTranslation(); const { appContainer } = props; const { data: currentUser } = useCurrentUser(); const { data: pageCreateModalData, close: closeCreateModal } = usePageCreateModal(); const { isOpened, path } = pageCreateModalData; const config = appContainer.getConfig(); const isReachable = config.isSearchServiceReachable; const pathname = path || ''; const userPageRootPath = userPageRoot(currentUser); const isCreatable = isCreatablePage(pathname) || isUsersHomePage(pathname); const pageNameInputInitialValue = isCreatable ? pathUtils.addTrailingSlash(pathname) : '/'; const now = format(new Date(), 'yyyy/MM/dd'); const [todayInput1, setTodayInput1] = useState(t('Memo')); const [todayInput2, setTodayInput2] = useState(''); const [pageNameInput, setPageNameInput] = useState(pageNameInputInitialValue); const [template, setTemplate] = useState(null); const [isMatchedWithUserHomePagePath, setIsMatchedWithUserHomePagePath] = useState(false); // ensure pageNameInput is synced with selectedPagePath || currentPagePath useEffect(() => { setPageNameInput(isCreatable ? pathUtils.addTrailingSlash(pathname) : '/'); }, [pathname, isCreatable]); const checkIsUsersHomePageDebounce = useMemo(() => { const checkIsUsersHomePage = () => { setIsMatchedWithUserHomePagePath(isUsersHomePage(pageNameInput)); }; return debounce(1000, checkIsUsersHomePage); }, [pageNameInput]); useEffect(() => { checkIsUsersHomePageDebounce(pageNameInput); }, [checkIsUsersHomePageDebounce, pageNameInput]); function transitBySubmitEvent(e, transitHandler) { // prevent page transition by submit e.preventDefault(); transitHandler(); } /** * change todayInput1 * @param {string} value */ function onChangeTodayInput1Handler(value) { setTodayInput1(value); } /** * change todayInput2 * @param {string} value */ function onChangeTodayInput2Handler(value) { setTodayInput2(value); } /** * change template * @param {string} value */ function onChangeTemplateHandler(value) { setTemplate(value); } /** * join path, check if creatable, then redirect * @param {string} paths */ async function redirectToEditor(...paths) { try { const editorPath = await generateEditorPath(...paths); window.location.href = editorPath; } catch (err) { toastError(err); } } /** * access today page */ function createTodayPage() { let tmpTodayInput1 = todayInput1; if (tmpTodayInput1 === '') { tmpTodayInput1 = t('Memo'); } redirectToEditor(userPageRootPath, tmpTodayInput1, now, todayInput2); } /** * access input page */ function createInputPage() { redirectToEditor(pageNameInput); } function ppacSubmitHandler(input) { redirectToEditor(input); } /** * access template page */ function createTemplatePage(e) { const pageName = (template === 'children') ? '_template' : '__template'; redirectToEditor(pathname, pageName); } function renderCreateTodayForm() { return (