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.

More UI updates w/ composite chart!

Tim Wilson0a1a93d802c8
diff --git a/apps/web/ui/analytics/analytics-area-chart.tsx b/apps/web/ui/analytics/analytics-area-chart.tsx
index 52ffb85bd7..e2687c3bdb 100644
--- a/apps/web/ui/analytics/analytics-area-chart.tsx
+++ b/apps/web/ui/analytics/analytics-area-chart.tsx
@@ -1,4 +1,4 @@
-import { fetcher, getDaysDifference, nFormatter } from "@dub/utils";
+import { cn, fetcher, getDaysDifference, nFormatter } from "@dub/utils";
import { useCallback, useContext, useMemo } from "react";
import useSWR from "swr";
import { AnalyticsContext } from ".";
@@ -28,7 +28,11 @@ export default function ClicksChart({
() =>
data?.map(({ start, clicks }) => ({
date: new Date(start),
- values: { clicks, leads: clicks, sales: clicks }, // TODO: Update these accessors once we have leads and sales data
+ values: {
+ clicks,
+ leads: Math.ceil(clicks / 2),
+ sales: Math.floor(clicks / 2),
+ }, // TODO: Update these accessors once we have leads and sales data
})) ?? null,
[data],
);
@@ -62,16 +66,19 @@ export default function ClicksChart({
id: "clicks",
valueAccessor: (d) => d.values.clicks,
isActive: show.includes("clicks"),
+ colorClassName: "text-sky-400",
},
{
id: "leads",
valueAccessor: (d) => d.values.leads,
isActive: show.includes("leads"),
+ colorClassName: "text-violet-600",
},
{
id: "sales",
valueAccessor: (d) => d.values.sales,
isActive: show.includes("sales"),
+ colorClassName: "text-teal-400",
},
];
@@ -85,17 +92,30 @@ export default function ClicksChart({
tooltipContent={(d) => (
<>
{show.map((resource) => {
+ const s = series.find(({ id }) => id === resource);
const value = d.values[resource];
return (
- <p className="text-gray-700">
- <strong className="text-gray-800">
- {nFormatter(value, { full: true })}
- </strong>{" "}
- {value === 1 ? resource.slice(0, -1) : resource}
- </p>
+ <div className="flex items-center gap-2">
+ {s && (
+ <div
+ className={cn(
+ s.colorClassName,
+ "h-2 w-2 rounded-sm bg-current opacity-50 shadow-[inset_0_0_0_1px_#0003]",
+ )}
+ />
+ )}
+ <p className="text-gray-600">
+ <strong className="font-medium text-gray-800">
+ {nFormatter(value, { full: true })}
+ </strong>{" "}
+ {value === 1 ? resource.slice(0, -1) : resource}
+ </p>
+ </div>
);
})}
- <p className="text-sm text-gray-500">{formatDate(d.date)}</p>
+ <p className="mt-0.5 text-sm text-gray-500">
+ {formatDate(d.date)}
+ </p>
</>
)}
>
diff --git a/apps/web/ui/analytics/index.tsx b/apps/web/ui/analytics/index.tsx
index f1aabc6b7f..e5475a97eb 100644
--- a/apps/web/ui/analytics/index.tsx
+++ b/apps/web/ui/analytics/index.tsx
@@ -80,7 +80,7 @@ export default function Analytics({
new Date(),
]),
};
- }, [searchParams]);
+ }, [searchParams?.get("start"), searchParams?.get("end")]);
const { basePath, domain, baseApiPath } = useMemo(() => {
// Workspace analytics page, e.g. app.dub.co/dub/analytics?domain=dub.sh&key=github
diff --git a/apps/web/ui/analytics/main.tsx b/apps/web/ui/analytics/main.tsx
index 4d6d2cf5a0..60c61a2a8c 100644
--- a/apps/web/ui/analytics/main.tsx
+++ b/apps/web/ui/analytics/main.tsx
@@ -8,15 +8,7 @@ import {
linkConstructor,
truncate,
} from "@dub/utils";
-import { AnimatePresence, motion } from "framer-motion";
-import {
- Crosshair,
- Lock,
- LucideIcon,
- MousePointerClick,
- Receipt,
- X,
-} from "lucide-react";
+import { ChevronRight, Lock, X } from "lucide-react";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
import { useContext, useMemo } from "react";
@@ -25,9 +17,9 @@ import AnalyticsAreaChart from "./analytics-area-chart";
type Tab = {
id: string;
- icon: LucideIcon;
label: string;
- resource: "clicks" | "leads" | "sales";
+ colorClassName: string;
+ show: ("clicks" | "leads" | "sales")[];
};
export default function Main() {
@@ -50,57 +42,82 @@ export default function Main() {
[
{
id: "clicks",
- icon: MousePointerClick,
label: "Clicks",
- resource: "clicks",
+ colorClassName: "text-sky-400/50",
+ show: ["clicks"],
},
...(betaTester
? [
{
id: "leads",
- icon: Crosshair,
label: "Leads",
- resource: "leads",
+ colorClassName: "text-violet-600/50",
+ show: ["leads"],
+ },
+ {
+ id: "sales",
+ label: "Sales",
+ colorClassName: "text-teal-400/50",
+ show: ["sales"],
},
- { id: "sales", icon: Receipt, label: "Sales", resource: "sales" },
]
: []),
] as Tab[],
[betaTester],
);
- const tab =
- tabs.find(({ id }) => id === (searchParams.get("tab") || "clicks")) ||
- tabs[0];
+ const tab = tabs.find(({ id }) => id === searchParams.get("tab")) || {
+ id: "composite",
+ show: ["clicks", "leads", "sales"],
+ };
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-x-scroll border-b border-gray-200">
- {tabs.map(({ id, icon: Icon, label }) => (
- <div>
+ {tabs.map(({ id, label, colorClassName }, idx) => (
+ <div className="relative">
+ {idx > 0 && (
+ <div className="absolute left-0 top-1/2 -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
key={id}
className={cn(
- "block h-full min-w-[120px] flex-none border-black px-4 py-3 sm:min-w-[180px] sm:px-8 sm:py-6",
+ "block h-full min-w-[140px] flex-none border-black px-4 py-3 sm:min-w-[240px] sm:px-8 sm:py-6",
"transition-all duration-100 hover:bg-gray-50 active:bg-gray-100",
tab.id === id ? "border-b-2" : "border-b-none",
)}
href={
- queryParams({
- set: {
- tab: id,
- },
- getNewPath: true,
- }) as string
+ (tab.id === id
+ ? queryParams({
+ del: "tab",
+ getNewPath: true,
+ })
+ : queryParams({
+ set: {
+ tab: id,
+ },
+ getNewPath: true,
+ })) as string
}
>
- <div className="flex items-end gap-3">
+ <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">
{totalClicks || totalClicks === 0 ? (
<NumberTooltip value={totalClicks}>
- <CountingNumbers
- as="h1"
- className="text-3xl font-bold sm:text-4xl"
- >
+ <CountingNumbers as="h1" className="text-3xl font-medium">
{totalClicks}
</CountingNumbers>
</NumberTooltip>
@@ -111,9 +128,7 @@ export default function Main() {
) : (
<div className="h-10 w-12 animate-pulse rounded-md bg-gray-200" />
)}
- <Icon className="mb-2 h-4 w-4 text-gray-600" />
</div>
- <p className="mt-1 text-sm uppercase text-gray-600">{label}</p>
</Link>
</div>
))}
@@ -213,15 +228,7 @@ export default function Main() {
</div>
</div>
<div className="p-5 sm:p-10">
- <AnimatePresence>
- <motion.div
- key={tab.id}
- initial={{ opacity: 0, y: 6 }}
- animate={{ opacity: 1, y: 0 }}
- >
- <AnalyticsAreaChart show={[tab.resource]} />
- </motion.div>
- </AnimatePresence>
+ <AnalyticsAreaChart show={tab.show} />
</div>
</div>
);
diff --git a/apps/web/ui/charts/areas.tsx b/apps/web/ui/charts/areas.tsx
index 7289d4a443..5f88db6657 100644
--- a/apps/web/ui/charts/areas.tsx
+++ b/apps/web/ui/charts/areas.tsx
@@ -1,3 +1,4 @@
+import { cn } from "@dub/utils";
import { LinearGradient } from "@visx/gradient";
import { Group } from "@visx/group";
import { Area, AreaClosed, Circle } from "@visx/shape";
@@ -5,7 +6,11 @@ import { motion } from "framer-motion";
import { Fragment, useMemo } from "react";
import { useChartContext, useChartTooltipContext } from "./chart-context";
-export default function Areas() {
+export default function Areas({
+ seriesClassNames,
+}: {
+ seriesClassNames?: { id: string; gradient?: string; line?: string }[];
+}) {
const { data, series, margin, xScale, yScale, startDate, endDate } =
useChartContext();
const { tooltipData } = useChartTooltipContext();
@@ -20,72 +25,85 @@ export default function Areas() {
return (
<Group left={margin.left} top={margin.top}>
- {series.map((s) => (
- // Prevent ugly x-scale animations when start/end dates change with unique key
- <Fragment key={`${s.id}_${startDate.toString()}_${endDate.toString()}`}>
- {/* Area background gradient */}
- <LinearGradient
- className="text-blue-500"
- id={`${s.id}-background`}
- fromOffset="20%"
- from="currentColor"
- fromOpacity={0.01}
- to="currentColor"
- toOpacity={0.2}
- x1={0}
- x2={0}
- y1={1}
- />
-
- {/* Area */}
- <AreaClosed
- data={data}
- x={(d) => xScale(d.date)}
- y={(d) => yScale(s.valueAccessor(d) ?? 0)}
- yScale={yScale}
+ {series
+ .filter(({ isActive }) => isActive)
+ .map((s) => (
+ // Prevent ugly x-scale animations when start/end dates change with unique key
+ <Fragment
+ key={`${s.id}_${startDate.toString()}_${endDate.toString()}`}
>
- {({ path }) => {
- return (
+ {/* Area background gradient */}
+ <LinearGradient
+ className={cn(
+ s.colorClassName ?? "text-blue-500",
+ seriesClassNames?.find(({ id }) => id === s.id)?.gradient,
+ )}
+ id={`${s.id}-background`}
+ fromOffset="20%"
+ from="currentColor"
+ fromOpacity={0.01}
+ to="currentColor"
+ toOpacity={0.2}
+ x1={0}
+ x2={0}
+ y1={1}
+ />
+
+ {/* Area */}
+ <AreaClosed
+ data={data}
+ x={(d) => xScale(d.date)}
+ y={(d) => yScale(s.valueAccessor(d) ?? 0)}
+ yScale={yScale}
+ >
+ {({ path }) => {
+ return (
+ <motion.path
+ initial={{ d: path(zeroedData) || "", opacity: 0 }}
+ animate={{ d: path(data) || "", opacity: 1 }}
+ fill={`url(#${s.id}-background)`}
+ />
+ );
+ }}
+ </AreaClosed>
+
+ {/* Line */}
+ <Area
+ data={data}
+ x={(d) => xScale(d.date)}
+ y={(d) => yScale(s.valueAccessor(d) ?? 0)}
+ >
+ {({ path }) => (
<motion.path
- initial={{ d: path(zeroedData) || "", opacity: 0 }}
- animate={{ d: path(data) || "", opacity: 1 }}
- fill={`url(#${s.id}-background)`}
+ initial={{ d: path(zeroedData) || "" }}
+ animate={{ d: path(data) || "" }}
+ className={cn(
+ s.colorClassName ?? "text-blue-700",
+ seriesClassNames?.find(({ id }) => id === s.id)?.line,
+ )}
+ stroke="currentColor"
+ strokeOpacity={0.8}
+ strokeWidth={2}
+ fill="transparent"
/>
- );
- }}
- </AreaClosed>
+ )}
+ </Area>
- {/* Line */}
- <Area
- data={data}
- x={(d) => xScale(d.date)}
- y={(d) => yScale(s.valueAccessor(d) ?? 0)}
- >
- {({ path }) => (
- <motion.path
- initial={{ d: path(zeroedData) || "" }}
- animate={{ d: path(data) || "" }}
- className="text-blue-700"
- stroke="currentColor"
- strokeOpacity={0.8}
- strokeWidth={2}
- fill="transparent"
+ {/* Latest value dot */}
+ {!tooltipData && (
+ <Circle
+ cx={xScale(data.at(-1)!.date)}
+ cy={yScale(s.valueAccessor(data.at(-1)!))}
+ r={4}
+ className={cn(
+ s.colorClassName ?? "text-blue-700",
+ seriesClassNames?.find(({ id }) => id === s.id)?.line,
+ )}
+ fill="currentColor"
/>
)}
- </Area>
-
- {/* Latest value dot */}
- {!tooltipData && (
- <Circle
- cx={xScale(data.at(-1)!.date)}
- cy={yScale(s.valueAccessor(data.at(-1)!))}
- r={4}
- className="text-blue-700"
- fill="currentColor"
- />
- )}
- </Fragment>
- ))}
+ </Fragment>
+ ))}
</Group>
);
}
diff --git a/apps/web/ui/charts/time-series-chart.tsx b/apps/web/ui/charts/time-series-chart.tsx
index 2d4415bc00..27c2486be1 100644
--- a/apps/web/ui/charts/time-series-chart.tsx
+++ b/apps/web/ui/charts/time-series-chart.tsx
@@ -154,16 +154,18 @@ function TimeSeriesChartInner<T extends Datum>({
strokeWidth={1.5}
/>
- {series.map((s) => (
- <Circle
- key={s.id}
- cx={xScale(tooltipData.date)}
- cy={yScale(s.valueAccessor(tooltipData))}
- r={4}
- className="text-blue-800"
- fill="currentColor"
- />
- ))}
+ {series
+ .filter(({ isActive }) => isActive)
+ .map((s) => (
+ <Circle
+ key={s.id}
+ cx={xScale(tooltipData.date)}
+ cy={yScale(s.valueAccessor(tooltipData))}
+ r={4}
+ className={s.colorClassName ?? "text-blue-800"}
+ fill="currentColor"
+ />
+ ))}
</>
)}
diff --git a/apps/web/ui/charts/types.ts b/apps/web/ui/charts/types.ts
index 1133976b16..9b75e24cba 100644
--- a/apps/web/ui/charts/types.ts
+++ b/apps/web/ui/charts/types.ts
@@ -19,7 +19,7 @@ export type Series<T extends Datum = any, TValue = number> = {
id: string;
isActive?: boolean;
valueAccessor: AccessorFn<T, TValue>;
- color?: string | AccessorFn<T, string>;
+ colorClassName?: string;
};
export type Data<T extends Datum> = TimeSeriesDatum<T>[];