aboutsummaryrefslogtreecommitdiffstats
path: root/packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx
blob: 4496d820df3979ebc3df3ea9c7718f018481f430 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"use client";

import { useToast } from "@/components/ui/use-toast";
import { api } from "@/lib/trpc";
import { ZBookmark, ZUpdateBookmarksRequest } from "@/lib/types/api/bookmarks";
import { useRouter } from "next/navigation";
import { Button } from "@/components/ui/button";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Archive, MoreHorizontal, Star, Trash2 } from "lucide-react";

export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
  const { toast } = useToast();
  const router = useRouter();
  const linkId = bookmark.id;

  const unbookmarkLink = async () => {
    try {
      await api.bookmarks.deleteBookmark.mutate({
        bookmarkId: linkId,
      });

      toast({
        description: "The bookmark has been deleted!",
      });
    } catch (e) {
      toast({
        variant: "destructive",
        title: "Something went wrong",
        description: "There was a problem with your request.",
      });
    }

    router.refresh();
  };

  const updateBookmark = async (req: ZUpdateBookmarksRequest) => {
    try {
      await api.bookmarks.updateBookmark.mutate(req);
      toast({
        description: "The bookmark has been updated!",
      });
    } catch (e) {
      toast({
        variant: "destructive",
        title: "Something went wrong",
        description: "There was a problem with your request.",
      });
    }

    router.refresh();
  };

  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <Button variant="ghost">
          <MoreHorizontal />
        </Button>
      </DropdownMenuTrigger>
      <DropdownMenuContent className="w-fit">
        <DropdownMenuItem
          onClick={() =>
            updateBookmark({
              bookmarkId: linkId,
              favourited: !bookmark.favourited,
            })
          }
        >
          <Star className="mr-2 size-4" />
          <span>{bookmark.favourited ? "Un-favourite" : "Favourite"}</span>
        </DropdownMenuItem>
        <DropdownMenuItem
          onClick={() =>
            updateBookmark({ bookmarkId: linkId, archived: !bookmark.archived })
          }
        >
          <Archive className="mr-2 size-4" />
          <span>{bookmark.archived ? "Un-archive" : "Archive"}</span>
        </DropdownMenuItem>
        <DropdownMenuItem className="text-destructive" onClick={unbookmarkLink}>
          <Trash2 className="mr-2 size-4" />
          <span>Delete</span>
        </DropdownMenuItem>
      </DropdownMenuContent>
    </DropdownMenu>
  );
}