BookmarkFolderTree.tsx 5.6 KB

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