| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 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> = (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 (
- <div
- id="page-path-header"
- className={`d-flex ${moduleClass}`}
- onMouseEnter={() => setButtonShown(true)}
- onMouseLeave={() => setButtonShown(false)}
- >
- <div className="me-2">
- {isRenameInputShown
- ? (
- <ClosableTextInput
- useAutosizeInput
- value={editedPagePath}
- placeholder={t('Input page name')}
- onPressEnter={onPressEnter}
- onPressEscape={onPressEscape}
- onChange={onInputChange}
- validationTarget={ValidationTarget.PAGE}
- />
- )
- : (
- <PagePathHierarchicalLink linkedPagePath={linkedPagePath} />
- )
- }
- </div>
- <div className={`page-path-header-buttons d-flex align-items-center ${isButtonsShown ? '' : 'd-none'}`}>
- <button
- type="button"
- className="btn btn-sm text-muted border border-secondary me-2 d-flex align-items-center justify-content-center"
- onClick={onClickEditButton}
- >
- <span className="material-symbols-outlined fs-5 mt-1">{isRenameInputShown ? 'check_circle' : 'edit'}</span>
- </button>
- <button
- type="button"
- className="btn btn-sm text-muted border border-secondary d-flex align-items-center justify-content-center"
- onClick={openPageSelectModal}
- >
- <span className="material-symbols-outlined fs-5 mt-1">account_tree</span>
- </button>
- </div>
- {isOpened && <PageSelectModal />}
- </div>
- );
- };
|