BookmarkFolderTree.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { useCallback } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import { toastSuccess } from '~/client/util/apiNotification';
  4. import { IPageToDeleteWithMeta } from '~/interfaces/page';
  5. import { OnDeletedFunction } from '~/interfaces/ui';
  6. import { useSWRxCurrentUserBookmarks } from '~/stores/bookmark';
  7. import { useSWRxBookamrkFolderAndChild } from '~/stores/bookmark-folder';
  8. import { usePageDeleteModal } from '~/stores/modal';
  9. import BookmarkFolderItem from './BookmarkFolderItem';
  10. import BookmarkItem from './BookmarkItem';
  11. import styles from './BookmarkFolderTree.module.scss';
  12. const BookmarkFolderTree = (): JSX.Element => {
  13. const { t } = useTranslation();
  14. const { data: currentUserBookmarksData, mutate: mutateCurrentUserBookmarks } = useSWRxCurrentUserBookmarks();
  15. const { data: bookmarkFolderData } = useSWRxBookamrkFolderAndChild();
  16. const { open: openDeleteModal } = usePageDeleteModal();
  17. const deleteMenuItemClickHandler = useCallback((pageToDelete: IPageToDeleteWithMeta) => {
  18. const pageDeletedHandler : OnDeletedFunction = (pathOrPathsToDelete, _isRecursively, isCompletely) => {
  19. if (typeof pathOrPathsToDelete !== 'string') {
  20. return;
  21. }
  22. const path = pathOrPathsToDelete;
  23. if (isCompletely) {
  24. toastSuccess(t('deleted_pages_completely', { path }));
  25. }
  26. else {
  27. toastSuccess(t('deleted_pages', { path }));
  28. }
  29. mutateCurrentUserBookmarks();
  30. };
  31. openDeleteModal([pageToDelete], { onDeleted: pageDeletedHandler });
  32. }, [mutateCurrentUserBookmarks, openDeleteModal, t]);
  33. return (
  34. <>
  35. <ul className={`grw-foldertree ${styles['grw-foldertree']} list-group p-3`}>
  36. {bookmarkFolderData?.map((item) => {
  37. return (
  38. <BookmarkFolderItem
  39. key={item._id}
  40. bookmarkFolder={item}
  41. isOpen={false}
  42. isSidebarItem={true}
  43. />
  44. );
  45. })}
  46. {currentUserBookmarksData?.length === 0 && (
  47. <div className="pt-3">
  48. <h5 className="pl-3">
  49. { t('No bookmarks yet') }
  50. </h5>
  51. </div>
  52. )}
  53. { currentUserBookmarksData?.map((currentUserBookmark) => {
  54. return (
  55. <BookmarkItem
  56. key={currentUserBookmark._id}
  57. bookmarkedPage={currentUserBookmark}
  58. onUnbookmarked={mutateCurrentUserBookmarks}
  59. onRenamed={mutateCurrentUserBookmarks}
  60. onClickDeleteMenuItem={deleteMenuItemClickHandler}
  61. isSidebarItem={true}
  62. />
  63. );
  64. })}
  65. </ul>
  66. </>
  67. );
  68. };
  69. export default BookmarkFolderTree;