PagePathHeader.tsx 3.7 KB

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