From e99dee0b4f7220568c8ffa2755147bc45d35b32b Mon Sep 17 00:00:00 2001
From: MohamedBassem <me@mbassem.com>
Date: Sat, 30 Mar 2024 04:45:42 +0000
Subject: feature(web): Add support for attaching notes to bookmarks

---
 .../components/dashboard/bookmarks/NoteEditor.tsx  | 48 ++++++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 apps/web/components/dashboard/bookmarks/NoteEditor.tsx

(limited to 'apps/web/components/dashboard/bookmarks/NoteEditor.tsx')

diff --git a/apps/web/components/dashboard/bookmarks/NoteEditor.tsx b/apps/web/components/dashboard/bookmarks/NoteEditor.tsx
new file mode 100644
index 00000000..d712d523
--- /dev/null
+++ b/apps/web/components/dashboard/bookmarks/NoteEditor.tsx
@@ -0,0 +1,48 @@
+import { Textarea } from "@/components/ui/textarea";
+import { toast } from "@/components/ui/use-toast";
+import { useClientConfig } from "@/lib/clientConfig";
+import { api } from "@/lib/trpc";
+
+import type { ZBookmark } from "@hoarder/trpc/types/bookmarks";
+
+export function NoteEditor({ bookmark }: { bookmark: ZBookmark }) {
+  const demoMode = !!useClientConfig().demoMode;
+
+  const invalidateBookmarkCache =
+    api.useUtils().bookmarks.getBookmark.invalidate;
+
+  const updateBookmarkMutator = api.bookmarks.updateBookmark.useMutation({
+    onSuccess: () => {
+      toast({
+        description: "The bookmark has been updated!",
+      });
+    },
+    onError: () => {
+      toast({
+        description: "Something went wrong while saving the note",
+        variant: "destructive",
+      });
+    },
+    onSettled: () => {
+      invalidateBookmarkCache({ bookmarkId: bookmark.id });
+    },
+  });
+
+  return (
+    <Textarea
+      className="h-44 w-full overflow-auto rounded bg-background p-2 text-sm text-gray-400 dark:text-gray-300"
+      defaultValue={bookmark.note ?? ""}
+      disabled={demoMode}
+      placeholder="Write some notes ..."
+      onBlur={(e) => {
+        if (e.currentTarget.value == bookmark.note) {
+          return;
+        }
+        updateBookmarkMutator.mutate({
+          bookmarkId: bookmark.id,
+          note: e.currentTarget.value,
+        });
+      }}
+    />
+  );
+}
-- 
cgit v1.2.3-70-g09d2

