The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Continue filling in side nav
Tim Wilson56ca7f5853ef
diff --git a/apps/web/ui/layout/main-nav.tsx b/apps/web/ui/layout/main-nav.tsxindex c0ba15a875..5b58abbf7d 100644--- a/apps/web/ui/layout/main-nav.tsx+++ b/apps/web/ui/layout/main-nav.tsx@@ -22,6 +22,7 @@ import {} from "react";import { Divider } from "../shared/icons";import NavTabs from "./nav-tabs";+import { SidebarNav } from "./sidebar/sidebar-nav";import UpgradeBanner from "./upgrade-banner";import UserDropdown from "./user-dropdown";import WorkspaceSwitcher from "./workspace-switcher";@@ -76,52 +77,15 @@ export function MainNav({ children }: PropsWithChildren) {!isOpen && "-translate-x-full",)}>- <div className="flex size-full items-center justify-center text-gray-500">- [nav]- </div>+ <SidebarNav /></nav></div>- <div>- <div className="sticky -top-16 z-20 border-b border-gray-200 bg-white">- <MaxWidthWrapper>- <div className="flex h-16 items-center justify-between">- <div className="flex items-center">- <Link- href={`/${slug || ""}`}- className={cn(- "hidden transition-all sm:block",- scrolled && "translate-y-[3.4rem]",- )}- >- <NavLogo- variant="symbol"- isInApp- {...(scrolled && { className: "h-6 w-6" })}- />- </Link>- <Divider className="hidden h-8 w-8 text-gray-200 sm:ml-3 sm:block" />- <WorkspaceSwitcher />- </div>- <div className="flex items-center space-x-6">- <UpgradeBanner />- <a- href="https://dub.co/help"- className="hidden text-sm text-gray-500 transition-colors hover:text-gray-700 sm:block"- target="_blank"- >- Help- </a>- <UserDropdown />- </div>- </div>- <Suspense fallback={<div className="h-12 w-full" />}>- <NavTabs />- </Suspense>- </MaxWidthWrapper>+ <div className="bg-neutral-100">+ <div className="relative min-h-full bg-white pt-px sm:rounded-tl-[16px]">+ <SideNavContext.Provider value={{ isOpen, setIsOpen }}>+ {children}+ </SideNavContext.Provider></div>- <SideNavContext.Provider value={{ isOpen, setIsOpen }}>- {children}- </SideNavContext.Provider></div></div>) : (diff --git a/apps/web/ui/layout/sidebar/sidebar-nav.tsx b/apps/web/ui/layout/sidebar/sidebar-nav.tsxnew file mode 100644index 0000000000..c4cba5fcd0--- /dev/null+++ b/apps/web/ui/layout/sidebar/sidebar-nav.tsx@@ -0,0 +1,56 @@+import { Wordmark } from "@dub/ui";+import { CursorRays, Hyperlink, LinesY } from "@dub/ui/src";+import { cn } from "@dub/utils";+import Link from "next/link";+import { useParams, usePathname } from "next/navigation";+import { useMemo } from "react";+import UserDropdown from "./user-dropdown";+import { WorkspaceDropdown } from "./workspace-dropdown";++export function SidebarNav() {+ const { slug } = useParams() as { slug?: string };+ const pathname = usePathname();++ const tabs = useMemo(+ () => [+ { name: "Links", icon: Hyperlink, href: `/${slug}` },+ { name: "Analytics", icon: LinesY, href: `/${slug}/analytics` },+ { name: "Events", icon: CursorRays, href: `/${slug}/events` },+ ],+ [slug],+ );++ return (+ <div className="p-3 text-gray-500">+ <div className="flex items-start justify-between gap-1">+ <Wordmark className="ml-1 h-6" />+ <UserDropdown />+ </div>++ <div className="mt-7">+ <WorkspaceDropdown />+ </div>++ <div className="mt-4 flex flex-col gap-0.5">+ {tabs.map(({ name, icon: Icon, href }) => {+ const isActive =+ href === `/${slug}` ? pathname === href : pathname.startsWith(href);++ return (+ <Link+ key={href}+ href={href}+ className={cn(+ "flex items-center gap-2.5 rounded-md p-2 text-sm text-neutral-600 transition-colors duration-75 hover:bg-neutral-200/50 active:bg-neutral-200/80",+ isActive && "bg-neutral-200/50",+ )}+ >+ <Icon className="size-4 text-gray-500" />+ {name}+ </Link>+ );+ })}+ </div>+ </div>+ );+}diff --git a/apps/web/ui/layout/sidebar/user-dropdown.tsx b/apps/web/ui/layout/sidebar/user-dropdown.tsxnew file mode 100644index 0000000000..e7c669bfe1--- /dev/null+++ b/apps/web/ui/layout/sidebar/user-dropdown.tsx@@ -0,0 +1,142 @@+"use client";++import { Avatar, Badge, Popover } from "@dub/ui";+import { Gear, Icon } from "@dub/ui/src";+import Cookies from "js-cookie";+import { Edit3, HelpCircle, LogOut } from "lucide-react";+import { signOut, useSession } from "next-auth/react";+import Link from "next/link";+import {+ ComponentPropsWithoutRef,+ ElementType,+ useEffect,+ useState,+} from "react";++export default function UserDropdown() {+ const { data: session } = useSession();+ const [openPopover, setOpenPopover] = useState(false);++ const [unreadChangelogs, setUnreadChangelogs] = useState(0);+ useEffect(() => {+ const lastReadChangelog = Cookies.get("lastReadChangelog");+ if (!lastReadChangelog) {+ setUnreadChangelogs(2);+ }+ }, []);++ return (+ <Popover+ content={+ <div className="flex w-full flex-col space-y-px rounded-md bg-white p-2 sm:w-56">+ {session?.user ? (+ <div className="p-2">+ <p className="truncate text-sm font-medium text-neutral-900">+ {session.user.name || session.user.email?.split("@")[0]}+ </p>+ <p className="truncate text-sm text-neutral-500">+ {session.user.email}+ </p>+ </div>+ ) : (+ <div className="grid gap-2 px-2 py-3">+ <div className="h-3 w-12 animate-pulse rounded-full bg-neutral-200" />+ <div className="h-3 w-20 animate-pulse rounded-full bg-neutral-200" />+ </div>+ )}+ <UserOption+ as={Link}+ label="Help Center"+ icon={HelpCircle}+ href="https://dub.co/help"+ target="_blank"+ onClick={() => setOpenPopover(false)}+ />+ <UserOption+ as={Link}+ label="User Settings"+ icon={Gear}+ href="/account/settings"+ onClick={() => setOpenPopover(false)}+ />+ <UserOption+ as={Link}+ label="Changelog"+ icon={Edit3}+ href="https://dub.co/changelog"+ target="_blank"+ onClick={() => {+ Cookies.set("lastReadChangelog", new Date().toISOString());+ setOpenPopover(false);+ }}+ >+ {unreadChangelogs > 0 && (+ <div className="flex grow justify-end">+ <Badge variant="blue">{unreadChangelogs}</Badge>+ </div>+ )}+ </UserOption>+ <UserOption+ as="button"+ type="button"+ label="Logout"+ icon={LogOut}+ onClick={() =>+ signOut({+ callbackUrl: "/login",+ })+ }+ />+ </div>+ }+ align="start"+ openPopover={openPopover}+ setOpenPopover={setOpenPopover}+ >+ <button+ onClick={() => setOpenPopover(!openPopover)}+ className="group relative rounded-full outline-none ring-offset-1 ring-offset-neutral-100 transition-all hover:ring-2 hover:ring-black/10 focus:outline-none active:ring-black/15 data-[focus-visible=true]:ring-2 data-[state='open']:ring-black/15"+ >+ {session?.user ? (+ <Avatar+ user={session.user}+ className="size-7 border-none duration-75 sm:size-7"+ />+ ) : (+ <div className="size-7 animate-pulse rounded-full bg-gray-100 sm:size-7" />+ )}+ {unreadChangelogs > 0 && (+ <div className="absolute -bottom-0.5 -right-0.5 size-3 rounded-full border-2 border-white bg-blue-500" />+ )}+ </button>+ </Popover>+ );+}++type UserOptionProps<T extends ElementType> = {+ as?: T;+ label: string;+ icon: Icon;+};++function UserOption<T extends ElementType = "button">({+ as,+ label,+ icon: Icon,+ children,+ ...rest+}: UserOptionProps<T> &+ Omit<ComponentPropsWithoutRef<T>, keyof UserOptionProps<T>>) {+ const Component = as ?? "button";++ return (+ <Component+ className="flex items-center gap-x-4 rounded-md px-2.5 py-2 text-sm transition-all duration-75 hover:bg-neutral-200/50 active:bg-neutral-200/80"+ {...rest}+ >+ <Icon className="size-4 text-neutral-500" />+ <span className="block truncate text-neutral-600">{label}</span>+ {children}+ </Component>+ );+}diff --git a/apps/web/ui/layout/sidebar/workspace-dropdown.tsx b/apps/web/ui/layout/sidebar/workspace-dropdown.tsxnew file mode 100644index 0000000000..65de3d160a--- /dev/null+++ b/apps/web/ui/layout/sidebar/workspace-dropdown.tsx@@ -0,0 +1,270 @@+"use client";++import useWorkspaces from "@/lib/swr/use-workspaces";+import { PlanProps, WorkspaceProps } from "@/lib/types";+import { ModalContext } from "@/ui/modals/modal-provider";+import { BlurImage, getUserAvatarUrl, Popover } from "@dub/ui";+import { Book2, Check2, Gear, Plus } from "@dub/ui/src/icons";+import { cn, DICEBEAR_AVATAR_URL } from "@dub/utils";+import { ChevronsUpDown } from "lucide-react";+import { useSession } from "next-auth/react";+import Link from "next/link";+import { useParams, usePathname } from "next/navigation";+import { useCallback, useContext, useMemo, useState } from "react";++export function WorkspaceDropdown() {+ const { workspaces } = useWorkspaces();+ const { data: session, status } = useSession();+ const { slug, key } = useParams() as {+ slug?: string;+ key?: string;+ };++ const selected = useMemo(() => {+ const selectedWorkspace = workspaces?.find(+ (workspace) => workspace.slug === slug,+ );++ if (slug && workspaces && selectedWorkspace) {+ return {+ ...selectedWorkspace,+ image:+ selectedWorkspace.logo ||+ `${DICEBEAR_AVATAR_URL}${selectedWorkspace.name}`,+ };++ // return personal account selector if there's no workspace or error (user doesn't have access to workspace)+ } else {+ return {+ name: session?.user?.name || session?.user?.email,+ slug: "/",+ image: getUserAvatarUrl(session?.user),+ plan: "free",+ };+ }+ }, [slug, workspaces, session]) as {+ id?: string;+ name: string;+ slug: string;+ image: string;+ plan: PlanProps;+ };++ const [openPopover, setOpenPopover] = useState(false);++ if (!workspaces || status === "loading") {+ return <WorkspaceSwitcherPlaceholder />;+ }++ return (+ <div>+ <Popover+ content={+ <WorkspaceList+ selected={selected}+ workspaces={workspaces}+ setOpenPopover={setOpenPopover}+ />+ }+ align="start"+ openPopover={openPopover}+ setOpenPopover={setOpenPopover}+ >+ <button+ onClick={() => setOpenPopover(!openPopover)}+ className="flex w-full items-center justify-between rounded-lg p-1.5 text-left text-sm outline-none transition-all duration-75 hover:bg-neutral-200/50 active:bg-neutral-200/80 data-[state=open]:bg-neutral-200/80"+ >+ <div className="flex min-w-0 items-center gap-x-2.5 pr-2">+ <BlurImage+ src={selected.image}+ referrerPolicy="no-referrer"+ width={28}+ height={28}+ alt={selected.id || selected.name}+ className="h-7 w-7 flex-none shrink-0 overflow-hidden rounded-full"+ />+ <div className={cn(key ? "hidden" : "block", "min-w-0 sm:block")}>+ <div className="truncate text-sm font-medium leading-5 text-neutral-900">+ {selected.name}+ </div>+ {selected.slug !== "/" && (+ <div+ className={cn(+ "truncate text-xs capitalize leading-tight",+ getPlanColor(selected.plan),+ )}+ >+ {selected.plan}+ </div>+ )}+ </div>+ </div>+ <ChevronsUpDown+ className="size-4 shrink-0 text-gray-400"+ aria-hidden="true"+ />+ </button>+ </Popover>+ </div>+ );+}++function WorkspaceSwitcherPlaceholder() {+ return (+ <div className="flex w-full animate-pulse items-center gap-x-1.5 rounded-lg p-1.5">+ <div className="size-7 animate-pulse rounded-full bg-gray-200" />+ <div className="mb-px mt-0.5 h-8 w-28 grow animate-pulse rounded-md bg-gray-200" />+ <ChevronsUpDown className="h-4 w-4 text-gray-400" aria-hidden="true" />+ </div>+ );+}++function WorkspaceList({+ selected,+ workspaces,+ setOpenPopover,+}: {+ selected: {+ name: string;+ slug: string;+ image: string;+ plan: PlanProps;+ };+ workspaces: WorkspaceProps[];+ setOpenPopover: (open: boolean) => void;+}) {+ const { setShowAddWorkspaceModal } = useContext(ModalContext);+ const { slug, domain, key } = useParams() as {+ slug?: string;+ domain?: string;+ key?: string;+ };+ const pathname = usePathname();++ const links = useMemo(+ () => [+ { name: "Settings", icon: Gear, href: `/${slug}/settings` },+ {+ name: "Documentation",+ icon: Book2,+ href: "https://dub.co/docs",+ target: "_blank",+ },+ ],+ [slug],+ );++ const href = useCallback(+ (slug: string) => {+ if (domain || key || selected.slug === "/") {+ // if we're on a link page, navigate back to the workspace root+ return `/${slug}`;+ } else {+ // else, we keep the path but remove all query params+ return pathname?.replace(selected.slug, slug).split("?")[0] || "/";+ }+ },+ [domain, key, pathname, selected.slug],+ );++ return (+ <div className="relative max-h-72 w-full space-y-0.5 overflow-auto rounded-lg bg-white text-base sm:w-64 sm:text-sm">+ <div className="flex flex-col gap-0.5 border-b border-neutral-200 p-2">+ {links.map(({ name, icon: Icon, href, target }) => (+ <Link+ key={name}+ href={href}+ target={target}+ className="flex items-center gap-x-4 rounded-md px-2.5 py-2 transition-all duration-75 hover:bg-neutral-200/50 active:bg-neutral-200/80"+ onClick={() => setOpenPopover(false)}+ >+ <Icon className="size-4 text-neutral-500" />+ <span className="block truncate text-neutral-600">{name}</span>+ </Link>+ ))}+ </div>+ <div className="p-2">+ <div className="flex items-center justify-between pb-1">+ <p className="px-1 text-xs font-medium text-neutral-500">+ Workspaces+ </p>+ {workspaces.length > 0 && (+ <Link+ href="/workspaces"+ onClick={() => setOpenPopover(false)}+ className="rounded-md px-2 py-1 text-xs text-neutral-500 transition-colors duration-75 hover:bg-neutral-200/50 hover:text-neutral-700 active:bg-neutral-200/80"+ >+ View All+ </Link>+ )}+ </div>+ <div className="flex flex-col gap-0.5">+ {workspaces.map(({ id, name, slug, logo, plan }) => {+ const isActive = selected.slug === slug;+ return (+ <Link+ key={slug}+ className={cn(+ "relative flex w-full items-center gap-x-2 rounded-md px-2 py-1.5 transition-all duration-75",+ "hover:bg-neutral-200/50 active:bg-neutral-200/80",+ isActive && "bg-neutral-200/50",+ )}+ href={href(slug)}+ shallow={false}+ onClick={() => setOpenPopover(false)}+ >+ <BlurImage+ src={logo || `${DICEBEAR_AVATAR_URL}${name}`}+ width={28}+ height={28}+ alt={id}+ className="size-7 shrink-0 overflow-hidden rounded-full"+ />+ <div>+ <span className="block truncate text-sm leading-5 text-neutral-900 sm:max-w-[140px]">+ {name}+ </span>+ {slug !== "/" && (+ <div+ className={cn(+ "truncate text-xs capitalize leading-tight",+ getPlanColor(plan),+ )}+ >+ {plan}+ </div>+ )}+ </div>+ {selected.slug === slug ? (+ <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-black">+ <Check2 className="size-4" aria-hidden="true" />+ </span>+ ) : null}+ </Link>+ );+ })}+ <button+ key="add"+ onClick={() => {+ setOpenPopover(false);+ setShowAddWorkspaceModal(true);+ }}+ className="group flex w-full cursor-pointer items-center gap-x-2 rounded-md p-2 text-neutral-700 transition-all duration-75 hover:bg-neutral-200/50 active:bg-neutral-200/80"+ >+ <Plus className="mx-1.5 size-4 text-neutral-500" />+ <span className="block truncate">Create new workspace</span>+ </button>+ </div>+ </div>+ </div>+ );+}++const getPlanColor = (plan: string) =>+ plan === "enterprise"+ ? "text-purple-700"+ : plan.startsWith("business")+ ? "text-blue-900"+ : plan === "pro"+ ? "text-cyan-900"+ : "text-neutral-500";