Skip to content

Commit

Permalink
feature: Extract hook logic into separate package and add a new actio…
Browse files Browse the repository at this point in the history
…n bar in bookmark preview
  • Loading branch information
MohamedBassem committed Apr 7, 2024
1 parent 4491831 commit 79d61be
Show file tree
Hide file tree
Showing 21 changed files with 483 additions and 141 deletions.
1 change: 1 addition & 0 deletions apps/browser-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"@hoarder/trpc": "workspace:^0.1.0",
"@hoarder/shared-react": "workspace:^0.1.0",
"@tanstack/react-query": "^5.24.8",
"@trpc/client": "11.0.0-next-beta.308",
"@trpc/next": "11.0.0-next-beta.308",
Expand Down
20 changes: 10 additions & 10 deletions apps/browser-extension/src/BookmarkSavedPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ import { useState } from "react";
import { ArrowUpRightFromSquare, Trash } from "lucide-react";
import { Link, useNavigate, useParams } from "react-router-dom";

import { useDeleteBookmark } from "@hoarder/shared-react/hooks/bookmarks";

import Spinner from "./Spinner";
import usePluginSettings from "./utils/settings";
import { api } from "./utils/trpc";

export default function BookmarkSavedPage() {
const { bookmarkId } = useParams();
const navigate = useNavigate();
const [error, setError] = useState("");

const { mutate: deleteBookmark, isPending } =
api.bookmarks.deleteBookmark.useMutation({
onSuccess: () => {
navigate("/bookmarkdeleted");
},
onError: (e) => {
setError(e.message);
},
});
const { mutate: deleteBookmark, isPending } = useDeleteBookmark({
onSuccess: () => {
navigate("/bookmarkdeleted");
},
onError: (e) => {
setError(e.message);
},
});

const { settings } = usePluginSettings();

Expand Down
25 changes: 9 additions & 16 deletions apps/mobile/components/bookmarks/BookmarkCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import { MenuView } from "@react-native-menu/menu";
import { Ellipsis, Star } from "lucide-react-native";

import type { ZBookmark } from "@hoarder/trpc/types/bookmarks";
import {
useDeleteBookmark,
useUpdateBookmark,
} from "@hoarder/shared-react/hooks/bookmarks";

import { Divider } from "../ui/Divider";
import { Skeleton } from "../ui/Skeleton";
Expand Down Expand Up @@ -45,7 +49,6 @@ export function isBookmarkStillLoading(bookmark: ZBookmark) {

function ActionBar({ bookmark }: { bookmark: ZBookmark }) {
const { toast } = useToast();
const apiUtils = api.useUtils();

const onError = () => {
toast({
Expand All @@ -56,37 +59,27 @@ function ActionBar({ bookmark }: { bookmark: ZBookmark }) {
};

const { mutate: deleteBookmark, isPending: isDeletionPending } =
api.bookmarks.deleteBookmark.useMutation({
useDeleteBookmark({
onSuccess: () => {
toast({
message: "The bookmark has been deleted!",
showProgress: false,
});
apiUtils.bookmarks.getBookmarks.invalidate();
apiUtils.bookmarks.searchBookmarks.invalidate();
},
onError,
});

const { mutate: favouriteBookmark, variables } =
api.bookmarks.updateBookmark.useMutation({
onSuccess: () => {
apiUtils.bookmarks.getBookmarks.invalidate();
apiUtils.bookmarks.getBookmark.invalidate({ bookmarkId: bookmark.id });
},
onError,
});
const { mutate: favouriteBookmark, variables } = useUpdateBookmark({
onError,
});

const { mutate: archiveBookmark, isPending: isArchivePending } =
api.bookmarks.updateBookmark.useMutation({
useUpdateBookmark({
onSuccess: (resp) => {
toast({
message: `The bookmark has been ${resp.archived ? "archived" : "un-archived"}!`,
showProgress: false,
});
apiUtils.bookmarks.getBookmarks.invalidate();
apiUtils.bookmarks.getBookmark.invalidate({ bookmarkId: bookmark.id });
apiUtils.bookmarks.searchBookmarks.invalidate();
},
onError,
});
Expand Down
1 change: 1 addition & 0 deletions apps/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"@hoarder/trpc": "workspace:^0.1.0",
"@hoarder/shared-react": "workspace:^0.1.0",
"@react-native-menu/menu": "^0.9.1",
"@tanstack/react-query": "^5.24.8",
"class-variance-authority": "^0.7.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/dashboard/preview/[bookmarkId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BookmarkPreview from "@/components/dashboard/bookmarks/BookmarkPreview";
import BookmarkPreview from "@/components/dashboard/preview/BookmarkPreview";
import { api } from "@/server/api/client";

export default async function BookmarkPreviewPage({
Expand Down
11 changes: 4 additions & 7 deletions apps/web/components/dashboard/bookmarks/BookmarkActionBar.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import BookmarkPreview from "@/components/dashboard/preview/BookmarkPreview";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogTrigger } from "@/components/ui/dialog";
import { Maximize2, Star } from "lucide-react";
import { Maximize2 } from "lucide-react";

import type { ZBookmark } from "@hoarder/trpc/types/bookmarks";

import BookmarkOptions from "./BookmarkOptions";
import BookmarkPreview from "./BookmarkPreview";
import { FavouritedActionIcon } from "./icons";

export default function BookmarkActionBar({
bookmark,
Expand All @@ -15,11 +16,7 @@ export default function BookmarkActionBar({
return (
<div className="flex text-gray-500">
{bookmark.favourited && (
<Star
className="m-1 size-8 rounded p-1"
color="#ebb434"
fill="#ebb434"
/>
<FavouritedActionIcon className="m-1 size-8 rounded p-1" favourited />
)}
<Dialog>
<DialogTrigger asChild>
Expand Down
52 changes: 20 additions & 32 deletions apps/web/components/dashboard/bookmarks/BookmarkOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,28 @@ import {
import { useToast } from "@/components/ui/use-toast";
import { useClientConfig } from "@/lib/clientConfig";
import { BookmarkListContext } from "@/lib/hooks/list-context";
import { api } from "@/lib/trpc";
import {
Archive,
Link,
List,
ListX,
MoreHorizontal,
Pencil,
RotateCw,
Star,
Tags,
Trash2,
} from "lucide-react";

import type { ZBookmark, ZBookmarkedLink } from "@hoarder/trpc/types/bookmarks";
import {
useDeleteBookmark,
useRecrawlBookmark,
useUpdateBookmark,
} from "@hoarder/shared-react/hooks//bookmarks";
import { useRemoveBookmarkFromList } from "@hoarder/shared-react/hooks//lists";

import { useAddToListModal } from "./AddToListModal";
import { BookmarkedTextEditor } from "./BookmarkedTextEditor";
import { ArchivedActionIcon, FavouritedActionIcon } from "./icons";
import { useTagModel } from "./TagModal";

export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
Expand All @@ -46,64 +50,42 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {

const { listId } = useContext(BookmarkListContext);

const invalidateAllBookmarksCache =
api.useUtils().bookmarks.getBookmarks.invalidate;

const invalidateBookmarkCache =
api.useUtils().bookmarks.getBookmark.invalidate;

const invalidateSearchCache =
api.useUtils().bookmarks.searchBookmarks.invalidate;

const onError = () => {
toast({
variant: "destructive",
title: "Something went wrong",
description: "There was a problem with your request.",
});
};
const deleteBookmarkMutator = api.bookmarks.deleteBookmark.useMutation({
const deleteBookmarkMutator = useDeleteBookmark({
onSuccess: () => {
toast({
description: "The bookmark has been deleted!",
});
},
onError,
onSettled: () => {
invalidateAllBookmarksCache();
invalidateSearchCache();
},
});

const updateBookmarkMutator = api.bookmarks.updateBookmark.useMutation({
const updateBookmarkMutator = useUpdateBookmark({
onSuccess: () => {
toast({
description: "The bookmark has been updated!",
});
},
onError,
onSettled: () => {
invalidateBookmarkCache({ bookmarkId: bookmark.id });
invalidateAllBookmarksCache();
invalidateSearchCache();
},
});

const crawlBookmarkMutator = api.bookmarks.recrawlBookmark.useMutation({
const crawlBookmarkMutator = useRecrawlBookmark({
onSuccess: () => {
toast({
description: "Re-fetch has been enqueued!",
});
},
onError,
onSettled: () => {
invalidateBookmarkCache({ bookmarkId: bookmark.id });
},
});

const removeFromListMutator = api.lists.removeFromList.useMutation({
onSuccess: (_resp, req) => {
invalidateAllBookmarksCache({ listId: req.listId });
const removeFromListMutator = useRemoveBookmarkFromList({
onSuccess: () => {
toast({
description: "The bookmark has been deleted from the list",
});
Expand Down Expand Up @@ -145,7 +127,10 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
})
}
>
<Star className="mr-2 size-4" />
<FavouritedActionIcon
className="mr-2 size-4"
favourited={bookmark.favourited}
/>
<span>{bookmark.favourited ? "Un-favourite" : "Favourite"}</span>
</DropdownMenuItem>
<DropdownMenuItem
Expand All @@ -157,7 +142,10 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
})
}
>
<Archive className="mr-2 size-4" />
<ArchivedActionIcon
className="mr-2 size-4"
archived={bookmark.archived}
/>
<span>{bookmark.archived ? "Un-archive" : "Archive"}</span>
</DropdownMenuItem>
{bookmark.content.type === "link" && (
Expand Down
29 changes: 29 additions & 0 deletions apps/web/components/dashboard/bookmarks/icons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Archive, ArchiveRestore, Star } from "lucide-react";

export function FavouritedActionIcon({
favourited,
className,
}: {
favourited: boolean;
className?: string;
}) {
return favourited ? (
<Star className={className} color="#ebb434" fill="#ebb434" />
) : (
<Star className={className} />
);
}

export function ArchivedActionIcon({
archived,
className,
}: {
archived: boolean;
className?: string;
}) {
return archived ? (
<ArchiveRestore className={className} />
) : (
<Archive className={className} />
);
}
Loading

0 comments on commit 79d61be

Please sign in to comment.