BookmarkFolderTree.tsx 4.9 KB

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