BookmarkFolderTree.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React, { useCallback } from 'react';
  2. import type { IPageToDeleteWithMeta } from '@growi/core';
  3. import { useTranslation } from 'next-i18next';
  4. import { useRouter } from 'next/router';
  5. import { DndProvider } from 'react-dnd';
  6. import { HTML5Backend } from 'react-dnd-html5-backend';
  7. import { toastSuccess } from '~/client/util/toastr';
  8. import type { OnDeletedFunction } from '~/interfaces/ui';
  9. import {
  10. useSWRxUserBookmarks, useSWRMUTxCurrentUserBookmarks,
  11. } from '~/stores/bookmark';
  12. import { useSWRxBookmarkFolderAndChild } from '~/stores/bookmark-folder';
  13. import { useIsReadOnlyUser } from '~/stores/context';
  14. import { usePageDeleteModal } from '~/stores/modal';
  15. import { mutateAllPageInfo, useSWRMUTxPageInfo, useSWRxCurrentPage } from '~/stores/page';
  16. import { BookmarkFolderItem } from './BookmarkFolderItem';
  17. import { BookmarkItem } from './BookmarkItem';
  18. import styles from './BookmarkFolderTree.module.scss';
  19. // type DragItemDataType = {
  20. // bookmarkFolder: BookmarkFolderItems
  21. // level: number
  22. // parentFolder: BookmarkFolderItems | null
  23. // } & IPageHasId
  24. type Props = {
  25. isUserHomepage?: boolean,
  26. userId?: string,
  27. isOperable: boolean,
  28. }
  29. export const BookmarkFolderTree: React.FC<Props> = (props: Props) => {
  30. const { isUserHomepage, userId } = props;
  31. // const acceptedTypes: DragItemType[] = [DRAG_ITEM_TYPE.FOLDER, DRAG_ITEM_TYPE.BOOKMARK];
  32. const { t } = useTranslation();
  33. const router = useRouter();
  34. const { data: isReadOnlyUser } = useIsReadOnlyUser();
  35. const { data: currentPage } = useSWRxCurrentPage();
  36. const { data: bookmarkFolders, mutate: mutateBookmarkFolders } = useSWRxBookmarkFolderAndChild(userId);
  37. const { data: userBookmarks, mutate: mutateUserBookmarks } = useSWRxUserBookmarks(userId ?? null);
  38. const { trigger: mutatePageInfo } = useSWRMUTxPageInfo(currentPage?._id ?? null);
  39. const { trigger: mutateCurrentUserBookmarks } = useSWRMUTxCurrentUserBookmarks();
  40. const { open: openDeleteModal } = usePageDeleteModal();
  41. const bookmarkFolderTreeMutation = useCallback(() => {
  42. mutateUserBookmarks();
  43. mutateCurrentUserBookmarks();
  44. mutatePageInfo();
  45. mutateBookmarkFolders();
  46. }, [mutateBookmarkFolders, mutatePageInfo, mutateCurrentUserBookmarks, mutateUserBookmarks]);
  47. const onClickDeleteMenuItemHandler = useCallback((pageToDelete: IPageToDeleteWithMeta) => {
  48. const pageDeletedHandler: OnDeletedFunction = (pathOrPathsToDelete, _isRecursively, isCompletely) => {
  49. if (typeof pathOrPathsToDelete !== 'string') return;
  50. toastSuccess(isCompletely ? t('deleted_pages_completely', { path: pathOrPathsToDelete }) : t('deleted_pages', { path: pathOrPathsToDelete }));
  51. bookmarkFolderTreeMutation();
  52. mutateAllPageInfo();
  53. if (pageToDelete.data._id === currentPage?._id && _isRecursively) {
  54. router.push(`/trash${currentPage.path}`);
  55. }
  56. };
  57. openDeleteModal([pageToDelete], { onDeleted: pageDeletedHandler });
  58. }, [openDeleteModal, t, bookmarkFolderTreeMutation, currentPage?._id, currentPage?.path, router]);
  59. /* TODO: update in bookmarks folder v2. */
  60. // const itemDropHandler = async(item: DragItemDataType, dragType: string | null | symbol) => {
  61. // if (dragType === DRAG_ITEM_TYPE.FOLDER) {
  62. // try {
  63. // await updateBookmarkFolder(item.bookmarkFolder._id, item.bookmarkFolder.name, null);
  64. // await mutateBookmarkData();
  65. // toastSuccess(t('toaster.update_successed', { target: t('bookmark_folder.bookmark_folder'), ns: 'commons' }));
  66. // }
  67. // catch (err) {
  68. // toastError(err);
  69. // }
  70. // }
  71. // else {
  72. // try {
  73. // await addBookmarkToFolder(item._id, null);
  74. // await mutateUserBookmarks();
  75. // toastSuccess(t('toaster.add_succeeded', { target: t('bookmark_folder.bookmark'), ns: 'commons' }));
  76. // }
  77. // catch (err) {
  78. // toastError(err);
  79. // }
  80. // }
  81. // };
  82. // const isDroppable = (item: DragItemDataType, dragType: string | null | symbol) => {
  83. // if (dragType === DRAG_ITEM_TYPE.FOLDER) {
  84. // const isRootFolder = item.level === 0;
  85. // return !isRootFolder;
  86. // }
  87. // const isRootBookmark = item.parentFolder == null;
  88. // return !isRootBookmark;
  89. // };
  90. return (
  91. <DndProvider backend={HTML5Backend}>
  92. <div className={`grw-folder-tree-container ${styles['grw-folder-tree-container']}`}>
  93. <ul className={`grw-foldertree ${styles['grw-foldertree']} list-group py-2`}>
  94. {bookmarkFolders?.map((bookmarkFolder) => {
  95. return (
  96. <BookmarkFolderItem
  97. key={bookmarkFolder._id}
  98. isReadOnlyUser={!!isReadOnlyUser}
  99. isOperable={props.isOperable}
  100. bookmarkFolder={bookmarkFolder}
  101. isOpen={false}
  102. level={0}
  103. root={bookmarkFolder._id}
  104. isUserHomepage={isUserHomepage}
  105. onClickDeleteMenuItemHandler={onClickDeleteMenuItemHandler}
  106. bookmarkFolderTreeMutation={bookmarkFolderTreeMutation}
  107. />
  108. );
  109. })}
  110. {userBookmarks?.map(userBookmark => (
  111. <div key={userBookmark?._id} className="grw-foldertree-item-container grw-root-bookmarks">
  112. <BookmarkItem
  113. isReadOnlyUser={!!isReadOnlyUser}
  114. isOperable={props.isOperable}
  115. bookmarkedPage={userBookmark}
  116. level={0}
  117. parentFolder={null}
  118. canMoveToRoot={false}
  119. onClickDeleteMenuItemHandler={onClickDeleteMenuItemHandler}
  120. bookmarkFolderTreeMutation={bookmarkFolderTreeMutation}
  121. />
  122. </div>
  123. ))}
  124. </ul>
  125. {/* TODO: update in bookmarks folder v2. Also delete drop_item_here in translation.json, if don't need it. */}
  126. {/* {bookmarkFolderData != null && bookmarkFolderData.length > 0 && (
  127. <DragAndDropWrapper
  128. useDropMode={true}
  129. type={acceptedTypes}
  130. onDropItem={itemDropHandler}
  131. isDropable={isDroppable}
  132. >
  133. <div className="grw-drop-item-area">
  134. <div className="d-flex flex-column align-items-center">
  135. {t('bookmark_folder.drop_item_here')}
  136. </div>
  137. </div>
  138. </DragAndDropWrapper>
  139. )} */}
  140. </div>
  141. </DndProvider>
  142. );
  143. };