From d07f2c90065f53d36a3fc0e7db54c32d54a2a332 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Thu, 25 Apr 2024 20:15:15 +0100 Subject: feature(web): Add ability to rename, merge and fast delete tags. Fixes #105 (#125) * feature(web): Allow deleting tags from the all tags page * feature(web): Add ability to rename, merge and fast delete tags. Fixes #105 --- packages/shared-react/hooks/tags.ts | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 packages/shared-react/hooks/tags.ts (limited to 'packages/shared-react') diff --git a/packages/shared-react/hooks/tags.ts b/packages/shared-react/hooks/tags.ts new file mode 100644 index 00000000..d3129fed --- /dev/null +++ b/packages/shared-react/hooks/tags.ts @@ -0,0 +1,55 @@ +import { api } from "../trpc"; + +export function useUpdateTag( + ...opts: Parameters +) { + const apiUtils = api.useUtils(); + + return api.tags.update.useMutation({ + ...opts[0], + onSuccess: (res, req, meta) => { + apiUtils.tags.list.invalidate(); + apiUtils.tags.get.invalidate({ tagId: res.id }); + apiUtils.bookmarks.getBookmarks.invalidate({ tagId: res.id }); + + // TODO: Maybe we can only look at the cache and invalidate only affected bookmarks + apiUtils.bookmarks.getBookmark.invalidate(); + return opts[0]?.onSuccess?.(res, req, meta); + }, + }); +} + +export function useMergeTag( + ...opts: Parameters +) { + const apiUtils = api.useUtils(); + + return api.tags.merge.useMutation({ + ...opts[0], + onSuccess: (res, req, meta) => { + apiUtils.tags.list.invalidate(); + [res.mergedIntoTagId, ...res.deletedTags].forEach((tagId) => { + apiUtils.tags.get.invalidate({ tagId }); + apiUtils.bookmarks.getBookmarks.invalidate({ tagId }); + }); + // TODO: Maybe we can only look at the cache and invalidate only affected bookmarks + apiUtils.bookmarks.getBookmark.invalidate(); + return opts[0]?.onSuccess?.(res, req, meta); + }, + }); +} + +export function useDeleteTag( + ...opts: Parameters +) { + const apiUtils = api.useUtils(); + + return api.tags.delete.useMutation({ + ...opts[0], + onSuccess: (res, req, meta) => { + apiUtils.tags.list.invalidate(); + apiUtils.bookmarks.getBookmark.invalidate(); + return opts[0]?.onSuccess?.(res, req, meta); + }, + }); +} -- cgit v1.2.3-70-g09d2