Bookmarks.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import React from 'react';
  2. import { useTranslation } from 'react-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 { useIsGuestUser } from '~/stores/context';
  8. import { usePageDeleteModal } from '~/stores/modal';
  9. import BookmarkFolder from './Bookmarks/BookmarkFolder';
  10. import BookmarkItem from './Bookmarks/BookmarkItem';
  11. const Bookmarks = () : JSX.Element => {
  12. const { t } = useTranslation();
  13. const { data: isGuestUser } = useIsGuestUser();
  14. const { data: currentUserBookmarksData, mutate: mutateCurrentUserBookmarks } = useSWRxCurrentUserBookmarks();
  15. const { open: openDeleteModal } = usePageDeleteModal();
  16. const deleteMenuItemClickHandler = (pageToDelete: IPageToDeleteWithMeta) => {
  17. const pageDeletedHandler : OnDeletedFunction = (pathOrPathsToDelete, _isRecursively, isCompletely) => {
  18. if (typeof pathOrPathsToDelete !== 'string') {
  19. return;
  20. }
  21. const path = pathOrPathsToDelete;
  22. if (isCompletely) {
  23. toastSuccess(t('deleted_pages_completely', { path }));
  24. }
  25. else {
  26. toastSuccess(t('deleted_pages', { path }));
  27. }
  28. mutateCurrentUserBookmarks();
  29. };
  30. openDeleteModal([pageToDelete], { onDeleted: pageDeletedHandler });
  31. };
  32. const renderBookmarkList = () => {
  33. if (currentUserBookmarksData?.length === 0) {
  34. return (
  35. <h4 className="pl-3">
  36. { t('No bookmarks yet') }
  37. </h4>
  38. );
  39. }
  40. return (
  41. <ul className="grw-bookmarks-list list-group p-3">
  42. <div className="grw-bookmarks-item-container">
  43. { currentUserBookmarksData?.map((currentUserBookmark) => {
  44. return (
  45. <BookmarkItem
  46. key={currentUserBookmark._id}
  47. bookmarkedPage={currentUserBookmark}
  48. onUnbookmarked={mutateCurrentUserBookmarks}
  49. onRenamed={mutateCurrentUserBookmarks}
  50. onClickDeleteMenuItem={deleteMenuItemClickHandler}
  51. />
  52. );
  53. })}
  54. </div>
  55. </ul>
  56. );
  57. };
  58. return (
  59. <>
  60. <div className="grw-sidebar-content-header p-3">
  61. <h3 className="mb-0">{t('Bookmarks')}</h3>
  62. </div>
  63. {!isGuestUser && (
  64. <>
  65. <BookmarkFolder />
  66. </>
  67. )
  68. }
  69. { isGuestUser
  70. ? (
  71. <h4 className="pl-3">
  72. { t('Not available for guest') }
  73. </h4>
  74. ) : renderBookmarkList()
  75. }
  76. </>
  77. );
  78. };
  79. export default Bookmarks;