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.

added counting numbers + loading state for analytics charts

Steven Tey798cb7bda5e6
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx
index 1a0e84224a..6fef2302e4 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx
@@ -5,8 +5,13 @@ import useTags from "@/lib/swr/use-tags";
import useUsers from "@/lib/swr/use-users";
import PlanBadge from "@/ui/projects/plan-badge";
import { Divider } from "@/ui/shared/icons";
-import ProgressBar from "@/ui/shared/progress-bar";
-import { Button, InfoTooltip, NumberTooltip, useRouterStuff } from "@dub/ui";
+import {
+ Button,
+ InfoTooltip,
+ NumberTooltip,
+ ProgressBar,
+ useRouterStuff,
+} from "@dub/ui";
import { HOME_DOMAIN, getFirstAndLastDay, nFormatter } from "@dub/utils";
import va from "@vercel/analytics";
import { useRouter, useSearchParams } from "next/navigation";
diff --git a/apps/web/ui/analytics/clicks-chart.tsx b/apps/web/ui/analytics/clicks-chart.tsx
index cdbb66d684..aaf52c50cb 100644
--- a/apps/web/ui/analytics/clicks-chart.tsx
+++ b/apps/web/ui/analytics/clicks-chart.tsx
@@ -1,3 +1,4 @@
+import { LoadingSpinner } from "@dub/ui";
import { fetcher, nFormatter } from "@dub/utils";
import { useCallback, useContext, useMemo } from "react";
import useSWR from "swr";
@@ -49,8 +50,8 @@ export default function ClicksChart() {
);
return (
- <div className="h-96 w-full">
- {chartData !== null && (
+ <div className="flex h-96 w-full items-center justify-center">
+ {chartData ? (
<TimeSeriesChart
key={queryString}
data={chartData}
@@ -71,6 +72,8 @@ export default function ClicksChart() {
<XAxis tickFormat={formatDate} />
<YAxis showGridLines tickFormat={nFormatter} />
</TimeSeriesChart>
+ ) : (
+ <LoadingSpinner />
)}
</div>
);
diff --git a/apps/web/ui/analytics/clicks.tsx b/apps/web/ui/analytics/clicks.tsx
index f3454b9166..8ee18ed306 100644
--- a/apps/web/ui/analytics/clicks.tsx
+++ b/apps/web/ui/analytics/clicks.tsx
@@ -1,14 +1,8 @@
import { VALID_ANALYTICS_FILTERS } from "@/lib/analytics";
import useTags from "@/lib/swr/use-tags";
import { Chart } from "@/ui/shared/icons";
-import { NumberTooltip, useRouterStuff } from "@dub/ui";
-import {
- COUNTRIES,
- capitalize,
- linkConstructor,
- nFormatter,
- truncate,
-} from "@dub/utils";
+import { CountingNumbers, NumberTooltip, useRouterStuff } from "@dub/ui";
+import { COUNTRIES, capitalize, linkConstructor, truncate } from "@dub/utils";
import { X } from "lucide-react";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
@@ -32,9 +26,12 @@ export default function Clicks() {
<div className="flex items-end space-x-1">
{totalClicks || totalClicks === 0 ? (
<NumberTooltip value={totalClicks}>
- <h1 className="text-3xl font-bold sm:text-4xl">
- {nFormatter(totalClicks)}
- </h1>
+ <CountingNumbers
+ as="h1"
+ className="text-3xl font-bold sm:text-4xl"
+ >
+ {totalClicks}
+ </CountingNumbers>
</NumberTooltip>
) : (
<div className="h-10 w-12 animate-pulse rounded-md bg-gray-200" />
diff --git a/apps/web/ui/links/link-filters.tsx b/apps/web/ui/links/link-filters.tsx
index 48da5ca628..28d8674775 100644
--- a/apps/web/ui/links/link-filters.tsx
+++ b/apps/web/ui/links/link-filters.tsx
@@ -236,7 +236,7 @@ const DomainsFilter = () => {
>
{options.slice(0, showMore ? options.length : 4).map((domain) => (
<div
- key={domain.id}
+ key={domain.slug}
className="group relative flex cursor-pointer items-center space-x-3 rounded-md bg-gray-50 transition-all hover:bg-gray-100"
>
<input
diff --git a/apps/web/ui/modals/link-qr-modal.tsx b/apps/web/ui/modals/link-qr-modal.tsx
index 577a8a4bbc..05caf56a37 100644
--- a/apps/web/ui/modals/link-qr-modal.tsx
+++ b/apps/web/ui/modals/link-qr-modal.tsx
@@ -66,13 +66,6 @@ export function QRCodePicker({
const { logo, plan } = useProject();
const apexDomain = props.url ? getApexDomain(props.url) : null;
- const qrLogoUrl = useMemo(() => {
- if (logo && plan !== "free") return logo;
- return typeof window !== "undefined" && window.location.origin
- ? new URL("/_static/logo.svg", window.location.origin).href
- : "https://dub.co/_static/logo.svg";
- }, [logo, plan]);
-
function download(url: string, extension: string) {
if (!anchorRef.current) return;
anchorRef.current.href = url;
@@ -95,14 +88,15 @@ export function QRCodePicker({
level: "Q", // QR Code error correction level: https://blog.qrstuff.com/general/qr-code-error-correction
...(showLogo && {
imageSettings: {
- src: qrLogoUrl,
+ src:
+ logo && plan !== "free" ? logo : "https://dub.co/_static/logo.svg",
height: 256,
width: 256,
excavate: true,
},
}),
}),
- [props, fgColor, showLogo, qrLogoUrl],
+ [props, fgColor, showLogo],
);
const [copiedImage, setCopiedImage] = useState(false);
@@ -235,7 +229,10 @@ export function QRCodePicker({
...(qrData.imageSettings && {
imageSettings: {
...qrData.imageSettings,
- src: logo || "https://dub.co/_static/logo.svg",
+ src:
+ logo && plan !== "free"
+ ? logo
+ : "https://dub.co/_static/logo.svg",
},
}),
}),
diff --git a/apps/web/ui/shared/counting-numbers.tsx b/apps/web/ui/shared/counting-numbers.tsx
deleted file mode 100644
index 710e948637..0000000000
--- a/apps/web/ui/shared/counting-numbers.tsx
+++ /dev/null
@@ -1,68 +0,0 @@
-"use client";
-
-import { useInView } from "framer-motion";
-import { useEffect, useRef, useState } from "react";
-
-export default function CountingNumbers({
- value,
- className,
- reverse = false,
- start = reverse ? 1000 : 0,
- interval = 10,
- duration = 800,
-}: {
- value: number;
- className: string;
- reverse?: boolean;
- start?: number;
- interval?: number;
- duration?: number;
-}) {
- const [number, setNumber] = useState(start);
- let increment = Math.floor(Math.abs(start - value) / (duration / interval));
- if (increment === 0) {
- increment = 1;
- }
- const ref = useRef(null);
- const isInView = useInView(ref);
-
- useEffect(() => {
- if (isInView) {
- let timer = setInterval(() => {
- if (reverse) {
- if (number > value) {
- setNumber((num) => {
- let newValue = num - increment;
- if (newValue < value) {
- newValue = value;
- if (timer) clearInterval(timer);
- }
- return newValue;
- });
- } else if (timer) {
- clearInterval(timer);
- }
- } else {
- if (number < value) {
- setNumber((num) => {
- let newValue = num + increment;
- if (newValue > value) {
- newValue = value;
- if (timer) clearInterval(timer);
- }
- return newValue;
- });
- } else if (timer) {
- clearInterval(timer);
- }
- }
- }, interval);
- }
- }, [isInView]);
-
- return (
- <p className={className} ref={ref}>
- {Intl.NumberFormat().format(number)}
- </p>
- );
-}
diff --git a/packages/ui/src/counting-numbers.tsx b/packages/ui/src/counting-numbers.tsx
new file mode 100644
index 0000000000..51e48000fe
--- /dev/null
+++ b/packages/ui/src/counting-numbers.tsx
@@ -0,0 +1,69 @@
+import React, {
+ useState,
+ useEffect,
+ ElementType,
+ ReactNode,
+ ForwardedRef,
+} from "react";
+import { Slot } from "@radix-ui/react-slot";
+import { nFormatter } from "@dub/utils";
+
+interface CountingNumbersProps {
+ asChild?: boolean;
+ className?: string;
+ children: ReactNode;
+ duration?: number;
+ as?: ElementType; // Allows defining the component as 'h1', 'h2', 'p', etc.
+}
+
+const CountingNumbers = React.forwardRef<HTMLSpanElement, CountingNumbersProps>(
+ (
+ {
+ asChild,
+ className,
+ children,
+ duration = 250,
+ as: Component = "span",
+ ...props
+ },
+ ref: ForwardedRef<any>,
+ ) => {
+ const [displayValue, setDisplayValue] = useState(Number(children));
+
+ useEffect(() => {
+ if (isNaN(Number(children))) {
+ console.warn("CountingNumbers expects a numeric value as children.");
+ return;
+ }
+ const target = Number(children);
+ const startValue = displayValue;
+ const endValue = target;
+ const startTime = performance.now();
+
+ const tick = () => {
+ const now = performance.now();
+ const progress = Math.min((now - startTime) / duration, 1);
+ const currentValue = startValue + (endValue - startValue) * progress;
+ setDisplayValue(Math.round(currentValue));
+
+ if (progress < 1) {
+ requestAnimationFrame(tick);
+ }
+ };
+
+ requestAnimationFrame(tick);
+ }, [children, duration]); // Depend on children and duration for changes
+
+ const Comp = asChild ? Slot : Component;
+
+ return (
+ <Comp ref={ref} className={className} {...props}>
+ {nFormatter(displayValue)}
+ </Comp>
+ );
+ },
+);
+
+CountingNumbers.displayName = "CountingNumbers";
+
+export { CountingNumbers };
diff --git a/packages/ui/src/index.tsx b/packages/ui/src/index.tsx
index f2bfd93f40..33813e90a2 100644
--- a/packages/ui/src/index.tsx
+++ b/packages/ui/src/index.tsx
@@ -35,7 +35,9 @@ export * from "./content";
// misc
export * from "./blur-image";
export * from "./copy-button";
+export * from "./counting-numbers";
export * from "./icon-menu";
export * from "./inline-snippet";
export * from "./link-preview";
+export * from "./progress-bar";
export * from "./tab-select";
diff --git a/apps/web/ui/shared/progress-bar.tsx b/packages/ui/src/progress-bar.tsx
similarity index 94%
rename from apps/web/ui/shared/progress-bar.tsx
rename to packages/ui/src/progress-bar.tsx
index c90d5a65a1..d47b3de1a5 100644
--- a/apps/web/ui/shared/progress-bar.tsx
+++ b/packages/ui/src/progress-bar.tsx
@@ -3,7 +3,7 @@
import { cn } from "@dub/utils";
import { motion } from "framer-motion";
-export default function ProgressBar({
+export function ProgressBar({
value = 0,
max = 100,
className,