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 mini charts

Tim Wilson7f44551a255e
diff --git a/apps/web/package.json b/apps/web/package.json
index 7146778f30..580395d4c2 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -42,6 +42,7 @@
"@vercel/og": "^0.6.2",
"@vercel/speed-insights": "^1.0.1",
"@visx/axis": "^2.14.0",
+ "@visx/curve": "^3.3.0",
"@visx/event": "^2.6.0",
"@visx/geo": "^2.10.0",
"@visx/gradient": "^3.3.0",
diff --git a/apps/web/ui/analytics/events/events-table.tsx b/apps/web/ui/analytics/events/events-table.tsx
index 748c9f33d1..2b5afa68d3 100644
--- a/apps/web/ui/analytics/events/events-table.tsx
+++ b/apps/web/ui/analytics/events/events-table.tsx
@@ -230,7 +230,7 @@ export default function EventsTable() {
</table>
</div>
) : (
- <div className="flex h-64 h-full w-full items-center justify-center text-sm text-gray-500">
+ <div className="flex h-64 w-full items-center justify-center text-sm text-gray-500">
{error ? "Failed to fetch data" : "No data available"}
</div>
)}
diff --git a/apps/web/ui/analytics/events/events-tabs.tsx b/apps/web/ui/analytics/events/events-tabs.tsx
index 81023a9a0d..efa531cc3a 100644
--- a/apps/web/ui/analytics/events/events-tabs.tsx
+++ b/apps/web/ui/analytics/events/events-tabs.tsx
@@ -1,20 +1,54 @@
-import { useRouterStuff } from "@dub/ui";
-import { capitalize, cn } from "@dub/utils";
-import { useContext } from "react";
+import { editQueryString } from "@/lib/analytics/utils";
+import useWorkspace from "@/lib/swr/use-workspace";
+import { useMediaQuery, useRouterStuff } from "@dub/ui";
+import { capitalize, cn, fetcher } from "@dub/utils";
+import { curveNatural } from "@visx/curve";
+import { LinearGradient } from "@visx/gradient";
+import { Group } from "@visx/group";
+import { ParentSize } from "@visx/responsive";
+import { scaleLinear, scaleUtc } from "@visx/scale";
+import { Area, AreaClosed } from "@visx/shape";
+import { motion } from "framer-motion";
+import { useContext, useMemo } from "react";
+import useSWR from "swr";
import { AnalyticsContext } from "../analytics-provider";
+type TimeseriesData = {
+ start: Date;
+ clicks: number;
+ leads: number;
+ sales: number;
+}[];
+
export default function EventsTabs() {
const { searchParams, queryParams } = useRouterStuff();
+ const { isMobile } = useMediaQuery();
const tab = searchParams.get("tab") || "clicks";
- const { totalEvents } = useContext(AnalyticsContext);
+ const { totalEvents, demo } = useContext(AnalyticsContext);
+
+ const { betaTester } = useWorkspace();
+ const { baseApiPath, queryString, requiresUpgrade } =
+ useContext(AnalyticsContext);
+
+ const { data } = useSWR<TimeseriesData>(
+ `${baseApiPath}?${editQueryString(queryString, {
+ groupBy: "timeseries",
+ event: demo || betaTester ? "composite" : "clicks",
+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
+ })}`,
+ fetcher,
+ {
+ shouldRetryOnError: !requiresUpgrade,
+ },
+ );
return (
- <div className="grid grid-cols-3 gap-4">
+ <div className="grid w-full grid-cols-3 gap-2 overflow-x-auto sm:gap-4">
{["clicks", "leads", "sales"].map((event) => (
<button
className={cn(
- "rounded-xl border border-gray-200 bg-white px-5 py-4 text-left transition-all",
+ "flex justify-between gap-4 rounded-xl border border-gray-200 bg-white px-5 py-4 text-left transition-all",
tab === event && "border-black shadow-[0_0_0_1px_black_inset]",
)}
onClick={() =>
@@ -23,12 +57,154 @@ export default function EventsTabs() {
})
}
>
- <p className="text-sm text-gray-600">{capitalize(event)}</p>
- <p className="mt-2 text-2xl">
- {(totalEvents?.[event] ?? 0).toLocaleString()}
- </p>
+ <div>
+ <p className="text-sm text-gray-600">{capitalize(event)}</p>
+ <p className="mt-2 text-2xl">
+ {(totalEvents?.[event] ?? 0).toLocaleString()}
+ </p>
+ </div>
+ {data?.length && !isMobile && (
+ <div className="relative h-full max-w-[140px] grow">
+ <Chart data={data} event={event} />
+ </div>
+ )}
</button>
))}
</div>
);
}
+
+type ChartProps = { data: TimeseriesData; event: string };
+function Chart(props: ChartProps) {
+ const noData = useMemo(
+ () => props.data.every((d) => (d?.[props.event] ?? 0) === 0),
+ [props.data],
+ );
+
+ return (
+ <ParentSize className="relative">
+ {({ width, height }) => {
+ return (
+ width > 0 &&
+ height > 0 &&
+ !noData && <ChartInner {...{ width, height, ...props }} />
+ );
+ }}
+ </ParentSize>
+ );
+}
+
+const padding = { top: 8, right: 2, bottom: 2, left: 2 };
+
+function ChartInner({
+ data,
+ event,
+ width,
+ height,
+}: ChartProps & { width: number; height: number }) {
+ const chartData = useMemo(
+ () =>
+ data?.map((d) => ({
+ date: new Date(d.start),
+ value: (d?.[event] as number | undefined) ?? 0,
+ })) ?? null,
+ [data, event],
+ );
+
+ const zeroedData = useMemo(
+ () =>
+ chartData.map(({ date }) => ({
+ date,
+ value: 0,
+ })),
+ [chartData],
+ );
+
+ const { yScale, xScale } = useMemo(() => {
+ const values = chartData.map(({ value }) => value);
+ const maxY = Math.max(...values);
+
+ const dateTimes = chartData.map(({ date }) => date.getTime());
+ const minDate = new Date(Math.min(...dateTimes));
+ const maxDate = new Date(Math.max(...dateTimes));
+
+ return {
+ yScale: scaleLinear<number>({
+ domain: [-2, maxY],
+ range: [height - padding.top - padding.bottom, 0],
+ nice: true,
+ clamp: true,
+ }),
+ xScale: scaleUtc<number>({
+ domain: [minDate, maxDate],
+ range: [0, width - padding.left - padding.right * 2],
+ }),
+ };
+ }, [chartData, height, width]);
+
+ return (
+ <svg width={width} height={height} key={chartData.length}>
+ <defs>
+ <LinearGradient
+ id="color-gradient"
+ from="#7D3AEC"
+ to="#DA2778"
+ x1={0}
+ x2={1}
+ />
+ <LinearGradient
+ id="mask-gradient"
+ from="white"
+ to="white"
+ fromOpacity={0.3}
+ toOpacity={0}
+ x1={0}
+ x2={0}
+ y1={0}
+ y2={1}
+ />
+ <mask id="mask" maskContentUnits="objectBoundingBox">
+ <rect width="1" height="1" fill="url(#mask-gradient)" />
+ </mask>
+ </defs>
+ <Group left={padding.left} top={padding.top}>
+ <Area
+ data={chartData}
+ x={({ date }) => xScale(date)}
+ y={({ value }) => yScale(value) ?? 0}
+ curve={curveNatural}
+ >
+ {({ path }) => {
+ return (
+ <motion.path
+ initial={{ d: path(zeroedData) || "", opacity: 0 }}
+ animate={{ d: path(chartData) || "", opacity: 1 }}
+ strokeWidth={1.5}
+ stroke="url(#color-gradient)"
+ />
+ );
+ }}
+ </Area>
+
+ <AreaClosed
+ data={chartData}
+ x={({ date }) => xScale(date)}
+ y={({ value }) => yScale(value) ?? 0}
+ yScale={yScale}
+ curve={curveNatural}
+ >
+ {({ path }) => {
+ return (
+ <motion.path
+ initial={{ d: path(zeroedData) || "", opacity: 0 }}
+ animate={{ d: path(chartData) || "", opacity: 1 }}
+ fill="url(#color-gradient)"
+ mask="url(#mask)"
+ />
+ );
+ }}
+ </AreaClosed>
+ </Group>
+ </svg>
+ );
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 51c06e1901..a866736fe4 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -124,6 +124,9 @@ importers:
'@visx/axis':
specifier: ^2.14.0
version: 2.14.0([email protected])
+ '@visx/curve':
+ specifier: ^3.3.0
+ version: 3.3.0
'@visx/event':
specifier: ^2.6.0
version: 2.6.0
@@ -8488,6 +8491,13 @@ packages:
d3-shape: 1.3.7
dev: false
+ resolution: {integrity: sha512-G1l1rzGWwIs8ka3mBhO/gj8uYK6XdU/3bwRSoiZ+MockMahQFPog0bUkuVgPwwzPSJfsA/E5u53Y/DNesnHQxg==}
+ dependencies:
+ '@types/d3-shape': 1.3.9
+ d3-shape: 1.3.7
+ dev: false
+
resolution: {integrity: sha512-WGp91g82s727g3NAnENF1ppC3ZAlvWg+Y+GG0WFg34NmmOZbvPI/PTOqTqZE3x6B8EUn8NJiMxRjxIMbi+IvRw==}
dependencies: