From a9e4ec0922b5da71aaae7f6a037f124c86b38edc Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Fri, 15 Mar 2024 13:32:05 +0000 Subject: refactor: Move tag fetching to trpc to reuse in the mobile app --- apps/web/app/dashboard/tags/[tagName]/page.tsx | 36 +++++++++----------------- 1 file changed, 12 insertions(+), 24 deletions(-) (limited to 'apps/web') diff --git a/apps/web/app/dashboard/tags/[tagName]/page.tsx b/apps/web/app/dashboard/tags/[tagName]/page.tsx index 51b3cb0b..0c5c1c1f 100644 --- a/apps/web/app/dashboard/tags/[tagName]/page.tsx +++ b/apps/web/app/dashboard/tags/[tagName]/page.tsx @@ -2,10 +2,7 @@ import { notFound, redirect } from "next/navigation"; import BookmarksGrid from "@/components/dashboard/bookmarks/BookmarksGrid"; import { api } from "@/server/api/client"; import { getServerAuthSession } from "@/server/auth"; -import { and, eq } from "drizzle-orm"; - -import { db } from "@hoarder/db"; -import { bookmarkTags, tagsOnBookmarks } from "@hoarder/db/schema"; +import { TRPCError } from "@trpc/server"; export default async function TagPage({ params, @@ -17,30 +14,21 @@ export default async function TagPage({ redirect("/"); } const tagName = decodeURIComponent(params.tagName); - const tag = await db.query.bookmarkTags.findFirst({ - where: and( - eq(bookmarkTags.userId, session.user.id), - eq(bookmarkTags.name, tagName), - ), - columns: { - id: true, - }, - }); - if (!tag) { - // TODO: Better error message when the tag is not there - notFound(); + let tag; + try { + tag = await api.tags.get({ tagName }); + } catch (e) { + if (e instanceof TRPCError) { + if (e.code == "NOT_FOUND") { + notFound(); + } + } + throw e; } - const bookmarkIds = await db.query.tagsOnBookmarks.findMany({ - where: eq(tagsOnBookmarks.tagId, tag.id), - columns: { - bookmarkId: true, - }, - }); - const query = { - ids: bookmarkIds.map((b) => b.bookmarkId), + ids: tag.bookmarks, archived: false, }; -- cgit v1.2.3-70-g09d2