PagePathHeader.tsx 3.7 KB

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