BookmarkFolderTree.tsx 6.1 KB

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