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 usage stats to sidebar

Tim Wilson89d8f9142a1f
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 f4c6ec04f5..6f4ed8eb4e 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
@@ -59,7 +59,7 @@ export default function WorkspaceBillingClient() {
}, [billingCycleStart]);
return (
- <div className="-mt-5 rounded-lg border border-gray-200 bg-white">
+ <div className="rounded-lg border border-gray-200 bg-white">
<div className="flex flex-col items-start justify-between space-y-4 p-10 xl:flex-row xl:space-y-0">
<div className="flex flex-col space-y-3">
<h2 className="text-xl font-medium">Plan &amp; Usage</h2>
diff --git a/apps/web/ui/layout/main-nav.tsx b/apps/web/ui/layout/main-nav.tsx
index ea494ac341..c68890c57d 100644
--- a/apps/web/ui/layout/main-nav.tsx
+++ b/apps/web/ui/layout/main-nav.tsx
@@ -64,9 +64,9 @@ export function MainNav({
}}
>
{/* Side nav */}
- <nav
+ <div
className={cn(
- "relative min-h-full w-[240px] max-w-full bg-neutral-100 transition-transform md:translate-x-0",
+ "relative flex min-h-full w-[240px] max-w-full flex-col bg-neutral-100 transition-transform md:translate-x-0",
!isOpen && "-translate-x-full",
)}
>
@@ -79,7 +79,7 @@ export function MainNav({
/>
</div>
<SidebarNav toolContent={toolContent} />
- </nav>
+ </div>
</div>
<div className="bg-neutral-100 md:pt-1.5">
<div className="relative min-h-full bg-neutral-100 pt-px md:rounded-tl-2xl md:border md:border-b-0 md:border-r-0 md:border-neutral-200/80 md:bg-white">
diff --git a/apps/web/ui/layout/sidebar/sidebar-nav.tsx b/apps/web/ui/layout/sidebar/sidebar-nav.tsx
index a1fc606155..968672b313 100644
--- a/apps/web/ui/layout/sidebar/sidebar-nav.tsx
+++ b/apps/web/ui/layout/sidebar/sidebar-nav.tsx
@@ -13,6 +13,7 @@ import {
useState,
} from "react";
import { ITEMS, type NavItem as NavItemType } from "./items";
+import { Usage } from "./usage";
import UserDropdown from "./user-dropdown";
import { WorkspaceDropdown } from "./workspace-dropdown";
@@ -32,68 +33,73 @@ export function SidebarNav({ toolContent }: { toolContent?: ReactNode }) {
}, [slug, pathname]);
return (
- <div className="relative p-3 text-gray-500">
- <div className="relative flex items-start justify-between gap-1 pb-3">
- {AREAS.map((area) => (
- <Link
- key={area}
- href={slug ? `/${slug}` : "/"}
- className={cn(
- "rounded-md px-1 outline-none transition-opacity focus-visible:ring-2 focus-visible:ring-black/50",
- area === currentArea
- ? "relative opacity-100"
- : "pointer-events-none absolute opacity-0",
- area === "default" && "mb-1",
- )}
- aria-hidden={area !== currentArea ? true : undefined}
- {...{ inert: area !== currentArea ? "" : undefined }}
- >
- {area === "default" ? (
- <Wordmark className="h-6" />
- ) : (
- <div className="py group -my-1 -ml-1 flex items-center gap-2 py-2 pr-1 text-sm font-medium text-neutral-900">
- <ChevronLeft className="size-4 text-neutral-500 transition-transform duration-100 group-hover:-translate-x-0.5" />
- Settings
- </div>
- )}
- </Link>
- ))}
- <div className="hidden items-center gap-3 md:flex">
- <Suspense fallback={null}>{toolContent}</Suspense>
- <UserDropdown />
+ <>
+ <nav className="relative p-3 text-gray-500">
+ <div className="relative flex items-start justify-between gap-1 pb-3">
+ {AREAS.map((area) => (
+ <Link
+ key={area}
+ href={slug ? `/${slug}` : "/"}
+ className={cn(
+ "rounded-md px-1 outline-none transition-opacity focus-visible:ring-2 focus-visible:ring-black/50",
+ area === currentArea
+ ? "relative opacity-100"
+ : "pointer-events-none absolute opacity-0",
+ area === "default" && "mb-1",
+ )}
+ aria-hidden={area !== currentArea ? true : undefined}
+ {...{ inert: area !== currentArea ? "" : undefined }}
+ >
+ {area === "default" ? (
+ <Wordmark className="h-6" />
+ ) : (
+ <div className="py group -my-1 -ml-1 flex items-center gap-2 py-2 pr-1 text-sm font-medium text-neutral-900">
+ <ChevronLeft className="size-4 text-neutral-500 transition-transform duration-100 group-hover:-translate-x-0.5" />
+ Settings
+ </div>
+ )}
+ </Link>
+ ))}
+ <div className="hidden items-center gap-3 md:flex">
+ <Suspense fallback={null}>{toolContent}</Suspense>
+ <UserDropdown />
+ </div>
</div>
- </div>
- <div className="relative w-full">
- {AREAS.map((area) => (
- <Area
- key={area}
- visible={area === currentArea}
- direction={area === "default" ? "left" : "right"}
- >
- {area === "default" && (
- <div className="pt-2">
- <WorkspaceDropdown />
- </div>
- )}
-
- <div className="flex flex-col gap-4 pt-4">
- {ITEMS[area].map(({ name, items }, idx) => (
- <div key={idx} className="flex flex-col gap-0.5">
- {name && (
- <div className="mb-2 pl-1 text-sm text-neutral-500">
- {name}
- </div>
- )}
- {items({ slug: slug || "", flags }).map((item) => (
- <NavItem key={item.name} item={item} />
- ))}
+ <div className="relative w-full">
+ {AREAS.map((area) => (
+ <Area
+ key={area}
+ visible={area === currentArea}
+ direction={area === "default" ? "left" : "right"}
+ >
+ {area === "default" && (
+ <div className="pt-2">
+ <WorkspaceDropdown />
</div>
- ))}
- </div>
- </Area>
- ))}
+ )}
+
+ <div className="flex flex-col gap-4 pt-4">
+ {ITEMS[area].map(({ name, items }, idx) => (
+ <div key={idx} className="flex flex-col gap-0.5">
+ {name && (
+ <div className="mb-2 pl-1 text-sm text-neutral-500">
+ {name}
+ </div>
+ )}
+ {items({ slug: slug || "", flags }).map((item) => (
+ <NavItem key={item.name} item={item} />
+ ))}
+ </div>
+ ))}
+ </div>
+ </Area>
+ ))}
+ </div>
+ </nav>
+ <div className="relative mt-6 flex grow flex-col justify-end">
+ <Usage />
</div>
- </div>
+ </>
);
}
diff --git a/apps/web/ui/layout/sidebar/usage.tsx b/apps/web/ui/layout/sidebar/usage.tsx
new file mode 100644
index 0000000000..d79bb2d978
--- /dev/null
+++ b/apps/web/ui/layout/sidebar/usage.tsx
@@ -0,0 +1,181 @@
+"use client";
+
+import useWorkspace from "@/lib/swr/use-workspace";
+import { AnimatedSizeContainer, Icon } from "@dub/ui";
+import { buttonVariants } from "@dub/ui/src/button";
+import { CursorRays, Hyperlink } from "@dub/ui/src/icons";
+import { cn, getFirstAndLastDay, nFormatter } from "@dub/utils";
+import { ChevronRight } from "lucide-react";
+import Link from "next/link";
+import { useParams } from "next/navigation";
+import { CSSProperties, useMemo } from "react";
+
+export function Usage() {
+ const { slug } = useParams() as { slug?: string };
+
+ return slug ? <UsageInner /> : null;
+}
+
+function UsageInner() {
+ const {
+ usage,
+ usageLimit,
+ linksUsage,
+ linksLimit,
+ billingCycleStart,
+ plan,
+ slug,
+ loading,
+ } = useWorkspace();
+
+ const [billingEnd] = useMemo(() => {
+ if (billingCycleStart) {
+ const { lastDay } = getFirstAndLastDay(billingCycleStart);
+ const end = lastDay.toLocaleDateString("en-us", {
+ month: "short",
+ day: "numeric",
+ year: "numeric",
+ });
+ return [end];
+ }
+ return [];
+ }, [billingCycleStart]);
+
+ // Warn the user if they're >= 90% of any limit
+ const warnings = useMemo(
+ () =>
+ [
+ [usage, usageLimit],
+ [linksUsage, linksLimit],
+ ].map(
+ ([usage, limit]) =>
+ usage !== undefined &&
+ limit !== undefined &&
+ usage / Math.max(0, usage, limit) >= 0.9,
+ ),
+ [usage, usageLimit, linksUsage, linksLimit],
+ );
+
+ const warning = warnings.some((w) => w);
+
+ return loading || usage !== undefined ? (
+ <div className="border-t border-neutral-300/80 p-3">
+ <Link
+ className="group flex items-center gap-0.5 text-sm font-normal text-neutral-500 transition-colors hover:text-neutral-700"
+ href={`/${slug}/settings/billing`}
+ >
+ Usage
+ <ChevronRight className="size-3 text-neutral-400 transition-[color,transform] group-hover:translate-x-0.5 group-hover:text-neutral-500" />
+ </Link>
+
+ <div className="mt-4 flex flex-col gap-4">
+ <UsageRow
+ icon={Hyperlink}
+ label="Links"
+ usage={linksUsage}
+ limit={linksLimit}
+ warning={warnings[1]}
+ />
+ <UsageRow
+ icon={CursorRays}
+ label="Events"
+ usage={usage}
+ limit={usageLimit}
+ warning={warnings[0]}
+ />
+ </div>
+
+ <div className="mt-3">
+ {billingEnd ? (
+ <p className="text-xs text-neutral-900/40">
+ Usage will reset {billingEnd}
+ </p>
+ ) : loading ? (
+ <div className="h-4 w-2/3 animate-pulse rounded-md bg-neutral-500/10" />
+ ) : (
+ <div className="h-4" />
+ )}
+ </div>
+
+ <AnimatedSizeContainer height>
+ {warning && (
+ <div className="pt-4">
+ <Link
+ href={`/${slug}/upgrade`}
+ className={cn(
+ buttonVariants(),
+ "flex h-9 items-center justify-center rounded-md border px-4 text-sm",
+ )}
+ >
+ {plan === "free" ? "Get Dub Pro" : "Upgrade plan"}
+ </Link>
+ </div>
+ )}
+ </AnimatedSizeContainer>
+ </div>
+ ) : null;
+}
+
+function UsageRow({
+ icon: Icon,
+ label,
+ usage,
+ limit,
+ warning,
+}: {
+ icon: Icon;
+ label: string;
+ usage?: number;
+ limit?: number;
+ warning: boolean;
+}) {
+ const loading = usage === undefined || limit === undefined;
+
+ return (
+ <div>
+ <div className="flex items-center justify-between gap-2">
+ <div className="flex items-center gap-2">
+ <Icon className="size-3.5 text-neutral-600" />
+ <span className="text-xs font-medium text-neutral-700">{label}</span>
+ </div>
+ {!loading ? (
+ <span className="text-xs font-medium text-neutral-600">
+ {formatNumber(usage)} of {formatNumber(limit)}
+ </span>
+ ) : (
+ <div className="h-4 w-16 animate-pulse rounded-md bg-neutral-500/10" />
+ )}
+ </div>
+ <div className="mt-1.5">
+ <div
+ className={cn(
+ "h-0.5 w-full overflow-hidden rounded-full bg-neutral-900/10 transition-colors",
+ loading && "bg-neutral-900/5",
+ )}
+ >
+ {!loading && (
+ <div
+ className="animate-slide-right-fade size-full"
+ style={{ "--offset": "-100%" } as CSSProperties}
+ >
+ <div
+ className={cn(
+ "size-full rounded-full bg-gradient-to-r from-transparent to-blue-600",
+ warning && "to-rose-500",
+ )}
+ style={{
+ transform: `translateX(-${100 - (usage / Math.max(0, usage, limit)) * 100}%)`,
+ }}
+ />
+ </div>
+ )}
+ </div>
+ </div>
+ </div>
+ );
+}
+
+const formatNumber = (value: number) =>
+ isFinite(value)
+ ? nFormatter(value, { full: value !== undefined && value < 9999 })
+ : "∞";
diff --git a/apps/web/ui/modals/link-builder/index.tsx b/apps/web/ui/modals/link-builder/index.tsx
index c89dda6c27..677ee6b429 100644
--- a/apps/web/ui/modals/link-builder/index.tsx
+++ b/apps/web/ui/modals/link-builder/index.tsx
@@ -260,12 +260,15 @@ function LinkBuilderInner({
});
if (res.status === 200) {
- await mutate(
- (key) =>
- typeof key === "string" && key.startsWith("/api/links"),
- undefined,
- { revalidate: true },
- );
+ await Promise.all([
+ mutate(
+ (key) =>
+ typeof key === "string" && key.startsWith("/api/links"),
+ undefined,
+ { revalidate: true },
+ ),
+ mutate(`/api/workspaces/${slug}`),
+ ]);
const data = await res.json();
posthog.capture(
props ? "link_updated" : "link_created",