From 044659fd1ba2082491eed713dc72bafd696f0439 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sat, 6 Apr 2024 02:13:47 +0100 Subject: fix: Refresh the all tags page automatically when a tag is modified --- packages/trpc/routers/tags.ts | 50 ++++++++++++++++++------------------------- packages/trpc/types/tags.ts | 8 +++++++ 2 files changed, 29 insertions(+), 29 deletions(-) (limited to 'packages') diff --git a/packages/trpc/routers/tags.ts b/packages/trpc/routers/tags.ts index b69c98e8..53b72a23 100644 --- a/packages/trpc/routers/tags.ts +++ b/packages/trpc/routers/tags.ts @@ -7,7 +7,7 @@ import { bookmarkTags, tagsOnBookmarks } from "@hoarder/db/schema"; import type { Context } from "../index"; import type { ZAttachedByEnum } from "../types/tags"; import { authedProcedure, router } from "../index"; -import { zAttachedByEnumSchema } from "../types/tags"; +import { zGetTagResponseSchema } from "../types/tags"; function conditionFromInput( input: { tagName: string } | { tagId: string }, @@ -57,13 +57,6 @@ const ensureTagOwnership = experimental_trpcMiddleware<{ return opts.next(); }); -const zTagSchema = z.object({ - id: z.string(), - name: z.string(), - count: z.number(), - countAttachedBy: z.record(zAttachedByEnumSchema, z.number()), -}); - export const tagsAppRouter = router({ get: authedProcedure .input( @@ -77,7 +70,7 @@ export const tagsAppRouter = router({ }), ), ) - .output(zTagSchema) + .output(zGetTagResponseSchema) .use(ensureTagOwnership) .query(async ({ input, ctx }) => { const res = await ctx.db @@ -145,7 +138,7 @@ export const tagsAppRouter = router({ list: authedProcedure .output( z.object({ - tags: z.array(zTagSchema), + tags: z.array(zGetTagResponseSchema), }), ) .query(async ({ ctx }) => { @@ -161,25 +154,24 @@ export const tagsAppRouter = router({ .innerJoin(bookmarkTags, eq(bookmarkTags.id, tagsOnBookmarks.tagId)) .where(eq(bookmarkTags.userId, ctx.user.id)); - const tags = res.reduce>>( - (acc, row) => { - if (!(row.id in acc)) { - acc[row.id] = { - id: row.id, - name: row.name, - count: 0, - countAttachedBy: { - ai: 0, - human: 0, - }, - }; - } - acc[row.id].count += row.count; - acc[row.id].countAttachedBy[row.attachedBy]! += row.count; - return acc; - }, - {}, - ); + const tags = res.reduce< + Record> + >((acc, row) => { + if (!(row.id in acc)) { + acc[row.id] = { + id: row.id, + name: row.name, + count: 0, + countAttachedBy: { + ai: 0, + human: 0, + }, + }; + } + acc[row.id].count += row.count; + acc[row.id].countAttachedBy[row.attachedBy]! += row.count; + return acc; + }, {}); return { tags: Object.values(tags) }; }), }); diff --git a/packages/trpc/types/tags.ts b/packages/trpc/types/tags.ts index 7a99dad4..c9fe2a93 100644 --- a/packages/trpc/types/tags.ts +++ b/packages/trpc/types/tags.ts @@ -8,3 +8,11 @@ export const zBookmarkTagSchema = z.object({ attachedBy: zAttachedByEnumSchema, }); export type ZBookmarkTags = z.infer; + +export const zGetTagResponseSchema = z.object({ + id: z.string(), + name: z.string(), + count: z.number(), + countAttachedBy: z.record(zAttachedByEnumSchema, z.number()), +}); +export type ZGetTagResponse = z.infer; -- cgit v1.2.3-70-g09d2