From fdd60a5764910619f9de6e8671767bafebaf3d66 Mon Sep 17 00:00:00 2001
From: MohamedBassem <me@mbassem.com>
Date: Mon, 4 Mar 2024 22:36:38 +0000
Subject: Fix: Correctly invalidate the bookmark cache when it's favourited

---
 .../bookmarks/components/BookmarkOptions.tsx        | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

(limited to 'packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx')

diff --git a/packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx b/packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx
index 62a0a625..584e8708 100644
--- a/packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx
+++ b/packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx
@@ -37,9 +37,12 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
 
   const [isTextEditorOpen, setTextEditorOpen] = useState(false);
 
-  const invalidateBookmarksCache =
+  const invalidateAllBookmarksCache =
     api.useUtils().bookmarks.getBookmarks.invalidate;
 
+  const invalidateBookmarkCache =
+    api.useUtils().bookmarks.getBookmark.invalidate;
+
   const onError = () => {
     toast({
       variant: "destructive",
@@ -47,9 +50,6 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
       description: "There was a problem with your request.",
     });
   };
-  const onSettled = () => {
-    invalidateBookmarksCache();
-  };
   const deleteBookmarkMutator = api.bookmarks.deleteBookmark.useMutation({
     onSuccess: () => {
       toast({
@@ -57,7 +57,9 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
       });
     },
     onError,
-    onSettled,
+    onSettled: () => {
+      invalidateAllBookmarksCache();
+    },
   });
 
   const updateBookmarkMutator = api.bookmarks.updateBookmark.useMutation({
@@ -67,7 +69,10 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
       });
     },
     onError,
-    onSettled,
+    onSettled: () => {
+      invalidateBookmarkCache({ bookmarkId: bookmark.id });
+      invalidateAllBookmarksCache();
+    },
   });
 
   const crawlBookmarkMutator = api.bookmarks.recrawlBookmark.useMutation({
@@ -77,7 +82,9 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
       });
     },
     onError,
-    onSettled,
+    onSettled: () => {
+      invalidateBookmarkCache({ bookmarkId: bookmark.id });
+    },
   });
 
   return (
-- 
cgit v1.2.3-70-g09d2

