The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add tag selector to analytics page
Tim Wilson2a2a6b6d0b7d
diff --git a/apps/web/ui/analytics/index.tsx b/apps/web/ui/analytics/index.tsxindex fd6f62ff2d..622d9a5323 100644--- a/apps/web/ui/analytics/index.tsx+++ b/apps/web/ui/analytics/index.tsx@@ -28,6 +28,7 @@ export const AnalyticsContext = createContext<{url?: string;queryString: string;interval: string;+ tagId?: string;totalClicks?: number;}>({basePath: "",@@ -56,6 +57,8 @@ export default function Analytics({key = searchParams?.get("key") || key;const interval = searchParams?.get("interval") || "24h";+ const tagId = searchParams?.get("tagId") ?? undefined;+const { basePath, domain, baseApiPath } = useMemo(() => {// Project link analytics page, e.g. app.dub.co/dub/analytics?domain=dub.sh&key=githubif (slug) {@@ -90,8 +93,9 @@ export default function Analytics({...(key && { key }),...availableFilterParams,...(interval && { interval }),+ ...(tagId && { tagId }),}).toString();- }, [slug, domain, key, searchParams, interval]);+ }, [slug, domain, key, searchParams, interval, tagId]);const { data: totalClicks } = useSWR<number>(`${baseApiPath}/clicks?${queryString}`,@@ -110,6 +114,7 @@ export default function Analytics({key: key ? decodeURIComponent(key) : undefined, // link key (e.g. github, weathergpt, etc.)url: staticUrl, // url for the link (only for public stats pages)interval, // time interval (e.g. 24h, 7d, 30d, etc.)+ tagId,totalClicks, // total clicks for the link}}>diff --git a/apps/web/ui/analytics/tag-selector.tsx b/apps/web/ui/analytics/tag-selector.tsxnew file mode 100644index 0000000000..53fcb607c2--- /dev/null+++ b/apps/web/ui/analytics/tag-selector.tsx@@ -0,0 +1,71 @@+import useTags from "@/lib/swr/use-tags";+import { IconMenu, Popover, Tick, useRouterStuff } from "@dub/ui";+import { ChevronDown, Tag } from "lucide-react";+import Link from "next/link";+import { useContext, useState } from "react";+import { AnalyticsContext } from ".";+import TagBadge from "../links/tag-badge";++export default function TagSelector() {+ const { queryParams } = useRouterStuff();++ const { tags } = useTags();+ const { tagId: selectedTagId } = useContext(AnalyticsContext);++ const [openPopover, setOpenPopover] = useState(false);++ return tags && tags.length > 0 ? (+ <Popover+ content={+ <div className="grid w-full p-2 md:w-48">+ <Link+ href={+ queryParams({+ del: "tagId",+ getNewPath: true,+ }) as string+ }+ className="flex w-full items-center justify-between space-x-2 rounded-md p-2 hover:bg-gray-100 active:bg-gray-200"+ >+ <p className="text-sm">All tags</p>+ {!selectedTagId && <Tick className="h-4 w-4" aria-hidden="true" />}+ </Link>+ {tags.map((tag) => (+ <Link+ key={tag.id}+ href={+ queryParams({+ set: { tagId: tag.id },+ getNewPath: true,+ }) as string+ }+ className="flex w-full items-center justify-between space-x-2 rounded-md p-2 hover:bg-gray-100 active:bg-gray-200"+ >+ <TagBadge {...tag} />+ {selectedTagId === tag.id && (+ <Tick className="h-4 w-4" aria-hidden="true" />+ )}+ </Link>+ ))}+ </div>+ }+ openPopover={openPopover}+ setOpenPopover={setOpenPopover}+ >+ <button+ onClick={(o) => setOpenPopover(!o)}+ className="flex w-full items-center justify-between space-x-2 rounded-md bg-white px-3 py-2.5 shadow transition-all hover:shadow-md md:w-48"+ >+ <IconMenu+ text={tags.find(({ id }) => id === selectedTagId)?.name || "All tags"}+ icon={<Tag className="h-4 w-4" />}+ />+ <ChevronDown+ className={`h-5 w-5 text-gray-400 ${+ openPopover ? "rotate-180 transform" : ""+ } transition-all duration-75`}+ />+ </button>+ </Popover>+ ) : undefined;+}diff --git a/apps/web/ui/analytics/toggle.tsx b/apps/web/ui/analytics/toggle.tsxindex 1d8a433cfa..825bc6acb9 100644--- a/apps/web/ui/analytics/toggle.tsx+++ b/apps/web/ui/analytics/toggle.tsx@@ -33,6 +33,7 @@ import punycode from "punycode/";import { useContext, useMemo, useState } from "react";import { toast } from "sonner";import { AnalyticsContext } from ".";+import TagSelector from "./tag-selector";export default function Toggle() {const { slug } = useParams() as { slug?: string };@@ -104,8 +105,9 @@ export default function Toggle() {)}</div>)}- <div className="flex items-center">+ <div className="flex items-center gap-2">{!isPublicStatsPage && key && <SharePopover />}+ {!isPublicStatsPage && !key && <TagSelector />}<Popovercontent={<div className="grid w-full p-2 md:w-48">@@ -339,7 +341,7 @@ const SharePopover = () => {><buttononClick={() => setopenSharePopoverPopover(!openSharePopover)}- className="mr-2 flex w-24 items-center justify-center space-x-2 rounded-md bg-white px-3 py-2.5 shadow transition-all duration-75 hover:shadow-md active:scale-95"+ className="flex w-24 items-center justify-center space-x-2 rounded-md bg-white px-3 py-2.5 shadow transition-all duration-75 hover:shadow-md active:scale-95"><IconMenu text="Share" icon={<Share2 className="h-4 w-4" />} /></button>