aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/components/dashboard/bookmarks/action-buttons/ArchiveBookmarkButton.tsx
diff options
context:
space:
mode:
authorMohamedBassem <me@mbassem.com>2024-05-19 17:56:57 +0000
committerMohamedBassem <me@mbassem.com>2024-05-19 17:56:57 +0000
commitf99f4c0ff118547388a7e1ea332aa8755a8c9baf (patch)
tree21c077840912cee39e481bfdb71573e9b7d201a2 /apps/web/components/dashboard/bookmarks/action-buttons/ArchiveBookmarkButton.tsx
parentbfcf0a4429e4d182307eb4c8599d4d11e9cc5bed (diff)
downloadkarakeep-f99f4c0ff118547388a7e1ea332aa8755a8c9baf.tar.zst
feature(web): Add an archive button to list management dialog
Diffstat (limited to 'apps/web/components/dashboard/bookmarks/action-buttons/ArchiveBookmarkButton.tsx')
-rw-r--r--apps/web/components/dashboard/bookmarks/action-buttons/ArchiveBookmarkButton.tsx64
1 files changed, 64 insertions, 0 deletions
diff --git a/apps/web/components/dashboard/bookmarks/action-buttons/ArchiveBookmarkButton.tsx b/apps/web/components/dashboard/bookmarks/action-buttons/ArchiveBookmarkButton.tsx
new file mode 100644
index 00000000..671c9bb2
--- /dev/null
+++ b/apps/web/components/dashboard/bookmarks/action-buttons/ArchiveBookmarkButton.tsx
@@ -0,0 +1,64 @@
+import React from "react";
+import { ActionButton, ActionButtonProps } from "@/components/ui/action-button";
+import { toast } from "@/components/ui/use-toast";
+import { api } from "@/lib/trpc";
+
+import { useUpdateBookmark } from "@hoarder/shared-react/hooks/bookmarks";
+
+interface ArchiveBookmarkButtonProps
+ extends Omit<ActionButtonProps, "loading" | "disabled"> {
+ bookmarkId: string;
+ onDone?: () => void;
+}
+
+const ArchiveBookmarkButton = React.forwardRef<
+ HTMLButtonElement,
+ ArchiveBookmarkButtonProps
+>(({ bookmarkId, onDone, ...props }, ref) => {
+ const { data } = api.bookmarks.getBookmark.useQuery({ bookmarkId });
+
+ const { mutate: updateBookmark, isPending: isArchivingBookmark } =
+ useUpdateBookmark({
+ onSuccess: () => {
+ toast({
+ description: "Bookmark has been archived!",
+ });
+ onDone?.();
+ },
+ onError: (e) => {
+ if (e.data?.code == "BAD_REQUEST") {
+ toast({
+ variant: "destructive",
+ description: e.message,
+ });
+ } else {
+ toast({
+ variant: "destructive",
+ title: "Something went wrong",
+ });
+ }
+ },
+ });
+
+ if (!data) {
+ return <span />;
+ }
+
+ return (
+ <ActionButton
+ ref={ref}
+ loading={isArchivingBookmark}
+ disabled={data.archived}
+ onClick={() =>
+ updateBookmark({
+ bookmarkId,
+ archived: !data.archived,
+ })
+ }
+ {...props}
+ />
+ );
+});
+
+ArchiveBookmarkButton.displayName = "ArchiveBookmarkButton";
+export default ArchiveBookmarkButton;