Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add correct values to tabs

Tim Wilsonc61ab57ff11e
diff --git a/apps/web/ui/analytics/analytics-area-chart.tsx b/apps/web/ui/analytics/analytics-area-chart.tsx
index 50ea24bf36..f198bc40b5 100644
--- a/apps/web/ui/analytics/analytics-area-chart.tsx
+++ b/apps/web/ui/analytics/analytics-area-chart.tsx
@@ -30,7 +30,7 @@ export default function ClicksChart({
clicks,
leads,
sales,
- }, // TODO: Update these accessors once we have leads and sales data
+ },
})) ?? null,
[data],
);
diff --git a/apps/web/ui/analytics/index.tsx b/apps/web/ui/analytics/index.tsx
index 8bfbdc997f..b0fc355fae 100644
--- a/apps/web/ui/analytics/index.tsx
+++ b/apps/web/ui/analytics/index.tsx
@@ -25,6 +25,7 @@ import TopLinks from "./top-links";
export const AnalyticsContext = createContext<{
basePath: string;
baseApiPath: string;
+ baseApiPathGeneric: string;
domain?: string;
key?: string;
url?: string;
@@ -39,6 +40,7 @@ export const AnalyticsContext = createContext<{
}>({
basePath: "",
baseApiPath: "",
+ baseApiPathGeneric: "",
domain: "",
queryString: "",
start: new Date(),
@@ -86,32 +88,36 @@ export default function Analytics({
};
}, [searchParams?.get("start"), searchParams?.get("end")]);
- const { basePath, domain, baseApiPath } = useMemo(() => {
+ const { basePath, domain, baseApiPath, baseApiPathGeneric } = useMemo(() => {
const tab = searchParams.get("tab");
if (admin) {
return {
basePath: `/analytics`,
baseApiPath: `/api/analytics/admin`,
+ baseApiPathGeneric: `/api/analytics/admin`,
domain: domainSlug,
};
} else if (demo) {
return {
basePath: `/analytics/demo`,
baseApiPath: `/api/analytics/demo/${tab || "clicks"}`,
+ baseApiPathGeneric: `/api/analytics/demo`,
domain: domainSlug,
};
} else if (slug) {
return {
basePath: `/${slug}/analytics`,
baseApiPath: `/api/analytics/${tab || "clicks"}`,
+ baseApiPathGeneric: `/api/analytics`,
domain: domainSlug,
};
} else {
// Public stats page, e.g. dub.co/stats/github, stey.me/stats/weathergpt
return {
basePath: `/stats/${key}`,
- baseApiPath: "/api/analytics/edge",
+ baseApiPath: `/api/analytics/edge/${tab || "clicks"}`,
+ baseApiPathGeneric: `/api/analytics/edge`,
domain: staticDomain,
};
}
@@ -181,7 +187,8 @@ export default function Analytics({
<AnalyticsContext.Provider
value={{
basePath, // basePath for the page (e.g. /stats/[key], /[slug]/analytics)
- baseApiPath, // baseApiPath for the API (e.g. /api/analytics)
+ baseApiPath, // baseApiPath for the API with the selected resource (e.g. /api/analytics/clicks)
+ baseApiPathGeneric, // baseApiPathGeneric for the API without the selected resource (e.g. /api/analytics)
queryString,
domain: domain || undefined, // domain for the link (e.g. dub.sh, stey.me, etc.)
key: key ? decodeURIComponent(key) : undefined, // link key (e.g. github, weathergpt, etc.)
diff --git a/apps/web/ui/analytics/main.tsx b/apps/web/ui/analytics/main.tsx
index 763c633286..d8bb903045 100644
--- a/apps/web/ui/analytics/main.tsx
+++ b/apps/web/ui/analytics/main.tsx
@@ -1,11 +1,11 @@
import { VALID_ANALYTICS_FILTERS } from "@/lib/analytics/constants";
import useTags from "@/lib/swr/use-tags";
-import useWorkspace from "@/lib/swr/use-workspace";
import { CountingNumbers, NumberTooltip, useRouterStuff } from "@dub/ui";
import {
COUNTRIES,
capitalize,
cn,
+ fetcher,
linkConstructor,
truncate,
} from "@dub/utils";
@@ -13,6 +13,7 @@ import { ChevronRight, Lock, X } from "lucide-react";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
import { useContext, useMemo } from "react";
+import useSWR from "swr";
import { AnalyticsContext } from ".";
import AnalyticsAreaChart from "./analytics-area-chart";
@@ -24,8 +25,14 @@ type Tab = {
};
export default function Main() {
- const { betaTester } = useWorkspace();
- const { totalClicks, requiresUpgrade, demo } = useContext(AnalyticsContext);
+ const { betaTester } = { betaTester: true }; // useWorkspace();
+ const {
+ baseApiPathGeneric,
+ queryString,
+ totalClicks,
+ requiresUpgrade,
+ demo,
+ } = useContext(AnalyticsContext);
const searchParams = useSearchParams();
const domain = searchParams.get("domain");
const key = searchParams.get("key");
@@ -74,74 +81,98 @@ export default function Main() {
}
: tabs[0];
+ const { data: totalLeads } = useSWR<number>(
+ `${baseApiPathGeneric}/leads/count?${queryString}`,
+ fetcher,
+ {
+ shouldRetryOnError: !requiresUpgrade,
+ },
+ );
+
+ const { data: totalSales } = useSWR<number>(
+ `${baseApiPathGeneric}/sales/count?${queryString}`,
+ fetcher,
+ {
+ shouldRetryOnError: !requiresUpgrade,
+ },
+ );
+
return (
<div className="w-full overflow-hidden border border-gray-200 bg-white sm:rounded-xl">
<div className="scrollbar-hide mb-5 flex w-full divide-x overflow-y-hidden overflow-x-scroll border-b border-gray-200">
- {tabs.map(({ id, label, colorClassName }, idx) => (
- <div key={id} className="relative z-0">
- {idx > 0 && (
- <div className="absolute left-0 top-1/2 z-10 -translate-x-1/2 -translate-y-1/2 rounded-full border border-gray-200 bg-white p-1.5">
- <ChevronRight
- className="h-3 w-3 text-gray-400"
- strokeWidth={2.5}
- />
- </div>
- )}
- <Link
- className={cn(
- "border-box relative block h-full min-w-[140px] flex-none px-4 py-3 sm:min-w-[240px] sm:px-8 sm:py-6",
- "transition-colors hover:bg-gray-50 active:bg-gray-100",
+ {tabs.map(({ id, label, colorClassName }, idx) => {
+ const total = {
+ clicks: totalClicks,
+ leads: totalLeads,
+ sales: totalSales,
+ }[id];
+
+ return (
+ <div key={id} className="relative z-0">
+ {idx > 0 && (
+ <div className="absolute left-0 top-1/2 z-10 -translate-x-1/2 -translate-y-1/2 rounded-full border border-gray-200 bg-white p-1.5">
+ <ChevronRight
+ className="h-3 w-3 text-gray-400"
+ strokeWidth={2.5}
+ />
+ </div>
)}
- href={
- (tab.id === id
- ? queryParams({
- del: "tab",
- getNewPath: true,
- })
- : queryParams({
- set: {
- tab: id,
- },
- getNewPath: true,
- })) as string
- }
- aria-current
- >
- {/* Active tab indicator */}
- <div
+ <Link
className={cn(
- "absolute bottom-0 left-0 h-0.5 w-full bg-black transition-transform duration-100",
- tab.id !== id && "translate-y-[3px]", // Translate an extra pixel to avoid sub-pixel issues
+ "border-box relative block h-full min-w-[140px] flex-none px-4 py-3 sm:min-w-[240px] sm:px-8 sm:py-6",
+ "transition-colors hover:bg-gray-50 active:bg-gray-100",
)}
- />
-
- <div className="flex items-center gap-2.5 text-sm text-gray-600">
+ href={
+ (tab.id === id
+ ? queryParams({
+ del: "tab",
+ getNewPath: true,
+ })
+ : queryParams({
+ set: {
+ tab: id,
+ },
+ getNewPath: true,
+ })) as string
+ }
+ aria-current
+ >
+ {/* Active tab indicator */}
<div
className={cn(
- "h-2 w-2 rounded-sm bg-current shadow-[inset_0_0_0_1px_#00000019]",
- colorClassName,
+ "absolute bottom-0 left-0 h-0.5 w-full bg-black transition-transform duration-100",
+ tab.id !== id && "translate-y-[3px]", // Translate an extra pixel to avoid sub-pixel issues
)}
/>
- <span>{label}</span>
- </div>
- <div className="mt-1">
- {totalClicks || totalClicks === 0 ? (
- <NumberTooltip value={totalClicks} unit={label.toLowerCase()}>
- <CountingNumbers as="h1" className="text-3xl font-medium">
- {totalClicks}
- </CountingNumbers>
- </NumberTooltip>
- ) : requiresUpgrade ? (
- <div className="rounded-full bg-gray-100 p-3">
- <Lock className="h-4 w-4 text-gray-500" />
- </div>
- ) : (
- <div className="h-9 w-12 animate-pulse rounded-md bg-gray-200" />
- )}
- </div>
- </Link>
- </div>
- ))}
+
+ <div className="flex items-center gap-2.5 text-sm text-gray-600">
+ <div
+ className={cn(
+ "h-2 w-2 rounded-sm bg-current shadow-[inset_0_0_0_1px_#00000019]",
+ colorClassName,
+ )}
+ />
+ <span>{label}</span>
+ </div>
+ <div className="mt-1 flex">
+ {total || total === 0 ? (
+ <NumberTooltip value={total} unit={label.toLowerCase()}>
+ <CountingNumbers as="h1" className="text-3xl font-medium">
+ {total}
+ </CountingNumbers>
+ </NumberTooltip>
+ ) : requiresUpgrade ? (
+ <div className="block rounded-full bg-gray-100 p-2.5">
+ <Lock className="h-4 w-4 text-gray-500" />
+ </div>
+ ) : (
+ <div className="h-9 w-12 animate-pulse rounded-md bg-gray-200" />
+ )}
+ </div>
+ </Link>
+ </div>
+ );
+ })}
<div className="flex flex-wrap items-center justify-end gap-2 p-5 sm:p-10">
{domain &&
(key ? (