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 set as default product action to sidebar product tooltips

Cursor Agentef845c5d42dd
diff --git a/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx b/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx
index dd3f9b1ce7..0a649fc161 100644
--- a/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx
+++ b/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx
@@ -1,5 +1,6 @@
"use client";
+import { clientAccessCheck } from "@/lib/client-access-check";
import { usePartnerMessagesCount } from "@/lib/messages/hooks/use-partner-messages-count";
import { getPlanCapabilities } from "@/lib/plan-capabilities";
import { SUBMITTED_LEADS_ENABLED_PROGRAM_IDS } from "@/lib/submitted-leads/constants";
@@ -46,10 +47,13 @@ import {
Webhook,
} from "@dub/ui/icons";
import { isWorkspaceBillingTrialActive } from "@dub/utils";
+import { DubProduct } from "@prisma/client";
import { Session } from "next-auth";
import { useSession } from "next-auth/react";
import { useParams, usePathname } from "next/navigation";
import { ReactNode, useMemo } from "react";
+import { toast } from "sonner";
+import { mutate } from "swr";
import { DubPartnersPopup } from "./dub-partners-popup";
import { Compass } from "./icons/compass";
import { ConnectedDots4 } from "./icons/connected-dots4";
@@ -67,6 +71,8 @@ type SidebarNavData = {
pathname: string;
queryString: string;
defaultProduct?: "program" | "links";
+ canSetDefaultProduct?: boolean;
+ onSetDefaultProduct?: (product: DubProduct) => Promise<void>;
session?: Session | null;
showNews?: boolean;
pendingPayoutsCount?: number;
@@ -83,9 +89,10 @@ const NAV_GROUPS: SidebarNavGroups<SidebarNavData> = ({
slug,
pathname,
defaultProduct,
+ canSetDefaultProduct,
+ onSetDefaultProduct,
}) => {
const programGroup = {
- id: "program",
name: "Partner Program",
description:
"Kickstart viral product-led growth with powerful, branded referral and affiliate programs.",
@@ -98,9 +105,13 @@ const NAV_GROUPS: SidebarNavGroups<SidebarNavData> = ({
!pathname.startsWith(`/${slug}/links`) &&
!pathname.startsWith(`/${slug}/settings`),
popup: DubPartnersPopup,
+ isDefault: defaultProduct === "program",
+ onSetDefault:
+ canSetDefaultProduct && onSetDefaultProduct
+ ? () => onSetDefaultProduct("program")
+ : undefined,
};
const linksGroup = {
- id: "links",
name: "Short Links",
description:
"Create, organize, and measure the performance of your short links.",
@@ -108,6 +119,11 @@ const NAV_GROUPS: SidebarNavGroups<SidebarNavData> = ({
icon: Compass,
href: slug ? `/${slug}/links` : "/links",
active: pathname.startsWith(`/${slug}/links`),
+ isDefault: (defaultProduct ?? "links") === "links",
+ onSetDefault:
+ canSetDefaultProduct && onSetDefaultProduct
+ ? () => onSetDefaultProduct("links")
+ : undefined,
};
return defaultProduct === "links"
@@ -502,8 +518,51 @@ export function AppSidebarNav({
const pathname = usePathname();
const { router, getQueryString } = useRouterStuff();
const { data: session } = useSession();
- const { plan, defaultProduct, defaultProgramId, trialEndsAt } =
- useWorkspace();
+ const {
+ id: workspaceId,
+ plan,
+ defaultProduct,
+ defaultProgramId,
+ trialEndsAt,
+ role,
+ } = useWorkspace();
+
+ const canSetDefaultProduct = !clientAccessCheck({
+ action: "workspaces.write",
+ role,
+ }).error;
+
+ async function onSetDefaultProduct(product: DubProduct) {
+ if (!workspaceId || !slug) {
+ return;
+ }
+
+ await toast.promise(
+ (async () => {
+ const response = await fetch(`/api/workspaces/${workspaceId}`, {
+ method: "PATCH",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ defaultProduct: product,
+ }),
+ });
+
+ if (!response.ok) {
+ const { error } = await response.json();
+ throw new Error(error.message);
+ }
+
+ await mutate(`/api/workspaces/${slug}`);
+ })(),
+ {
+ loading: "Setting default product...",
+ success: "Successfully updated your default product!",
+ error: (error) => error.message,
+ },
+ );
+ }
const currentArea = useMemo(() => {
return pathname.startsWith("/account/settings")
@@ -597,6 +656,8 @@ export function AppSidebarNav({
session: session || undefined,
showNews: true,
defaultProduct,
+ canSetDefaultProduct,
+ onSetDefaultProduct,
pendingPayoutsCount: pendingPayoutsCount?.[0]?.count ?? 0,
applicationsCount,
submittedBountiesCount,
diff --git a/apps/web/ui/layout/sidebar/sidebar-nav.tsx b/apps/web/ui/layout/sidebar/sidebar-nav.tsx
index 0e1a83caec..d365fed710 100644
--- a/apps/web/ui/layout/sidebar/sidebar-nav.tsx
+++ b/apps/web/ui/layout/sidebar/sidebar-nav.tsx
@@ -57,6 +57,8 @@ export type NavGroupType = {
badge?: ReactNode;
description: string;
learnMoreHref?: string;
+ isDefault?: boolean;
+ onSetDefault?: () => Promise<void> | void;
};
export type SidebarNavGroups<T extends Record<any, any>> = (
@@ -317,21 +319,27 @@ export function NavGroupTooltip({
name,
description,
learnMoreHref,
+ isDefault,
+ onSetDefault,
disabled,
children,
}: PropsWithChildren<{
name: string;
description?: string;
learnMoreHref?: string;
+ isDefault?: boolean;
+ onSetDefault?: () => Promise<void> | void;
disabled?: boolean;
}>) {
+ const [settingDefault, setSettingDefault] = useState(false);
+
return (
<Tooltip
side="right"
delayDuration={100}
disabled={disabled}
className="rounded-lg bg-black px-3 py-1.5 text-sm font-medium text-white"
- content={
+ content={({ setOpen }) => (
<div>
<span>{name}</span>
{description && (
@@ -343,22 +351,54 @@ export function NavGroupTooltip({
>
<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>
+ {(learnMoreHref || onSetDefault || isDefault) && (
+ <div className="mt-2.5 flex flex-col gap-2">
+ {learnMoreHref && (
+ <Link
+ href={learnMoreHref}
+ target="_blank"
+ className="font-semibold text-white underline"
+ >
+ Learn more
+ </Link>
+ )}
+ {isDefault ? (
+ <p className="text-content-muted font-medium">
+ Default product
+ </p>
+ ) : (
+ onSetDefault && (
+ <button
+ type="button"
+ disabled={settingDefault}
+ onClick={async (e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setSettingDefault(true);
+ try {
+ await onSetDefault();
+ setOpen(false);
+ } catch {
+ // Error toast is handled by the caller
+ } finally {
+ setSettingDefault(false);
+ }
+ }}
+ className="w-full rounded-md bg-white/10 px-2 py-1.5 text-left text-xs font-semibold text-white transition-colors hover:bg-white/20 disabled:cursor-not-allowed disabled:opacity-60"
+ >
+ {settingDefault
+ ? "Setting default..."
+ : "Set as default product"}
+ </button>
+ )
+ )}
</div>
)}
</div>
</motion.div>
)}
</div>
- }
+ )}
>
{children}
</Tooltip>
@@ -370,6 +410,8 @@ function NavGroupItem({
name,
description,
learnMoreHref,
+ isDefault,
+ onSetDefault,
icon: Icon,
href,
active,
@@ -389,6 +431,8 @@ function NavGroupItem({
name={name}
description={description}
learnMoreHref={learnMoreHref}
+ isDefault={isDefault}
+ onSetDefault={onSetDefault}
>
<div>
<Link