BookmarkFolderTree.tsx 5.2 KB

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