PagePathHeader.tsx 4.7 KB

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