PagePathHeader.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { useState, useEffect, useCallback } from 'react';
  2. import type { FC } from 'react';
  3. import type { IPagePopulatedToShowRevision } from '@growi/core';
  4. import { useTranslation } from 'next-i18next';
  5. import { ValidationTarget } from '~/client/util/input-validator';
  6. import LinkedPagePath from '~/models/linked-page-path';
  7. import { usePageSelectModal } from '~/stores/modal';
  8. import ClosableTextInput from '../Common/ClosableTextInput';
  9. import { PagePathHierarchicalLink } from '../Common/PagePathHierarchicalLink';
  10. import { PageSelectModal } from '../PageSelectModal/PageSelectModal';
  11. import { usePagePathRenameHandler } from './page-header-utils';
  12. import styles from './PagePathHeader.module.scss';
  13. const moduleClass = styles['page-path-header'];
  14. export type Props = {
  15. currentPage: IPagePopulatedToShowRevision
  16. }
  17. export const PagePathHeader: FC<Props> = (props) => {
  18. const { t } = useTranslation();
  19. const { currentPage } = props;
  20. const currentPagePath = currentPage.path;
  21. const linkedPagePath = new LinkedPagePath(currentPagePath);
  22. const [isRenameInputShown, setRenameInputShown] = useState(false);
  23. const [isButtonsShown, setButtonShown] = useState(false);
  24. const [editedPagePath, setEditedPagePath] = useState(currentPagePath);
  25. const { data: PageSelectModalData, open: openPageSelectModal } = usePageSelectModal();
  26. const isOpened = PageSelectModalData?.isOpened ?? false;
  27. const pagePathRenameHandler = usePagePathRenameHandler(currentPage);
  28. const onRenameFinish = useCallback(() => {
  29. setRenameInputShown(false);
  30. }, []);
  31. const onRenameFailure = useCallback(() => {
  32. setRenameInputShown(true);
  33. }, []);
  34. const onInputChange = useCallback((inputText: string) => {
  35. setEditedPagePath(inputText);
  36. }, []);
  37. const onPressEnter = useCallback(() => {
  38. pagePathRenameHandler(editedPagePath, onRenameFinish, onRenameFailure);
  39. }, [editedPagePath, onRenameFailure, onRenameFinish, pagePathRenameHandler]);
  40. const onPressEscape = useCallback(() => {
  41. setEditedPagePath(currentPagePath);
  42. setRenameInputShown(false);
  43. }, [currentPagePath]);
  44. const onClickEditButton = useCallback(() => {
  45. if (isRenameInputShown) {
  46. pagePathRenameHandler(editedPagePath, onRenameFinish, onRenameFailure);
  47. }
  48. else {
  49. setEditedPagePath(currentPagePath);
  50. setRenameInputShown(true);
  51. }
  52. }, [currentPagePath, editedPagePath, isRenameInputShown, onRenameFailure, onRenameFinish, pagePathRenameHandler]);
  53. const clickOutSideHandler = useCallback((e) => {
  54. const container = document.getElementById('page-path-header');
  55. if (container && !container.contains(e.target)) {
  56. setRenameInputShown(false);
  57. }
  58. }, []);
  59. useEffect(() => {
  60. document.addEventListener('click', clickOutSideHandler);
  61. return () => {
  62. document.removeEventListener('click', clickOutSideHandler);
  63. };
  64. }, [clickOutSideHandler]);
  65. return (
  66. <div
  67. id="page-path-header"
  68. className={`d-flex ${moduleClass}`}
  69. onMouseEnter={() => setButtonShown(true)}
  70. onMouseLeave={() => setButtonShown(false)}
  71. >
  72. <div className="me-2">
  73. {isRenameInputShown
  74. ? (
  75. <ClosableTextInput
  76. useAutosizeInput
  77. value={editedPagePath}
  78. placeholder={t('Input page name')}
  79. onPressEnter={onPressEnter}
  80. onPressEscape={onPressEscape}
  81. onChange={onInputChange}
  82. validationTarget={ValidationTarget.PAGE}
  83. />
  84. )
  85. : (
  86. <PagePathHierarchicalLink linkedPagePath={linkedPagePath} />
  87. )
  88. }
  89. </div>
  90. <div className={`page-path-header-buttons d-flex align-items-center ${isButtonsShown ? '' : 'd-none'}`}>
  91. <button
  92. type="button"
  93. className="btn btn-sm text-muted border border-secondary me-2 d-flex align-items-center justify-content-center"
  94. onClick={onClickEditButton}
  95. >
  96. <span className="material-symbols-outlined fs-5 mt-1">{isRenameInputShown ? 'check_circle' : 'edit'}</span>
  97. </button>
  98. <button
  99. type="button"
  100. className="btn btn-sm text-muted border border-secondary d-flex align-items-center justify-content-center"
  101. onClick={openPageSelectModal}
  102. >
  103. <span className="material-symbols-outlined fs-5 mt-1">account_tree</span>
  104. </button>
  105. </div>
  106. {isOpened && <PageSelectModal />}
  107. </div>
  108. );
  109. };