PagePathHeader.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import type { FC } from 'react';
  2. import { useMemo, useState, useEffect } 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 { editedPagePathHandler } from './PageHeader';
  9. import { TextInputForPageTitleAndPath } from './TextInputForPageTitleAndPath';
  10. import { usePagePathRenameHandler } from './page-header-utils';
  11. export type Props = {
  12. currentPage: IPagePopulatedToShowRevision
  13. editedPagePathHandler: editedPagePathHandler
  14. }
  15. export const PagePathHeader: FC<Props> = (props) => {
  16. const { currentPage, editedPagePathHandler } = 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 } = editedPagePathHandler;
  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. <>
  36. {currentPagePath != null && (
  37. <PagePathNav
  38. pageId={currentPage._id}
  39. pagePath={currentPagePath}
  40. isSingleLineMode={isEditorMode}
  41. />
  42. )}
  43. </>
  44. ), [currentPage._id, currentPagePath, isEditorMode]);
  45. const handleInputChange = (inputText: string) => {
  46. setEditedPagePath(inputText);
  47. };
  48. const handleEditButtonClick = () => {
  49. if (isRenameInputShown) {
  50. pagePathRenameHandler(editedPagePath);
  51. }
  52. else {
  53. setRenameInputShown(true);
  54. }
  55. };
  56. const buttonStyle = isButtonsShown ? '' : 'd-none';
  57. const clickOutSideHandler = (e) => {
  58. const container = document.getElementById('page-path-header');
  59. if (container && !container.contains(e.target)) {
  60. setRenameInputShown(false);
  61. }
  62. };
  63. useEffect(() => {
  64. document.addEventListener('click', clickOutSideHandler);
  65. return () => {
  66. document.removeEventListener('click', clickOutSideHandler);
  67. };
  68. }, []);
  69. return (
  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. editedPagePathHandler={editedPagePathHandler}
  83. inputValue={editedPagePath}
  84. CustomComponent={PagePath}
  85. handleInputChange={handleInputChange}
  86. />
  87. </div>
  88. <div className={`${buttonStyle} col-4 row`}>
  89. <div className="col-4">
  90. <button type="button" onClick={handleEditButtonClick}>
  91. {isRenameInputShown ? <span className="material-symbols-outlined">check_circle</span> : <span className="material-symbols-outlined">edit</span>}
  92. </button>
  93. </div>
  94. <div className="col-4">
  95. <button type="button" onClick={openPageSelectModal}>
  96. <span className="material-symbols-outlined">account_tree</span>
  97. </button>
  98. </div>
  99. </div>
  100. {isOpened
  101. && (
  102. <PageSelectModal />
  103. )}
  104. </div>
  105. </div>
  106. );
  107. };