import { useMemo, useState, useEffect, useCallback, } from 'react'; import type { FC } from 'react'; import nodePath from 'path'; import type { IPagePopulatedToShowRevision } from '@growi/core'; import { pathUtils } from '@growi/core/dist/utils'; import { useTranslation } from 'next-i18next'; import { ValidationTarget } from '~/client/util/input-validator'; import { usePageSelectModal } from '~/stores/modal'; import { EditorMode, useEditorMode } from '~/stores/ui'; import ClosableTextInput from '../Common/ClosableTextInput'; import { PagePathNav } from '../Common/PagePathNav'; import { PageSelectModal } from '../PageSelectModal/PageSelectModal'; import { usePagePathRenameHandler } from './page-header-utils'; export type Props = { currentPage: IPagePopulatedToShowRevision } export const PagePathHeader: FC = (props) => { const { currentPage } = props; const currentPagePath = currentPage.path; const parentPagePath = pathUtils.addTrailingSlash(nodePath.dirname(currentPagePath)); const pageTitle = nodePath.basename(currentPagePath) || '/'; const [isRenameInputShown, setRenameInputShown] = useState(false); const [isButtonsShown, setButtonShown] = useState(false); const [editedPagePath, setEditedPagePath] = useState(currentPagePath); const { data: editorMode } = useEditorMode(); const { data: PageSelectModalData, open: openPageSelectModal } = usePageSelectModal(); const pagePathRenameHandler = usePagePathRenameHandler(currentPage); const { t } = useTranslation(); const onRenameFinish = useCallback(() => { setRenameInputShown(false); }, []); const onRenameFailure = useCallback(() => { setRenameInputShown(true); }, []); const onInputChange = useCallback((inputText: string) => { const newPagePath = nodePath.resolve(inputText, pageTitle); setEditedPagePath(newPagePath); }, [pageTitle]); const onPressEnter = useCallback(() => { pagePathRenameHandler(editedPagePath, onRenameFinish, onRenameFailure); }, [editedPagePath, onRenameFailure, onRenameFinish, pagePathRenameHandler]); const onPressEscape = useCallback(() => { setEditedPagePath(currentPagePath); setRenameInputShown(false); }, [currentPagePath]); const onClickEditButton = useCallback(() => { if (isRenameInputShown) { pagePathRenameHandler(editedPagePath, onRenameFinish, onRenameFailure); } else { setEditedPagePath(currentPagePath); setRenameInputShown(true); } }, [currentPagePath, editedPagePath, isRenameInputShown, onRenameFailure, onRenameFinish, pagePathRenameHandler]); const isOpened = PageSelectModalData?.isOpened ?? false; const isViewMode = editorMode === EditorMode.View; const isEditorMode = !isViewMode; const PagePath = useMemo(() => ( ), [currentPage._id, parentPagePath, isEditorMode]); const buttonStyle = isButtonsShown ? '' : 'd-none'; const clickOutSideHandler = useCallback((e) => { const container = document.getElementById('page-path-header'); if (container && !container.contains(e.target)) { setRenameInputShown(false); } }, []); useEffect(() => { document.addEventListener('click', clickOutSideHandler); return () => { document.removeEventListener('click', clickOutSideHandler); }; }, []); return (
setButtonShown(false)} >
setButtonShown(true)} > {isRenameInputShown ? (
) : ( <>{ PagePath } )}
{isOpened && ( )}
); };