From fe13408831dce4bdae4911098d6079a097cae9e8 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Tue, 9 Apr 2024 15:49:24 +0100 Subject: feature(web): Allow uploading directly into lists/tags. Fixes #69 --- packages/shared-react/hooks/bookmarks.ts | 91 ++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 3 deletions(-) (limited to 'packages/shared-react/hooks/bookmarks.ts') diff --git a/packages/shared-react/hooks/bookmarks.ts b/packages/shared-react/hooks/bookmarks.ts index 7349e680..5f246b38 100644 --- a/packages/shared-react/hooks/bookmarks.ts +++ b/packages/shared-react/hooks/bookmarks.ts @@ -1,4 +1,22 @@ import { api } from "../trpc"; +import { useBookmarkGridContext } from "./bookmark-grid-context"; +import { useAddBookmarkToList } from "./lists"; + +export function useCreateBookmarkWithPostHook( + ...opts: Parameters +) { + const apiUtils = api.useUtils(); + const postCreationCB = useBookmarkPostCreationHook(); + return api.bookmarks.createBookmark.useMutation({ + ...opts, + onSuccess: async (res, req, meta) => { + apiUtils.bookmarks.getBookmarks.invalidate(); + apiUtils.bookmarks.searchBookmarks.invalidate(); + await postCreationCB(res.id); + return opts[0]?.onSuccess?.(res, req, meta); + }, + }); +} export function useDeleteBookmark( ...opts: Parameters @@ -9,7 +27,7 @@ export function useDeleteBookmark( onSuccess: (res, req, meta) => { apiUtils.bookmarks.getBookmarks.invalidate(); apiUtils.bookmarks.searchBookmarks.invalidate(); - opts[0]?.onSuccess?.(res, req, meta); + return opts[0]?.onSuccess?.(res, req, meta); }, }); } @@ -24,7 +42,7 @@ export function useUpdateBookmark( apiUtils.bookmarks.getBookmarks.invalidate(); apiUtils.bookmarks.searchBookmarks.invalidate(); apiUtils.bookmarks.getBookmark.invalidate({ bookmarkId: req.bookmarkId }); - opts[0]?.onSuccess?.(res, req, meta); + return opts[0]?.onSuccess?.(res, req, meta); }, }); } @@ -37,7 +55,74 @@ export function useRecrawlBookmark( ...opts, onSuccess: (res, req, meta) => { apiUtils.bookmarks.getBookmark.invalidate({ bookmarkId: req.bookmarkId }); - opts[0]?.onSuccess?.(res, req, meta); + return opts[0]?.onSuccess?.(res, req, meta); + }, + }); +} + +export function useUpdateBookmarkTags( + ...opts: Parameters +) { + const apiUtils = api.useUtils(); + return api.bookmarks.updateTags.useMutation({ + ...opts, + onSuccess: (res, req, meta) => { + apiUtils.bookmarks.getBookmark.invalidate({ bookmarkId: req.bookmarkId }); + + [...res.attached, ...res.detached].forEach((id) => { + apiUtils.tags.get.invalidate({ tagId: id }); + apiUtils.bookmarks.getBookmarks.invalidate({ tagId: id }); + }); + apiUtils.tags.list.invalidate(); + return opts[0]?.onSuccess?.(res, req, meta); }, }); } + +/** + * Checks the grid query context to know if we need to augment the bookmark post creation to fit the grid context + */ +export function useBookmarkPostCreationHook() { + const gridQueryCtx = useBookmarkGridContext(); + const { mutateAsync: updateBookmark } = useUpdateBookmark(); + const { mutateAsync: addToList } = useAddBookmarkToList(); + const { mutateAsync: updateTags } = useUpdateBookmarkTags(); + + return async (bookmarkId: string) => { + if (!gridQueryCtx) { + return; + } + + const promises = []; + if (gridQueryCtx.favourited ?? gridQueryCtx.archived) { + promises.push( + updateBookmark({ + bookmarkId, + favourited: gridQueryCtx.favourited, + archived: gridQueryCtx.archived, + }), + ); + } + + if (gridQueryCtx.listId) { + promises.push( + addToList({ + bookmarkId, + listId: gridQueryCtx.listId, + }), + ); + } + + if (gridQueryCtx.tagId) { + promises.push( + updateTags({ + bookmarkId, + attach: [{ tagId: gridQueryCtx.tagId }], + detach: [], + }), + ); + } + + return Promise.all(promises); + }; +} -- cgit v1.2.3-70-g09d2