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 --- apps/web/app/dashboard/tags/page.tsx | 53 +---------------- .../components/dashboard/bookmarks/TagsEditor.tsx | 8 +-- apps/web/components/dashboard/tags/AllTagsView.tsx | 67 ++++++++++++++++++++++ 3 files changed, 74 insertions(+), 54 deletions(-) create mode 100644 apps/web/components/dashboard/tags/AllTagsView.tsx (limited to 'apps/web') diff --git a/apps/web/app/dashboard/tags/page.tsx b/apps/web/app/dashboard/tags/page.tsx index 9f5038e7..6caea513 100644 --- a/apps/web/app/dashboard/tags/page.tsx +++ b/apps/web/app/dashboard/tags/page.tsx @@ -1,62 +1,15 @@ -import Link from "next/link"; -import InfoTooltip from "@/components/ui/info-tooltip"; +import AllTagsView from "@/components/dashboard/tags/AllTagsView"; import { Separator } from "@/components/ui/separator"; import { api } from "@/server/api/client"; -function TagPill({ name, count }: { name: string; count: number }) { - return ( - - {name} {count} - - ); -} - export default async function TagsPage() { - let allTags = (await api.tags.list()).tags; - - // Sort tags by usage desc - allTags = allTags.sort((a, b) => b.count - a.count); - - const humanTags = allTags.filter((t) => (t.countAttachedBy.human ?? 0) > 0); - const aiTags = allTags.filter((t) => (t.countAttachedBy.human ?? 0) == 0); - - const tagsToPill = (tags: typeof allTags) => { - let tagPill; - if (tags.length) { - tagPill = tags.map((t) => ( - - )); - } else { - tagPill = "No Tags"; - } - return tagPill; - }; + const allTags = (await api.tags.list()).tags; return (
All Tags - - -

Your Tags

- -

Tags that were attached at least once by you

-
-
-
{tagsToPill(humanTags)}
- - - - -

AI Tags

- -

Tags that were only attached automatically (by AI)

-
-
-
{tagsToPill(aiTags)}
+
); } diff --git a/apps/web/components/dashboard/bookmarks/TagsEditor.tsx b/apps/web/components/dashboard/bookmarks/TagsEditor.tsx index ecd6d29c..91294b2e 100644 --- a/apps/web/components/dashboard/bookmarks/TagsEditor.tsx +++ b/apps/web/components/dashboard/bookmarks/TagsEditor.tsx @@ -17,16 +17,16 @@ interface EditableTag { export function TagsEditor({ bookmark }: { bookmark: ZBookmark }) { const demoMode = !!useClientConfig().demoMode; - const bookmarkInvalidationFunction = - api.useUtils().bookmarks.getBookmark.invalidate; + const apiUtils = api.useUtils(); const { mutate } = api.bookmarks.updateTags.useMutation({ onSuccess: () => { toast({ description: "Tags has been updated!", }); - bookmarkInvalidationFunction({ bookmarkId: bookmark.id }); - // TODO(bug) Invalidate the tag views as well + apiUtils.bookmarks.getBookmark.invalidate({ bookmarkId: bookmark.id }); + apiUtils.tags.list.invalidate(); + apiUtils.tags.get.invalidate(); }, onError: () => { toast({ diff --git a/apps/web/components/dashboard/tags/AllTagsView.tsx b/apps/web/components/dashboard/tags/AllTagsView.tsx new file mode 100644 index 00000000..0f9ee823 --- /dev/null +++ b/apps/web/components/dashboard/tags/AllTagsView.tsx @@ -0,0 +1,67 @@ +"use client"; + +import Link from "next/link"; +import InfoTooltip from "@/components/ui/info-tooltip"; +import { Separator } from "@/components/ui/separator"; +import { api } from "@/lib/trpc"; + +import type { ZGetTagResponse } from "@hoarder/trpc/types/tags"; + +function TagPill({ name, count }: { name: string; count: number }) { + return ( + + {name} {count} + + ); +} + +export default function AllTagsView({ + initialData, +}: { + initialData: ZGetTagResponse[]; +}) { + const { data } = api.tags.list.useQuery(undefined, { + initialData: { tags: initialData }, + }); + // Sort tags by usage desc + const allTags = data.tags.sort((a, b) => b.count - a.count); + + const humanTags = allTags.filter((t) => (t.countAttachedBy.human ?? 0) > 0); + const aiTags = allTags.filter((t) => (t.countAttachedBy.human ?? 0) == 0); + + const tagsToPill = (tags: typeof allTags) => { + let tagPill; + if (tags.length) { + tagPill = tags.map((t) => ( + + )); + } else { + tagPill = "No Tags"; + } + return tagPill; + }; + return ( + <> + +

Your Tags

+ +

Tags that were attached at least once by you

+
+
+
{tagsToPill(humanTags)}
+ + + + +

AI Tags

+ +

Tags that were only attached automatically (by AI)

+
+
+
{tagsToPill(aiTags)}
+ + ); +} -- cgit v1.2.3-70-g09d2