diff options
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/shared-react/hooks/tags.ts | 14 | ||||
| -rw-r--r-- | packages/trpc/routers/tags.ts | 26 |
2 files changed, 38 insertions, 2 deletions
diff --git a/packages/shared-react/hooks/tags.ts b/packages/shared-react/hooks/tags.ts index d3129fed..6ce0f0c9 100644 --- a/packages/shared-react/hooks/tags.ts +++ b/packages/shared-react/hooks/tags.ts @@ -53,3 +53,17 @@ export function useDeleteTag( }, }); } + +export function useDeleteUnusedTags( + ...opts: Parameters<typeof api.tags.deleteUnused.useMutation> +) { + const apiUtils = api.useUtils(); + + return api.tags.deleteUnused.useMutation({ + ...opts[0], + onSuccess: (res, req, meta) => { + apiUtils.tags.list.invalidate(); + return opts[0]?.onSuccess?.(res, req, meta); + }, + }); +} diff --git a/packages/trpc/routers/tags.ts b/packages/trpc/routers/tags.ts index b95570ae..dc70b068 100644 --- a/packages/trpc/routers/tags.ts +++ b/packages/trpc/routers/tags.ts @@ -1,5 +1,5 @@ import { experimental_trpcMiddleware, TRPCError } from "@trpc/server"; -import { and, eq, inArray } from "drizzle-orm"; +import { and, eq, inArray, notExists } from "drizzle-orm"; import { z } from "zod"; import type { ZAttachedByEnum } from "@hoarder/shared/types/tags"; @@ -115,6 +115,28 @@ export const tagsAppRouter = router({ } // TODO: Update affected bookmarks in search index }), + deleteUnused: authedProcedure + .output( + z.object({ + deletedTags: z.number(), + }), + ) + .mutation(async ({ ctx }) => { + const res = await ctx.db + .delete(bookmarkTags) + .where( + and( + eq(bookmarkTags.userId, ctx.user.id), + notExists( + ctx.db + .select({ id: tagsOnBookmarks.tagId }) + .from(tagsOnBookmarks) + .where(eq(tagsOnBookmarks.tagId, bookmarkTags.id)), + ), + ), + ); + return { deletedTags: res.changes }; + }), update: authedProcedure .input( z.object({ @@ -261,7 +283,7 @@ export const tagsAppRouter = router({ tagId: input.intoTagId, })), ) - .onConflictDoNothing() + .onConflictDoNothing(); } // Delete the old tags |
