2
0

BookmarkFolderTree.tsx 5.5 KB

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