diff options
| author | MohamedBassem <me@mbassem.com> | 2024-04-06 02:13:47 +0100 |
|---|---|---|
| committer | MohamedBassem <me@mbassem.com> | 2024-04-06 02:13:47 +0100 |
| commit | 044659fd1ba2082491eed713dc72bafd696f0439 (patch) | |
| tree | d597da0ae1165e6a2dc50387121ea079a607b8a3 /packages | |
| parent | 4cf990816817c009a512356373fdb1c4baa5e63b (diff) | |
| download | karakeep-044659fd1ba2082491eed713dc72bafd696f0439.tar.zst | |
fix: Refresh the all tags page automatically when a tag is modified
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/trpc/routers/tags.ts | 50 | ||||
| -rw-r--r-- | packages/trpc/types/tags.ts | 8 |
2 files changed, 29 insertions, 29 deletions
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<Record<string, z.infer<typeof zTagSchema>>>( - (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<string, z.infer<typeof zGetTagResponseSchema>> + >((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<typeof zBookmarkTagSchema>; + +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<typeof zGetTagResponseSchema>; |
