import { useState, useEffect, useCallback } from 'react'; import type { FC } from 'react'; import type { IPagePopulatedToShowRevision } from '@growi/core'; import { useTranslation } from 'next-i18next'; import { ValidationTarget } from '~/client/util/input-validator'; import LinkedPagePath from '~/models/linked-page-path'; import { usePageSelectModal } from '~/stores/modal'; import ClosableTextInput from '../Common/ClosableTextInput'; import { PagePathHierarchicalLink } from '../Common/PagePathHierarchicalLink'; import { PageSelectModal } from '../PageSelectModal/PageSelectModal'; import { usePagePathRenameHandler } from './page-header-utils'; import styles from './PagePathHeader.module.scss'; const moduleClass = styles['page-path-header']; export type Props = { currentPage: IPagePopulatedToShowRevision } export const PagePathHeader: FC = (props) => { const { t } = useTranslation(); const { currentPage } = props; const currentPagePath = currentPage.path; const linkedPagePath = new LinkedPagePath(currentPagePath); const [isRenameInputShown, setRenameInputShown] = useState(false); const [isButtonsShown, setButtonShown] = useState(false); const [editedPagePath, setEditedPagePath] = useState(currentPagePath); const { data: PageSelectModalData, open: openPageSelectModal } = usePageSelectModal(); const isOpened = PageSelectModalData?.isOpened ?? false; const pagePathRenameHandler = usePagePathRenameHandler(currentPage); const onRenameFinish = useCallback(() => { setRenameInputShown(false); }, []); const onRenameFailure = useCallback(() => { setRenameInputShown(true); }, []); const onInputChange = useCallback((inputText: string) => { setEditedPagePath(inputText); }, []); 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 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); }; }, [clickOutSideHandler]); return (
setButtonShown(true)} onMouseLeave={() => setButtonShown(false)} >
{isRenameInputShown ? ( ) : ( ) }
{isOpened && }
); };