Ellipsis.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import React, {
  2. useCallback, useState, FC,
  3. } from 'react';
  4. import nodePath from 'path';
  5. import { pathUtils } from '@growi/core';
  6. import { useTranslation } from 'next-i18next';
  7. import { DropdownToggle } from 'reactstrap';
  8. import { bookmark, unbookmark, resumeRenameOperation } from '~/client/services/page-operation';
  9. import { apiv3Put } from '~/client/util/apiv3-client';
  10. import { ValidationTarget } from '~/client/util/input-validator';
  11. import { toastError, toastSuccess } from '~/client/util/toastr';
  12. import { NotAvailableForGuest } from '~/components/NotAvailableForGuest';
  13. import {
  14. IPageInfoAll, IPageToDeleteWithMeta, IPageForItem,
  15. } from '~/interfaces/page';
  16. import { useSWRMUTxCurrentUserBookmarks } from '~/stores/bookmark';
  17. import { useSWRMUTxPageInfo } from '~/stores/page';
  18. import ClosableTextInput from '../../Common/ClosableTextInput';
  19. import { PageItemControl } from '../../Common/Dropdown/PageItemControl';
  20. import {
  21. SimpleItemToolProps, NotDraggableForClosableTextInput, SimpleItemTool,
  22. } from '../../TreeItem/SimpleItem';
  23. type EllipsisProps = SimpleItemToolProps & {page: IPageForItem};
  24. export const Ellipsis: FC<EllipsisProps> = (props) => {
  25. const [isRenameInputShown, setRenameInputShown] = useState(false);
  26. const { t } = useTranslation();
  27. const {
  28. page, onRenamed, onClickDuplicateMenuItem,
  29. onClickDeleteMenuItem, isEnableActions, isReadOnlyUser,
  30. } = props;
  31. const { trigger: mutateCurrentUserBookmarks } = useSWRMUTxCurrentUserBookmarks();
  32. const { trigger: mutatePageInfo } = useSWRMUTxPageInfo(page._id ?? null);
  33. const bookmarkMenuItemClickHandler = async(_pageId: string, _newValue: boolean): Promise<void> => {
  34. const bookmarkOperation = _newValue ? bookmark : unbookmark;
  35. await bookmarkOperation(_pageId);
  36. mutateCurrentUserBookmarks();
  37. mutatePageInfo();
  38. };
  39. const duplicateMenuItemClickHandler = useCallback((): void => {
  40. if (onClickDuplicateMenuItem == null) {
  41. return;
  42. }
  43. const { _id: pageId, path } = page;
  44. if (pageId == null || path == null) {
  45. throw Error('Any of _id and path must not be null.');
  46. }
  47. const pageToDuplicate = { pageId, path };
  48. onClickDuplicateMenuItem(pageToDuplicate);
  49. }, [onClickDuplicateMenuItem, page]);
  50. const renameMenuItemClickHandler = useCallback(() => {
  51. setRenameInputShown(true);
  52. }, []);
  53. const onPressEnterForRenameHandler = async(inputText: string) => {
  54. const parentPath = pathUtils.addTrailingSlash(nodePath.dirname(page.path ?? ''));
  55. const newPagePath = nodePath.resolve(parentPath, inputText);
  56. if (newPagePath === page.path) {
  57. setRenameInputShown(false);
  58. return;
  59. }
  60. try {
  61. setRenameInputShown(false);
  62. await apiv3Put('/pages/rename', {
  63. pageId: page._id,
  64. revisionId: page.revision,
  65. newPagePath,
  66. });
  67. if (onRenamed != null) {
  68. onRenamed(page.path, newPagePath);
  69. }
  70. toastSuccess(t('renamed_pages', { path: page.path }));
  71. }
  72. catch (err) {
  73. setRenameInputShown(true);
  74. toastError(err);
  75. }
  76. };
  77. const deleteMenuItemClickHandler = useCallback(async(_pageId: string, pageInfo: IPageInfoAll | undefined): Promise<void> => {
  78. if (onClickDeleteMenuItem == null) {
  79. return;
  80. }
  81. if (page._id == null || page.path == null) {
  82. throw Error('_id and path must not be null.');
  83. }
  84. const pageToDelete: IPageToDeleteWithMeta = {
  85. data: {
  86. _id: page._id,
  87. revision: page.revision as string,
  88. path: page.path,
  89. },
  90. meta: pageInfo,
  91. };
  92. onClickDeleteMenuItem(pageToDelete);
  93. }, [onClickDeleteMenuItem, page]);
  94. const pathRecoveryMenuItemClickHandler = async(pageId: string): Promise<void> => {
  95. try {
  96. await resumeRenameOperation(pageId);
  97. toastSuccess(t('page_operation.paths_recovered'));
  98. }
  99. catch {
  100. toastError(t('page_operation.path_recovery_failed'));
  101. }
  102. };
  103. return (
  104. <>
  105. {isRenameInputShown ? (
  106. <div className="flex-fill">
  107. <NotDraggableForClosableTextInput>
  108. <ClosableTextInput
  109. value={nodePath.basename(page.path ?? '')}
  110. placeholder={t('Input page name')}
  111. onClickOutside={() => { setRenameInputShown(false) }}
  112. onPressEnter={onPressEnterForRenameHandler}
  113. validationTarget={ValidationTarget.PAGE}
  114. />
  115. </NotDraggableForClosableTextInput>
  116. </div>
  117. ) : (
  118. <SimpleItemTool page={page} isEnableActions={false} isReadOnlyUser={false}/>
  119. )}
  120. <NotAvailableForGuest>
  121. <div className="grw-pagetree-control d-flex">
  122. <PageItemControl
  123. pageId={page._id}
  124. isEnableActions={isEnableActions}
  125. isReadOnlyUser={isReadOnlyUser}
  126. onClickBookmarkMenuItem={bookmarkMenuItemClickHandler}
  127. onClickDuplicateMenuItem={duplicateMenuItemClickHandler}
  128. onClickRenameMenuItem={renameMenuItemClickHandler}
  129. onClickDeleteMenuItem={deleteMenuItemClickHandler}
  130. onClickPathRecoveryMenuItem={pathRecoveryMenuItemClickHandler}
  131. isInstantRename
  132. // Todo: It is wanted to find a better way to pass operationProcessData to PageItemControl
  133. operationProcessData={page.processData}
  134. >
  135. {/* pass the color property to reactstrap dropdownToggle props. https://6-4-0--reactstrap.netlify.app/components/dropdowns/ */}
  136. <DropdownToggle color="transparent" className="border-0 rounded btn-page-item-control p-0 grw-visible-on-hover mr-1">
  137. <i id='option-button-in-page-tree' className="icon-options fa fa-rotate-90 p-1"></i>
  138. </DropdownToggle>
  139. </PageItemControl>
  140. </div>
  141. </NotAvailableForGuest>
  142. </>
  143. );
  144. };