PagePathHeader.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import {
  2. useMemo, useState, useEffect, useCallback,
  3. } from 'react';
  4. import type { FC } from 'react';
  5. import type { IPagePopulatedToShowRevision } from '@growi/core';
  6. import { useTranslation } from 'next-i18next';
  7. import { ValidationTarget } from '~/client/util/input-validator';
  8. import { usePageSelectModal } from '~/stores/modal';
  9. import { EditorMode, useEditorMode } from '~/stores/ui';
  10. import ClosableTextInput from '../Common/ClosableTextInput';
  11. import { PagePathNav } from '../Common/PagePathNav';
  12. import { PageSelectModal } from '../PageSelectModal/PageSelectModal';
  13. import { usePagePathRenameHandler } from './page-header-utils';
  14. export type Props = {
  15. currentPage: IPagePopulatedToShowRevision
  16. }
  17. export const PagePathHeader: FC<Props> = (props) => {
  18. const { currentPage } = props;
  19. const currentPagePath = currentPage.path;
  20. const [isRenameInputShown, setRenameInputShown] = useState(false);
  21. const [isButtonsShown, setButtonShown] = useState(false);
  22. const [editedPagePath, setEditedPagePath] = useState(currentPagePath);
  23. const { data: editorMode } = useEditorMode();
  24. const { data: PageSelectModalData, open: openPageSelectModal } = usePageSelectModal();
  25. const pagePathRenameHandler = usePagePathRenameHandler(currentPage);
  26. const { t } = useTranslation();
  27. const onRenameFinish = useCallback(() => {
  28. setRenameInputShown(false);
  29. }, []);
  30. const onRenameFailure = useCallback(() => {
  31. setRenameInputShown(true);
  32. }, []);
  33. const onInputChange = useCallback((inputText: string) => {
  34. setEditedPagePath(inputText);
  35. }, []);
  36. const onPressEnter = useCallback(() => {
  37. pagePathRenameHandler(editedPagePath, onRenameFinish, onRenameFailure);
  38. }, [editedPagePath, onRenameFailure, onRenameFinish, pagePathRenameHandler]);
  39. const onPressEscape = useCallback(() => {
  40. setEditedPagePath(currentPagePath);
  41. setRenameInputShown(false);
  42. }, [currentPagePath]);
  43. const onClickEditButton = useCallback(() => {
  44. if (isRenameInputShown) {
  45. pagePathRenameHandler(editedPagePath, onRenameFinish, onRenameFailure);
  46. }
  47. else {
  48. setEditedPagePath(currentPagePath);
  49. setRenameInputShown(true);
  50. }
  51. }, [currentPagePath, editedPagePath, isRenameInputShown, pagePathRenameHandler]);
  52. const isOpened = PageSelectModalData?.isOpened ?? false;
  53. const isViewMode = editorMode === EditorMode.View;
  54. const isEditorMode = !isViewMode;
  55. const PagePath = useMemo(() => (
  56. <PagePathNav
  57. pageId={currentPage._id}
  58. pagePath={currentPagePath}
  59. isSingleLineMode={isEditorMode}
  60. />
  61. ), [currentPage._id, currentPagePath, isEditorMode]);
  62. const buttonStyle = isButtonsShown ? '' : 'd-none';
  63. const clickOutSideHandler = useCallback((e) => {
  64. const container = document.getElementById('page-path-header');
  65. if (container && !container.contains(e.target)) {
  66. setRenameInputShown(false);
  67. }
  68. }, []);
  69. useEffect(() => {
  70. document.addEventListener('click', clickOutSideHandler);
  71. return () => {
  72. document.removeEventListener('click', clickOutSideHandler);
  73. };
  74. }, []);
  75. return (
  76. <div
  77. id="page-path-header"
  78. onMouseLeave={() => setButtonShown(false)}
  79. >
  80. <div className="row">
  81. <div
  82. className="col-4"
  83. onMouseEnter={() => setButtonShown(true)}
  84. >
  85. {isRenameInputShown ? (
  86. <div className="flex-fill">
  87. <ClosableTextInput
  88. value={editedPagePath}
  89. placeholder={t('Input page name')}
  90. onPressEnter={onPressEnter}
  91. onPressEscape={onPressEscape}
  92. validationTarget={ValidationTarget.PAGE}
  93. handleInputChange={onInputChange}
  94. />
  95. </div>
  96. ) : (
  97. <>{ PagePath }</>
  98. )}
  99. </div>
  100. <div className={`${buttonStyle} col-4 row`}>
  101. <div className="col-4">
  102. <button type="button" onClick={onClickEditButton}>
  103. {isRenameInputShown ? <span className="material-symbols-outlined">check_circle</span> : <span className="material-symbols-outlined">edit</span>}
  104. </button>
  105. </div>
  106. <div className="col-4">
  107. <button type="button" onClick={openPageSelectModal}>
  108. <span className="material-symbols-outlined">account_tree</span>
  109. </button>
  110. </div>
  111. </div>
  112. {isOpened
  113. && (
  114. <PageSelectModal />
  115. )}
  116. </div>
  117. </div>
  118. );
  119. };