From da03fce5e5373e4791ccf0a885aaecff513156fe Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Tue, 13 Feb 2024 20:49:42 +0000 Subject: feature: Implement a usable but ugly all tags and a single tag page --- packages/db/prisma/schema.prisma | 1 + .../dashboard/bookmarks/components/Bookmarks.tsx | 19 ++++---- .../dashboard/bookmarks/components/LinkCard.tsx | 15 ++++--- packages/web/app/dashboard/components/Sidebar.tsx | 13 +----- packages/web/app/dashboard/tags/[tagName]/page.tsx | 51 ++++++++++++++++++++++ packages/web/app/dashboard/tags/page.tsx | 36 +++++++++++++++ packages/web/server/api/routers/bookmarks.ts | 29 ++++++++++++ packages/web/server/api/routers/tags.ts | 0 8 files changed, 135 insertions(+), 29 deletions(-) create mode 100644 packages/web/app/dashboard/tags/[tagName]/page.tsx create mode 100644 packages/web/app/dashboard/tags/page.tsx create mode 100644 packages/web/server/api/routers/tags.ts (limited to 'packages') diff --git a/packages/db/prisma/schema.prisma b/packages/db/prisma/schema.prisma index 5c575c97..e77297c6 100644 --- a/packages/db/prisma/schema.prisma +++ b/packages/db/prisma/schema.prisma @@ -102,6 +102,7 @@ model BookmarkedLink { model BookmarkTags { id String @id @default(cuid()) + // TODO: Tags are unique per user not globally name String @unique createdAt DateTime @default(now()) diff --git a/packages/web/app/dashboard/bookmarks/components/Bookmarks.tsx b/packages/web/app/dashboard/bookmarks/components/Bookmarks.tsx index d7e3f1f3..bd144a67 100644 --- a/packages/web/app/dashboard/bookmarks/components/Bookmarks.tsx +++ b/packages/web/app/dashboard/bookmarks/components/Bookmarks.tsx @@ -20,20 +20,17 @@ export default async function Bookmarks({ archived, }); - if (bookmarks.bookmarks.length == 0) { - // TODO: This needs to be polished - return ( - <> -
{title}
-
No bookmarks
- - ); - } - + // TODO: This needs to be polished return ( <>
{title}
- +
+ {bookmarks.bookmarks.length == 0 ? ( + "No bookmarks" + ) : ( + + )} +
); } diff --git a/packages/web/app/dashboard/bookmarks/components/LinkCard.tsx b/packages/web/app/dashboard/bookmarks/components/LinkCard.tsx index abd4bff7..00e4ef26 100644 --- a/packages/web/app/dashboard/bookmarks/components/LinkCard.tsx +++ b/packages/web/app/dashboard/bookmarks/components/LinkCard.tsx @@ -27,13 +27,14 @@ export default function LinkCard({ bookmark }: { bookmark: ZBookmark }) { {bookmark.tags.map((t) => ( - - #{t.name} - + + + #{t.name} + + ))} diff --git a/packages/web/app/dashboard/components/Sidebar.tsx b/packages/web/app/dashboard/components/Sidebar.tsx index 0563e26f..30179166 100644 --- a/packages/web/app/dashboard/components/Sidebar.tsx +++ b/packages/web/app/dashboard/components/Sidebar.tsx @@ -1,13 +1,4 @@ -import { Button } from "@/components/ui/button"; -import { - Archive, - MoreHorizontal, - Star, - Tag, - Home, - Brain, - Settings, -} from "lucide-react"; +import { Archive, Star, Tag, Home, Brain, Settings } from "lucide-react"; import { redirect } from "next/navigation"; import SidebarItem from "./SidebarItem"; import { getServerAuthSession } from "@/server/auth"; @@ -46,7 +37,7 @@ export default async function Sidebar() { name="Archive" path="/dashboard/bookmarks/archive" /> - } name="Tags" path="#" /> + } name="Tags" path="/dashboard/tags" /> } name="Settings" diff --git a/packages/web/app/dashboard/tags/[tagName]/page.tsx b/packages/web/app/dashboard/tags/[tagName]/page.tsx new file mode 100644 index 00000000..e55c7d7e --- /dev/null +++ b/packages/web/app/dashboard/tags/[tagName]/page.tsx @@ -0,0 +1,51 @@ +import { getServerAuthSession } from "@/server/auth"; +import { prisma } from "@remember/db"; +import { notFound, redirect } from "next/navigation"; +import BookmarksGrid from "../../bookmarks/components/BookmarksGrid"; +import { api } from "@/server/api/client"; + +export default async function TagPage({ + params, +}: { + params: { tagName: string }; +}) { + const session = await getServerAuthSession(); + if (!session) { + redirect("/"); + } + const tag = await prisma.bookmarkTags.findUnique({ + where: { + userId: session.user.id, + name: params.tagName, + }, + select: { + id: true, + }, + }); + + if (!tag) { + // TODO: Better error message when the tag is not there + notFound(); + } + + const bookmarkIds = await prisma.tagsOnBookmarks.findMany({ + where: { + tagId: tag.id, + }, + select: { + bookmarkId: true, + }, + }); + + const bookmarks = await api.bookmarks.getBookmarksById({ + ids: bookmarkIds.map((b) => b.bookmarkId), + archived: false, + }); + + return ( +
+ #{params.tagName} + +
+ ); +} diff --git a/packages/web/app/dashboard/tags/page.tsx b/packages/web/app/dashboard/tags/page.tsx new file mode 100644 index 00000000..546ee28d --- /dev/null +++ b/packages/web/app/dashboard/tags/page.tsx @@ -0,0 +1,36 @@ +import { Button } from "@/components/ui/button"; +import { getServerAuthSession } from "@/server/auth"; +import { prisma } from "@remember/db"; +import Link from "next/link"; +import { redirect } from "next/navigation"; + +export default async function TagsPage() { + const session = await getServerAuthSession(); + if (!session) { + redirect("/"); + } + + const tags = await prisma.bookmarkTags.findMany({ + where: { + userId: session.user.id, + }, + }); + + return ( +
+ All Tags +
+
+ {tags.map((t) => ( + + {t.name} + + ))} +
+
+ ); +} diff --git a/packages/web/server/api/routers/bookmarks.ts b/packages/web/server/api/routers/bookmarks.ts index 0b97563f..953dab66 100644 --- a/packages/web/server/api/routers/bookmarks.ts +++ b/packages/web/server/api/routers/bookmarks.ts @@ -134,6 +134,35 @@ export const bookmarksAppRouter = router({ }) ).map(toZodSchema); + return { bookmarks }; + }), + getBookmarksById: authedProcedure + .input( + zGetBookmarksRequestSchema.merge( + z.object({ + ids: z.array(z.string()), + }), + ), + ) + .output(zGetBookmarksResponseSchema) + .query(async ({ input, ctx }) => { + const bookmarks = ( + await prisma.bookmark.findMany({ + where: { + id: { + in: input.ids, + }, + userId: ctx.user.id, + archived: input.archived, + favourited: input.favourited, + }, + orderBy: { + createdAt: "desc", + }, + select: defaultBookmarkFields, + }) + ).map(toZodSchema); + return { bookmarks }; }), }); diff --git a/packages/web/server/api/routers/tags.ts b/packages/web/server/api/routers/tags.ts new file mode 100644 index 00000000..e69de29b -- cgit v1.2.3-70-g09d2