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 number tooltip to fix #322

Steven Tey2e395e37f105
diff --git a/app/dub.sh/@modal/(.)stats/[key]/default.tsx b/app/dub.sh/_modal/(.)stats/[key]/default.tsx
similarity index 100%
rename from app/dub.sh/@modal/(.)stats/[key]/default.tsx
rename to app/dub.sh/_modal/(.)stats/[key]/default.tsx
diff --git a/app/dub.sh/@modal/(.)stats/[key]/page.tsx b/app/dub.sh/_modal/(.)stats/[key]/page.tsx
similarity index 100%
rename from app/dub.sh/@modal/(.)stats/[key]/page.tsx
rename to app/dub.sh/_modal/(.)stats/[key]/page.tsx
diff --git a/app/dub.sh/@modal/default.tsx b/app/dub.sh/_modal/default.tsx
similarity index 100%
rename from app/dub.sh/@modal/default.tsx
rename to app/dub.sh/_modal/default.tsx
diff --git a/app/dub.sh/layout.tsx b/app/dub.sh/layout.tsx
index a0b1f5cdab..a7aa86fb73 100644
--- a/app/dub.sh/layout.tsx
+++ b/app/dub.sh/layout.tsx
@@ -1,5 +1,4 @@
import { ReactNode } from "react";
-import MarketingProviders from "./providers";
import Nav from "#/ui/home/nav";
import Footer from "#/ui/home/footer";
import Background from "#/ui/home/background";
@@ -13,14 +12,12 @@ export default function MarketingLayout({
modal: ReactNode;
}) {
return (
- <MarketingProviders modal={modal}>
- <div className="flex min-h-screen flex-col justify-between">
- <MobileNav />
- <Nav />
- {children}
- <Footer />
- <Background />
- </div>
- </MarketingProviders>
+ <div className="flex min-h-screen flex-col justify-between">
+ <MobileNav />
+ <Nav />
+ {children}
+ <Footer />
+ <Background />
+ </div>
);
}
diff --git a/app/dub.sh/providers.tsx b/app/dub.sh/providers.tsx
deleted file mode 100644
index ab3089b0ff..0000000000
--- a/app/dub.sh/providers.tsx
+++ /dev/null
@@ -1,21 +0,0 @@
-"use client";
-
-import { useSelectedLayoutSegment } from "next/navigation";
-import { ReactNode } from "react";
-
-export default function MarketingProviders({
- modal,
- children,
-}: {
- modal: ReactNode;
- children: ReactNode;
-}) {
- const segment = useSelectedLayoutSegment();
- return (
- <>
- {/* Only show stats modal if it's on the home page */}
- {!segment && modal}
- {children}
- </>
- );
-}
diff --git a/app/ui/home/link-card.tsx b/app/ui/home/link-card.tsx
index e3b55b2124..df7bc0074f 100644
--- a/app/ui/home/link-card.tsx
+++ b/app/ui/home/link-card.tsx
@@ -22,6 +22,7 @@ import {
linkConstructor,
nFormatter,
} from "#/lib/utils";
+import Number from "#/ui/number";
export default function LinkCard({
_key: key,
@@ -170,20 +171,22 @@ export default function LinkCard({
<span className="sr-only">Copy</span>
<QrCode className="h-4 w-4 text-gray-700 transition-all group-hover:text-blue-800" />
</button>
- <Link
- href={`/stats/${encodeURI(key)}`}
- className="flex items-center space-x-1 rounded-md bg-gray-100 px-2 py-0.5 text-gray-700 transition-all duration-75 hover:scale-105 active:scale-95"
- >
- <Chart className="h-4 w-4" />
- <p className="text-sm">
- {!clicks && clicks !== 0 ? (
- <LoadingDots color="#71717A" />
- ) : (
- nFormatter(clicks)
- )}
- <span className="ml-1 hidden sm:inline-block">clicks</span>
- </p>
- </Link>
+ <Number value={clicks}>
+ <Link
+ href={`/stats/${encodeURI(key)}`}
+ className="flex items-center space-x-1 rounded-md bg-gray-100 px-2 py-0.5 text-gray-700 transition-all duration-75 hover:scale-105 active:scale-95"
+ >
+ <Chart className="h-4 w-4" />
+ <p className="text-sm">
+ {!clicks && clicks !== 0 ? (
+ <LoadingDots color="#71717A" />
+ ) : (
+ nFormatter(clicks)
+ )}
+ <span className="ml-1 hidden sm:inline-block">clicks</span>
+ </p>
+ </Link>
+ </Number>
</div>
<a
href={url}
diff --git a/app/ui/home/pricing.tsx b/app/ui/home/pricing.tsx
index 237009d6d8..5f1ed3bbac 100644
--- a/app/ui/home/pricing.tsx
+++ b/app/ui/home/pricing.tsx
@@ -247,7 +247,7 @@ const Pricing = () => {
</h3>
<p className="text-gray-500">{tagline}</p>
<p className="my-5 font-display text-6xl font-semibold">
- ${period === "yearly" ? nFormatter(price / 12, 1) : price}
+ ${period === "yearly" ? nFormatter(price / 12) : price}
</p>
<p className="text-gray-500">
per {period === "yearly" ? "month, billed annually" : "month"}
diff --git a/app/ui/number.tsx b/app/ui/number.tsx
new file mode 100644
index 0000000000..58c7150438
--- /dev/null
+++ b/app/ui/number.tsx
@@ -0,0 +1,24 @@
+"use client";
+
+import { ReactNode } from "react";
+import { nFormatter } from "#/lib/utils";
+import Tooltip from "#/ui/tooltip";
+
+export default function Number({
+ value,
+ unit = "clicks",
+ children,
+}: {
+ value?: number | null;
+ unit?: string;
+ children: ReactNode;
+}) {
+ if (!value || value < 1000) {
+ return children;
+ }
+ return (
+ <Tooltip content={`${nFormatter(value, { full: true })} ${unit}`}>
+ {children}
+ </Tooltip>
+ );
+}
diff --git a/app/ui/stats/bar-list.tsx b/app/ui/stats/bar-list.tsx
index ac41fe7069..d1a5d1add8 100644
--- a/app/ui/stats/bar-list.tsx
+++ b/app/ui/stats/bar-list.tsx
@@ -5,6 +5,7 @@ import { ReactNode, useMemo, useState } from "react";
import { motion } from "framer-motion";
import { Search } from "lucide-react";
import Fuse from "fuse.js";
+import Number from "#/ui/number";
export default function BarList({
tab,
@@ -61,7 +62,9 @@ export default function BarList({
animate={{ transform: "scaleX(1)" }}
/>
</div>
- <p className="z-10 text-sm text-gray-600">{nFormatter(clicks)}</p>
+ <Number value={clicks}>
+ <p className="z-10 text-sm text-gray-600">{nFormatter(clicks)}</p>
+ </Number>
</div>
))}
</div>
diff --git a/app/ui/stats/clicks.tsx b/app/ui/stats/clicks.tsx
index cd5b6d9190..89be642c75 100644
--- a/app/ui/stats/clicks.tsx
+++ b/app/ui/stats/clicks.tsx
@@ -5,6 +5,7 @@ import useSWR from "swr";
import { fetcher } from "#/lib/utils";
import { StatsContext } from ".";
import { useContext } from "react";
+import Number from "#/ui/number";
export default function Clicks() {
const { endpoint, queryString } = useContext(StatsContext);
@@ -19,9 +20,11 @@ export default function Clicks() {
<div className="mb-5 text-left">
<div className="flex items-end space-x-1">
{totalClicks || totalClicks === 0 ? (
- <h1 className="text-3xl font-bold sm:text-4xl">
- {nFormatter(totalClicks)}
- </h1>
+ <Number value={totalClicks}>
+ <h1 className="text-3xl font-bold sm:text-4xl">
+ {nFormatter(totalClicks)}
+ </h1>
+ </Number>
) : (
<div className="h-10 w-12 animate-pulse rounded-md bg-gray-200" />
)}
diff --git a/components/app/domains/domain-card.tsx b/components/app/domains/domain-card.tsx
index 390fd7cbb5..95f4160641 100644
--- a/components/app/domains/domain-card.tsx
+++ b/components/app/domains/domain-card.tsx
@@ -15,6 +15,7 @@ import DomainConfiguration from "./domain-configuration";
import Link from "next/link";
import punycode from "punycode/";
import Button from "#/ui/button";
+import Number from "#/ui/number";
export default function DomainCard({ props }: { props: DomainProps }) {
const router = useRouter();
@@ -60,20 +61,22 @@ export default function DomainCard({ props }: { props: DomainProps }) {
</p>
<ExternalLink className="h-5 w-5" />
</a>
- <Link
- href={`/${slug}/${domain}`}
- className="flex items-center space-x-1 rounded-md bg-gray-100 px-2 py-0.5 transition-all duration-75 hover:scale-105 active:scale-100"
- >
- <Chart className="h-4 w-4" />
- <p className="text-sm">
- {!clicks && clicks !== 0 ? (
- <LoadingDots color="#71717A" />
- ) : (
- nFormatter(clicks)
- )}
- <span className="ml-1 hidden sm:inline-block">clicks</span>
- </p>
- </Link>
+ <Number value={clicks}>
+ <Link
+ href={`/${slug}/${domain}`}
+ className="flex items-center space-x-1 rounded-md bg-gray-100 px-2 py-0.5 transition-all duration-75 hover:scale-105 active:scale-100"
+ >
+ <Chart className="h-4 w-4" />
+ <p className="text-sm">
+ {!clicks && clicks !== 0 ? (
+ <LoadingDots color="#71717A" />
+ ) : (
+ nFormatter(clicks)
+ )}
+ <span className="ml-1 hidden sm:inline-block">clicks</span>
+ </p>
+ </Link>
+ </Number>
{primary && (
<span className="rounded-full bg-blue-500 px-3 py-0.5 text-xs text-white">
Primary Domain
diff --git a/components/app/links/link-card.tsx b/components/app/links/link-card.tsx
index a4caa371de..e6e3618093 100644
--- a/components/app/links/link-card.tsx
+++ b/components/app/links/link-card.tsx
@@ -31,6 +31,7 @@ import { GOOGLE_FAVICON_URL, HOME_DOMAIN } from "#/lib/constants";
import useTags from "#/lib/swr/use-tags";
import TagBadge from "@/components/app/links/tag-badge";
import { ModalContext } from "#/ui/modal-provider";
+import Number from "#/ui/number";
export default function LinkCard({ props }: { props: LinkProps }) {
const { key, domain, url, rewrite, createdAt, archived, tagId } = props;
@@ -285,7 +286,7 @@ export default function LinkCard({ props }: { props: LinkProps }) {
href={url}
target="_blank"
rel="noopener noreferrer"
- className="max-w-[180px] truncate text-sm font-medium text-gray-700 underline-offset-2 hover:underline sm:max-w-[300px] md:max-w-[360px] xl:max-w-[500px]"
+ className="max-w-[180px] truncate text-sm font-medium text-gray-700 underline-offset-2 hover:underline sm:max-w-[300px] md:max-w-[360px] xl:max-w-[440px]"
>
{url}
</a>
@@ -294,21 +295,23 @@ export default function LinkCard({ props }: { props: LinkProps }) {
</div>
<div className="flex items-center space-x-2">
- <Link
- onClick={(e) => {
- e.stopPropagation();
- }}
- href={`/${
- slug ? `${slug}/${domain}` : "links"
- }/${encodeURIComponent(key)}`}
- className="flex items-center space-x-1 rounded-md bg-gray-100 px-2 py-0.5 transition-all duration-75 hover:scale-105 active:scale-100"
- >
- <Chart className="h-4 w-4" />
- <p className="whitespace-nowrap text-sm text-gray-500">
- {nFormatter(clicks)}
- <span className="ml-1 hidden sm:inline-block">clicks</span>
- </p>
- </Link>
+ <Number value={clicks}>
+ <Link
+ onClick={(e) => {
+ e.stopPropagation();
+ }}
+ href={`/${
+ slug ? `${slug}/${domain}` : "links"
+ }/${encodeURIComponent(key)}`}
+ className="flex items-center space-x-1 rounded-md bg-gray-100 px-2 py-0.5 transition-all duration-75 hover:scale-105 active:scale-100"
+ >
+ <Chart className="h-4 w-4" />
+ <p className="whitespace-nowrap text-sm text-gray-500">
+ {nFormatter(clicks)}
+ <span className="ml-1 hidden sm:inline-block">clicks</span>
+ </p>
+ </Link>
+ </Number>
<Popover
content={
<div className="grid w-full gap-px p-2 sm:w-48">
diff --git a/components/app/links/link-filters.tsx b/components/app/links/link-filters.tsx
index 548e7e5358..3a6e6f797d 100644
--- a/components/app/links/link-filters.tsx
+++ b/components/app/links/link-filters.tsx
@@ -28,6 +28,7 @@ import { ThreeDots } from "@/components/shared/icons";
import Popover from "#/ui/popover";
import IconMenu from "@/components/shared/icon-menu";
import { mutate } from "swr";
+import Number from "#/ui/number";
export default function LinkFilters() {
const { primaryDomain } = useDomains();
@@ -248,7 +249,9 @@ const DomainsFilter = ({ domains, primaryDomain }) => {
className="flex w-full cursor-pointer justify-between px-3 py-2 pl-0 text-sm font-medium text-gray-700"
>
<p>{punycode.toUnicode(value || "")}</p>
- <p className="text-gray-500">{nFormatter(count)}</p>
+ <Number value={count} unit="links">
+ <p className="text-gray-500">{nFormatter(count)}</p>
+ </Number>
</label>
</div>
))}
diff --git a/components/app/links/link-pagination.tsx b/components/app/links/link-pagination.tsx
index a7dd352c56..7c43b95802 100644
--- a/components/app/links/link-pagination.tsx
+++ b/components/app/links/link-pagination.tsx
@@ -1,6 +1,7 @@
import { PAGINATION_LIMIT } from "#/lib/constants";
import useLinksCount from "#/lib/swr/use-links-count";
-import { cn, nFormatter, setQueryString } from "#/lib/utils";
+import { nFormatter, setQueryString } from "#/lib/utils";
+import Number from "#/ui/number";
import { ChevronLeft, ChevronRight } from "lucide-react";
import { useRouter } from "next/router";
import { useMemo } from "react";
@@ -89,7 +90,11 @@ export default function LinkPagination() {
</div>
<p className="text-sm text-gray-500">
Showing {(currentPage - 1) * PAGINATION_LIMIT + 1} -{" "}
- {currentPage * PAGINATION_LIMIT} of {nFormatter(count)} links
+ {currentPage * PAGINATION_LIMIT} of{" "}
+ <Number value={count} unit="links">
+ <span>{nFormatter(count)}</span>
+ </Number>{" "}
+ links
</p>
</div>
);
diff --git a/components/app/projects/project-card.tsx b/components/app/projects/project-card.tsx
index 5bf1532fa1..0b26284674 100644
--- a/components/app/projects/project-card.tsx
+++ b/components/app/projects/project-card.tsx
@@ -9,6 +9,7 @@ import { BarChart2, Globe, Link2 } from "lucide-react";
import PlanBadge from "./plan-badge";
import { GOOGLE_FAVICON_URL } from "#/lib/constants";
import Badge from "#/ui/badge";
+import Number from "#/ui/number";
export default function ProjectCard({
id,
@@ -90,18 +91,22 @@ export default function ProjectCard({
<div className="flex items-center space-x-2 text-gray-500">
<Link2 className="h-4 w-4" />
{count || count === 0 ? (
- <h2 className="whitespace-nowrap text-sm">
- {nFormatter(count)} link{count != 1 && "s"}
- </h2>
+ <Number value={count} unit="links">
+ <h2 className="whitespace-nowrap text-sm">
+ {nFormatter(count)} link{count != 1 && "s"}
+ </h2>
+ </Number>
) : (
<div className="h-4 w-8 animate-pulse rounded-md bg-gray-200" />
)}
</div>
<div className="flex items-center space-x-2 text-gray-500">
<BarChart2 className="h-4 w-4" />
- <h2 className="whitespace-nowrap text-sm">
- {nFormatter(usage)} click{usage != 1 && "s"}
- </h2>
+ <Number value={usage}>
+ <h2 className="whitespace-nowrap text-sm">
+ {nFormatter(usage)} click{usage != 1 && "s"}
+ </h2>
+ </Number>
</div>
</div>
</Link>
diff --git a/components/stats/clicks.tsx b/components/stats/clicks.tsx
index d74570185a..d4ac2973f4 100644
--- a/components/stats/clicks.tsx
+++ b/components/stats/clicks.tsx
@@ -6,6 +6,7 @@ import useSWR from "swr";
import { fetcher } from "#/lib/utils";
import { useContext } from "react";
import { StatsContext } from ".";
+import Number from "#/ui/number";
export default function Clicks() {
const router = useRouter();
@@ -21,9 +22,11 @@ export default function Clicks() {
<div className="mb-5 text-left">
<div className="flex items-end space-x-1">
{totalClicks || totalClicks === 0 ? (
- <h1 className="text-3xl font-bold sm:text-4xl">
- {nFormatter(totalClicks)}
- </h1>
+ <Number value={totalClicks}>
+ <h1 className="text-3xl font-bold sm:text-4xl">
+ {nFormatter(totalClicks)}
+ </h1>
+ </Number>
) : (
<div className="h-10 w-12 animate-pulse rounded-md bg-gray-200" />
)}
diff --git a/emails/usage-exceeded.tsx b/emails/usage-exceeded.tsx
index bbfbec1793..1f931cc691 100644
--- a/emails/usage-exceeded.tsx
+++ b/emails/usage-exceeded.tsx
@@ -57,7 +57,8 @@ export default function UsageExceeded({
Your Dub project, <strong> {name} </strong> has exceeded the
<strong> {capitalize(plan)} Plan </strong>
limit of <strong>{nFormatter(usageLimit)} link clicks</strong>.
- You have used <strong>{nFormatter(usage, 2)} link clicks</strong>{" "}
+ You have used{" "}
+ <strong>{nFormatter(usage, { digits: 2 })} link clicks</strong>{" "}
across all your projects in your current billing cycle.
</Text>
<Text className="text-sm leading-6 text-black">
diff --git a/lib/utils.ts b/lib/utils.ts
index 319167d514..7b93a66b4f 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -78,8 +78,16 @@ export async function fetcher<JSON = any>(
return res.json();
}
-export function nFormatter(num?: number, digits?: number) {
+export function nFormatter(
+ num?: number,
+ opts: { digits?: number; full?: boolean } = {
+ digits: 1,
+ },
+) {
if (!num) return "0";
+ if (opts.full) {
+ return Intl.NumberFormat("en-US").format(num);
+ }
const lookup = [
{ value: 1, symbol: "" },
{ value: 1e3, symbol: "K" },
@@ -97,7 +105,7 @@ export function nFormatter(num?: number, digits?: number) {
return num >= item.value;
});
return item
- ? (num / item.value).toFixed(digits || 1).replace(rx, "$1") + item.symbol
+ ? (num / item.value).toFixed(opts.digits).replace(rx, "$1") + item.symbol
: "0";
}
diff --git a/pages/api/edge/links/[key]/stats/[endpoint].ts b/pages/api/edge/links/[key]/stats/[endpoint].ts
index 07879ee273..bc2455ae6d 100644
--- a/pages/api/edge/links/[key]/stats/[endpoint].ts
+++ b/pages/api/edge/links/[key]/stats/[endpoint].ts
@@ -28,7 +28,7 @@ export default async function handler(req: NextRequest) {
}
const ip = ipAddress(req) || LOCALHOST_IP;
const { success } = await ratelimit(
- 5,
+ 10,
key === "github" ? "1 d" : "10 s",
).limit(`${ip}:${domain}:${key}:${endpoint}`);
diff --git a/pages/app/[slug]/settings/billing.tsx b/pages/app/[slug]/settings/billing.tsx
index 26142f31e9..da63bb506e 100644
--- a/pages/app/[slug]/settings/billing.tsx
+++ b/pages/app/[slug]/settings/billing.tsx
@@ -3,7 +3,6 @@ import { useRouter } from "next/router";
import { useContext, useEffect, useMemo, useState } from "react";
import { motion } from "framer-motion";
import { Infinity, Divider } from "@/components/shared/icons";
-import { LoadingDots } from "#/ui/icons";
import { InfoTooltip } from "#/ui/tooltip";
import { fetcher, getFirstAndLastDay, nFormatter } from "#/lib/utils";
import useProject from "#/lib/swr/use-project";
@@ -13,6 +12,7 @@ import useSWR, { mutate } from "swr";
import va from "@vercel/analytics";
import { ModalContext } from "#/ui/modal-provider";
import Button from "#/ui/button";
+import Number from "#/ui/number";
export default function ProjectBilling() {
const router = useRouter();
@@ -92,9 +92,11 @@ export default function ProjectBilling() {
{plan === "enterprise" ? (
<div className="mt-4 flex items-center">
{usage || usage === 0 ? (
- <p className="text-2xl font-semibold text-black">
- {nFormatter(usage)}
- </p>
+ <Number value={usage}>
+ <p className="text-2xl font-semibold text-black">
+ {nFormatter(usage)}
+ </p>
+ </Number>
) : (
<div className="h-8 w-8 animate-pulse rounded-md bg-gray-200" />
)}
@@ -105,7 +107,10 @@ export default function ProjectBilling() {
<div className="mt-2 flex flex-col space-y-2">
{usage !== undefined && usageLimit ? (
<p className="text-sm text-gray-600">
- {nFormatter(usage)} / {nFormatter(usageLimit)} clicks (
+ <Number value={usage}>
+ <span>{nFormatter(usage)} </span>
+ </Number>
+ / {nFormatter(usageLimit)} clicks (
{((usage / usageLimit) * 100).toFixed(1)}%)
</p>
) : (
@@ -138,9 +143,11 @@ export default function ProjectBilling() {
</div>
<div className="mt-4 flex items-center">
{links || links === 0 ? (
- <p className="text-2xl font-semibold text-black">
- {nFormatter(links)}
- </p>
+ <Number value={links} unit="links">
+ <p className="text-2xl font-semibold text-black">
+ {nFormatter(links)}
+ </p>
+ </Number>
) : (
<div className="h-8 w-8 animate-pulse rounded-md bg-gray-200" />
)}