diff options
| author | Mohamed Bassem <me@mbassem.com> | 2024-04-25 20:15:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-25 20:15:15 +0100 |
| commit | d07f2c90065f53d36a3fc0e7db54c32d54a2a332 (patch) | |
| tree | 27102aeb30ee9798ca639517ff577bc7d135a4b4 /packages/shared-react/hooks | |
| parent | da6df7c7853e9c8350e52d6f4c17021667caf8b8 (diff) | |
| download | karakeep-d07f2c90065f53d36a3fc0e7db54c32d54a2a332.tar.zst | |
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
Diffstat (limited to 'packages/shared-react/hooks')
| -rw-r--r-- | packages/shared-react/hooks/tags.ts | 55 |
1 files changed, 55 insertions, 0 deletions
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<typeof api.tags.update.useMutation> +) { + 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<typeof api.tags.merge.useMutation> +) { + 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<typeof api.tags.delete.useMutation> +) { + 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); + }, + }); +} |
