PagePathHeader.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import type { FC } from 'react';
  2. import { useMemo, useState } 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 { TextInputForPageTitleAndPath, type editingPagePathHandler } from './TextInputForPageTitleAndPath';
  9. import { usePagePathRenameHandler } from './page-header-utils';
  10. export type Props = {
  11. currentPagePath: string
  12. currentPage: IPagePopulatedToShowRevision
  13. editingPagePathHandler: editingPagePathHandler
  14. }
  15. export const PagePathHeader: FC<Props> = (props) => {
  16. const { currentPagePath, currentPage, editingPagePathHandler } = props;
  17. const [isRenameInputShown, setRenameInputShown] = useState(false);
  18. const [isButtonsShown, setButtonShown] = useState(false);
  19. const { data: editorMode } = useEditorMode();
  20. const { data: PageSelectModalData, open: openPageSelectModal } = usePageSelectModal();
  21. const { editingPagePath, setEditingPagePath } = editingPagePathHandler;
  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. setEditingPagePath(inputText);
  46. };
  47. const handleEditButtonClick = () => {
  48. if (isRenameInputShown) {
  49. pagePathRenameHandler(editingPagePath);
  50. }
  51. else {
  52. setRenameInputShown(true);
  53. }
  54. };
  55. const buttonStyle = isButtonsShown ? '' : 'd-none';
  56. return (
  57. <div
  58. onMouseLeave={() => setButtonShown(false)}
  59. >
  60. <div className="row">
  61. <div
  62. className="col-4"
  63. onMouseEnter={() => setButtonShown(true)}
  64. >
  65. <TextInputForPageTitleAndPath
  66. currentPage={currentPage}
  67. stateHandler={stateHandler}
  68. editingPagePathHandler={editingPagePathHandler}
  69. inputValue={editingPagePath}
  70. CustomComponent={PagePath}
  71. handleInputChange={handleInputChange}
  72. />
  73. </div>
  74. <div className={`${buttonStyle} col-4 row`}>
  75. <div className="col-4">
  76. <button type="button" onClick={handleEditButtonClick}>
  77. {isRenameInputShown ? <span className="material-symbols-outlined">check_circle</span> : <span className="material-symbols-outlined">edit</span>}
  78. </button>
  79. </div>
  80. <div className="col-4">
  81. <button type="button" onClick={openPageSelectModal}>
  82. <span className="material-symbols-outlined">account_tree</span>
  83. </button>
  84. </div>
  85. </div>
  86. {isOpened
  87. && (
  88. <PageSelectModal />
  89. )}
  90. </div>
  91. </div>
  92. );
  93. };