The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
"use client";
import { getPlanCapabilities } from "@/lib/plan-capabilities";
import useWorkspace from "@/lib/swr/use-workspace";
import { useKeyboardShortcut, useRouterStuff } from "@dub/ui";
import {
Bell,
BookOpen,
ConnectedDots,
CubeSettings,
DiamondTurnRight,
Folder,
Gear2,
Globe,
Key,
MarketingTarget,
Receipt2,
ShieldCheck,
StackY3,
Tag,
Users6,
Webhook,
} from "@dub/ui/icons";
import { isWorkspaceBillingTrialActive } from "@dub/utils";
import { Session } from "next-auth";
import { useSession } from "next-auth/react";
import Link from "next/link";
import { useParams, usePathname } from "next/navigation";
import { ReactNode, useMemo } from "react";
import { Compass } from "./icons/compass";
import { CursorRays } from "./icons/cursor-rays";
import { Hyperlink } from "./icons/hyperlink";
import { LinesY } from "./icons/lines-y";
import { User } from "./icons/user";
import { SidebarNav, SidebarNavAreas, SidebarNavGroups } from "./sidebar-nav";
import { SidebarUsage } from "./sidebar-usage";
import { WorkspaceDropdown } from "./workspace-dropdown";
type SidebarNavData = {
slug: string;
pathname: string;
queryString: string;
session?: Session | null;
};
const NAV_GROUPS: SidebarNavGroups<SidebarNavData> = ({ slug, pathname }) => [
{
name: "Short Links",
description:
"Create, organize, and measure the performance of your short links.",
learnMoreHref: "https://dub.co/links",
icon: Compass,
href: slug ? `/${slug}/links` : "/links",
active: pathname.startsWith(`/${slug}/links`),
},
];
const NAV_AREAS: SidebarNavAreas<SidebarNavData> = {
// short links
links: ({ slug, pathname, queryString }) => ({
title: "Short Links",
direction: "left",
content: [
{
items: [
{
name: "Links",
icon: Hyperlink,
href: `/${slug}/links${pathname === `/${slug}/links` ? "" : queryString}`,
isActive: (pathname: string, href: string) => {
const basePath = href.split("?")[0];
// Exact match for the base links page
if (pathname === basePath) return true;
// Check if it's a link detail page (path segment after base contains a dot for domain)
if (pathname.startsWith(basePath + "/")) {
const nextSegment = pathname
.slice(basePath.length + 1)
.split("/")[0];
return nextSegment.includes(".");
}
return false;
},
},
{
name: "Domains",
icon: Globe,
href: `/${slug}/links/domains`,
},
],
},
{
name: "Insights",
items: [
{
name: "Analytics",
icon: LinesY,
href: `/${slug}/links/analytics${pathname === `/${slug}/links/analytics` ? "" : queryString}`,
},
{
name: "Events",
icon: CursorRays,
href: `/${slug}/links/events${pathname === `/${slug}/links/events` ? "" : queryString}`,
},
{
name: "Customers",
icon: User,
href: `/${slug}/links/customers`,
},
],
},
{
name: "Library",
items: [
{
name: "Folders",
icon: Folder,
href: `/${slug}/links/folders`,
},
{
name: "Tags",
icon: Tag,
href: `/${slug}/links/tags`,
},
{
name: "UTM Templates",
icon: DiamondTurnRight,
href: `/${slug}/links/utm`,
},
],
},
],
}),
// Workspace settings
workspaceSettings: ({ slug }) => ({
title: "Settings",
backHref: `/${slug}`,
content: [
{
name: "Workspace",
items: [
{
name: "General",
icon: Gear2,
href: `/${slug}/settings`,
exact: true,
},
{
name: "Billing",
icon: Receipt2,
href: `/${slug}/settings/billing`,
},
{
name: "Domains",
icon: Globe,
href: `/${slug}/settings/domains`,
},
{
name: "Members",
icon: Users6,
href: `/${slug}/settings/members`,
},
{
name: "Integrations",
icon: ConnectedDots,
href: `/${slug}/settings/integrations`,
},
{
name: "Security",
icon: ShieldCheck,
href: `/${slug}/settings/security`,
},
],
},
{
name: "Developer",
items: [
{
name: "API Keys",
icon: Key,
href: `/${slug}/settings/tokens`,
},
{
name: "Logs",
icon: StackY3,
href: `/${slug}/settings/logs`,
},
{
name: "Tracking",
icon: MarketingTarget,
href: `/${slug}/settings/tracking`,
},
{
name: "Webhooks",
icon: Webhook,
href: `/${slug}/settings/webhooks`,
},
{
name: "OAuth Apps",
icon: CubeSettings,
href: `/${slug}/settings/oauth-apps`,
},
],
},
{
name: "Account",
items: [
{
name: "Notifications",
icon: Bell,
href: `/${slug}/settings/notifications`,
},
],
},
],
}),
// User settings
userSettings: ({ slug }) => ({
title: "Settings",
backHref: `/${slug}`,
hideSwitcherIcons: true,
content: [
{
name: "Account",
items: [
{
name: "General",
icon: Gear2,
href: "/account/settings",
exact: true,
},
{
name: "Security",
icon: ShieldCheck,
href: "/account/settings/security",
},
{
name: "Notifications",
icon: Bell,
href: "/settings/notifications",
arrow: true,
},
],
},
],
}),
};
export function AppSidebarNav({
toolContent,
newsContent,
}: {
toolContent?: ReactNode;
newsContent?: ReactNode;
}) {
const { slug } = useParams() as { slug?: string };
const pathname = usePathname();
const { router, getQueryString } = useRouterStuff();
const { data: session } = useSession();
const { plan, trialEndsAt } = useWorkspace();
const currentArea = useMemo(() => {
return pathname.startsWith("/account/settings")
? "userSettings"
: pathname.startsWith(`/${slug}/settings`)
? "workspaceSettings"
: "links";
}, [slug, pathname]);
// Navigate back to the links page when the Escape key is pressed in the workspace settings
useKeyboardShortcut("Escape", () => router.push(`/${slug}/links`), {
enabled: currentArea === "workspaceSettings",
});
const { canTrackConversions } = getPlanCapabilities(plan);
const AppBottomContent =
canTrackConversions && pathname.startsWith(`/${slug}/links`) ? (
<Link
href={`/${slug}/settings/tracking`}
className="flex w-full items-center justify-center gap-2 rounded-lg bg-neutral-200/75 px-2.5 py-2 text-xs font-medium text-neutral-700 transition-colors hover:bg-neutral-200"
>
<BookOpen className="size-4" />
Set up conversion tracking
</Link>
) : null;
const freePlanOrTrial =
plan && (plan === "free" || isWorkspaceBillingTrialActive(trialEndsAt));
return (
<SidebarNav
groups={NAV_GROUPS}
areas={NAV_AREAS}
currentArea={currentArea}
data={{
slug: slug || "",
pathname,
queryString: getQueryString(undefined, {
include: ["folderId"],
}),
session: session || undefined,
}}
switcher={<WorkspaceDropdown />}
toolContent={toolContent}
bottomContent={
AppBottomContent || freePlanOrTrial ? (
<>
<div className="px-3 pb-2">{AppBottomContent}</div>
{freePlanOrTrial && <SidebarUsage />}
</>
) : undefined
}
newsContent={
!freePlanOrTrial && currentArea === "links" ? newsContent : undefined
}
/>
);
}