import type { FC } from 'react'; import { useState, useMemo, useCallback } from 'react'; import nodePath from 'path'; import { pathUtils } from '@growi/core/dist/utils'; import type { Props } from './PagePathHeader'; import { TextInputForPageTitleAndPath } from './TextInputForPageTitleAndPath'; import { usePagePathRenameHandler } from './page-header-utils'; export const PageTitleHeader: FC = (props) => { const { currentPage } = props; const currentPagePath = currentPage.path; const pageTitle = nodePath.basename(currentPagePath) || '/'; const [isRenameInputShown, setRenameInputShown] = useState(false); const [editedPagePath, setEditedPagePath] = useState(currentPagePath); const editedPageTitle = nodePath.basename(editedPagePath); const onRenameFinish = () => { setRenameInputShown(false); }; const onRenameFailure = () => { setRenameInputShown(true); }; const onInputChange = useCallback((inputText: string) => { const parentPath = pathUtils.addTrailingSlash(nodePath.dirname(currentPage?.path ?? '')); const newPagePath = nodePath.resolve(parentPath, inputText); setEditedPagePath(newPagePath); }, [currentPage?.path, setEditedPagePath]); const onPressEscape = () => { setEditedPagePath(currentPagePath); setRenameInputShown(false); }; const onClickPageTitle = () => { console.log(currentPagePath); setEditedPagePath(currentPagePath); setRenameInputShown(true); }; const pagePathRenameHandler = usePagePathRenameHandler(currentPage); const stateHandler = { isRenameInputShown, setRenameInputShown }; const PageTitle = useMemo(() => (
{pageTitle}
), [pageTitle]); const buttonStyle = isRenameInputShown ? '' : 'd-none'; const onClickButton = () => { pagePathRenameHandler(editedPagePath, onRenameFinish, onRenameFailure); }; return (
); };