The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add product persistence
Tim Wilsonbe3f26c6c5bb
diff --git a/apps/web/lib/middleware/utils/app-redirect.ts b/apps/web/lib/middleware/utils/app-redirect.tsindex 92f0f7ed4c..e83136460c 100644--- a/apps/web/lib/middleware/utils/app-redirect.ts+++ b/apps/web/lib/middleware/utils/app-redirect.ts@@ -1,4 +1,5 @@import { RESERVED_SLUGS } from "@dub/utils";+import { cookies } from "next/headers";const APP_REDIRECTS = {"/account": "/account/settings",@@ -11,10 +12,22 @@ export const appRedirect = (path: string) => {if (APP_REDIRECTS[path]) {return APP_REDIRECTS[path];}- // Redirect "/[slug]" to "/[slug]/links"++ // Redirect "/[slug]" to "/[slug]/[product]"const rootRegex = /^\/([^\/]+)$/;- if (rootRegex.test(path) && !RESERVED_SLUGS.includes(path.split("/")[1]))- return path.replace(rootRegex, "/$1/links");+ if (rootRegex.test(path) && !RESERVED_SLUGS.includes(path.split("/")[1])) {+ // Determine product from cookie (default to links)+ let product = "links";++ const productCookie = cookies().get(+ `dub_product:${path.split("/")[1]}`,+ )?.value;++ if (productCookie && ["links", "program"].includes(productCookie))+ product = `/${productCookie}`;++ return path.replace(rootRegex, `/$1/${product}`);+ }// Redirect "/[slug]/upgrade" to "/[slug]/settings/billing/upgrade"const upgradeRegex = /^\/([^\/]+)\/upgrade$/;diff --git a/apps/web/lib/middleware/workspaces.ts b/apps/web/lib/middleware/workspaces.tsindex ff8ccdb4ff..cd2e2f640d 100644--- a/apps/web/lib/middleware/workspaces.ts+++ b/apps/web/lib/middleware/workspaces.ts@@ -1,4 +1,5 @@import { UserProps } from "@/lib/types";+import { cookies } from "next/headers";import { NextRequest, NextResponse } from "next/server";import { isValidInternalRedirect, parse } from "./utils";import { getDefaultWorkspace } from "./utils/get-default-workspace";@@ -27,9 +28,22 @@ export default async function WorkspacesMiddleware(} else if (isTopLevelSettingsRedirect(path)) {redirectPath = `/settings/${path}`;}++ if (!redirectPath) {+ // Determine product from cookie (default to links)+ redirectPath = "/links";++ const productCookie = cookies().get(+ `dub_product:${defaultWorkspace}`,+ )?.value;++ if (productCookie && ["links", "program"].includes(productCookie))+ redirectPath = `/${productCookie}`;+ }+return NextResponse.redirect(new URL(- `/${defaultWorkspace}${redirectPath || "/links"}${searchParamsString}`,+ `/${defaultWorkspace}${redirectPath}${searchParamsString}`,req.url,),);diff --git a/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx b/apps/web/ui/layout/sidebar/app-sidebar-nav.tsxindex 60f8ca7db1..6a22d39c24 100644--- a/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx+++ b/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx@@ -42,6 +42,8 @@ type SidebarNavData = {showNews?: boolean;};+const FIVE_YEARS_SECONDS = 60 * 60 * 24 * 365 * 5;+const NAV_GROUPS: SidebarNavGroups<SidebarNavData> = ({slug,pathname,@@ -58,6 +60,10 @@ const NAV_GROUPS: SidebarNavGroups<SidebarNavData> = ({active:pathname.startsWith(`/${slug}`) &&!pathname.startsWith(`/${slug}/program`),++ onClick: () => {+ document.cookie = `dub_product:${slug}=links;path=/;max-age=${FIVE_YEARS_SECONDS}`;+ },},{name: "Partner Program",@@ -67,6 +73,12 @@ const NAV_GROUPS: SidebarNavGroups<SidebarNavData> = ({icon: ConnectedDots4,href: `/${slug}/program`,active: pathname.startsWith(`/${slug}/program`),++ onClick: defaultProgramId+ ? () => {+ document.cookie = `dub_product:${slug}=program;path=/;max-age=${FIVE_YEARS_SECONDS}`;+ }+ : undefined,},];diff --git a/apps/web/ui/layout/sidebar/sidebar-nav.tsx b/apps/web/ui/layout/sidebar/sidebar-nav.tsxindex fd4eb03291..857533e851 100644--- a/apps/web/ui/layout/sidebar/sidebar-nav.tsx+++ b/apps/web/ui/layout/sidebar/sidebar-nav.tsx@@ -38,6 +38,7 @@ export type NavGroupType = {icon: Icon;href: string;active: boolean;+ onClick?: () => void;description: string;learnMoreHref?: string;@@ -180,7 +181,7 @@ export function NavGroupTooltip({children,}: PropsWithChildren<{name: string;- description: string;+ description?: string;learnMoreHref?: string;disabled?: boolean;}>) {@@ -193,27 +194,29 @@ export function NavGroupTooltip({content={<div><span>{name}</span>- <motion.div- initial={{ opacity: 0, width: 0, height: 0 }}- animate={{ opacity: 1, width: "auto", height: "auto" }}- transition={{ delay: 0.5, duration: 0.25, type: "spring" }}- className="overflow-hidden"- >- <div className="w-44 py-1 text-xs tracking-tight">- <p className="text-content-muted">{description}</p>- {learnMoreHref && (- <div className="mt-2.5">- <Link- href={learnMoreHref}- target="_blank"- className="font-semibold text-white underline"- >- Learn more- </Link>- </div>- )}- </div>- </motion.div>+ {description && (+ <motion.div+ initial={{ opacity: 0, width: 0, height: 0 }}+ animate={{ opacity: 1, width: "auto", height: "auto" }}+ transition={{ delay: 0.5, duration: 0.25, type: "spring" }}+ className="overflow-hidden"+ >+ <div className="w-44 py-1 text-xs tracking-tight">+ <p className="text-content-muted">{description}</p>+ {learnMoreHref && (+ <div className="mt-2.5">+ <Link+ href={learnMoreHref}+ target="_blank"+ className="font-semibold text-white underline"+ >+ Learn more+ </Link>+ </div>+ )}+ </div>+ </motion.div>+ )}</div>}>@@ -223,7 +226,15 @@ export function NavGroupTooltip({}function NavGroupItem({- group: { name, description, learnMoreHref, icon: Icon, href, active },+ group: {+ name,+ description,+ learnMoreHref,+ icon: Icon,+ href,+ active,+ onClick,+ },}: {group: NavGroupType;}) {@@ -240,6 +251,7 @@ function NavGroupItem({href={href}onPointerEnter={() => setHovered(true)}onPointerLeave={() => setHovered(false)}+ onClick={onClick}className={cn("flex size-12 items-center justify-center rounded-lg transition-colors duration-150",activediff --git a/apps/web/ui/layout/sidebar/user-dropdown.tsx b/apps/web/ui/layout/sidebar/user-dropdown.tsxindex 2c4b49c8b0..7e09680776 100644--- a/apps/web/ui/layout/sidebar/user-dropdown.tsx+++ b/apps/web/ui/layout/sidebar/user-dropdown.tsx@@ -77,7 +77,7 @@ export default function UserDropdown() {<UserOptionas="button"type="button"- label="Logout"+ label="Log out"icon={LogOut}onClick={() =>signOut({