The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add partnerPreferences
Pedro Ladeira69d63f7155a9
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/links/[linkId]/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/links/[linkId]/route.tsindex b9a2b51d74..f2aa969006 100644--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/links/[linkId]/route.ts+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/links/[linkId]/route.ts@@ -7,8 +7,10 @@ import { extractUtmParams } from "@/lib/api/utm/extract-utm-params";import { withPartnerProfile } from "@/lib/auth/partner";import { prisma } from "@/lib/prisma";import { NewLinkProps } from "@/lib/types";-import { PartnerProfileLinkSchema } from "@/lib/zod/schemas/partner-profile";-import { createPartnerLinkSchema } from "@/lib/zod/schemas/partners";+import {+ PartnerProfileLinkSchema,+ createPartnerProfileLinkSchema,+} from "@/lib/zod/schemas/partner-profile";import { getPrettyUrl, toCentsNumber } from "@dub/utils";import { NextResponse } from "next/server";@@ -16,14 +18,7 @@ import { NextResponse } from "next/server";export const PATCH = withPartnerProfile(async ({ partner, params, req, session }) => {const { url, key, partnerLinkTitle, partnerLinkComments } =- createPartnerLinkSchema- .pick({- url: true,- key: true,- partnerLinkTitle: true,- partnerLinkComments: true,- })- .parse(await parseRequestBody(req));+ createPartnerProfileLinkSchema.parse(await parseRequestBody(req));const { programId, linkId } = params;diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/links/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/links/route.tsindex 139cfb3537..c6837de199 100644--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/links/route.ts+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/links/route.ts@@ -6,11 +6,11 @@ import { parseRequestBody } from "@/lib/api/utils";import { extractUtmParams } from "@/lib/api/utm/extract-utm-params";import { withPartnerProfile } from "@/lib/auth/partner";import { prisma } from "@/lib/prisma";-import { PartnerProfileLinkSchema } from "@/lib/zod/schemas/partner-profile";import {- createPartnerLinkSchema,- INACTIVE_ENROLLMENT_STATUSES,-} from "@/lib/zod/schemas/partners";+ createPartnerProfileLinkSchema,+ PartnerProfileLinkSchema,+} from "@/lib/zod/schemas/partner-profile";+import { INACTIVE_ENROLLMENT_STATUSES } from "@/lib/zod/schemas/partners";import { getUTMParamsFromURL } from "@dub/utils";import { NextResponse } from "next/server";import * as z from "zod/v4";@@ -48,14 +48,7 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {export const POST = withPartnerProfile(async ({ partner, params, req, session }) => {const { url, key, partnerLinkTitle, partnerLinkComments } =- createPartnerLinkSchema- .pick({- url: true,- key: true,- partnerLinkTitle: true,- partnerLinkComments: true,- })- .parse(await parseRequestBody(req));+ createPartnerProfileLinkSchema.parse(await parseRequestBody(req));const {program,diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/links/page-client.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/links/page-client.tsxindex 3e115c2fc8..6121c65836 100644--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/links/page-client.tsx+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/links/page-client.tsx@@ -8,7 +8,6 @@ import { IntervalOptions } from "@/lib/analytics/types";import usePartnerLinks from "@/lib/swr/use-partner-links";import {PartnerLinksDisplayContext,- PartnerLinksDisplayOption,PartnerLinksDisplayProvider,} from "@/lib/swr/use-partner-links-display";import useProgramEnrollment from "@/lib/swr/use-program-enrollment";@@ -28,7 +27,7 @@ const PartnerLinksContext = createContext<{interval: (typeof DATE_RANGE_INTERVAL_PRESETS)[number];openMenuLinkId: string | null;setOpenMenuLinkId: (id: string | null) => void;- displayOption: PartnerLinksDisplayOption;+ displayOption: "full" | "cards";} | null>(null);export function usePartnerLinksContext() {diff --git a/apps/web/lib/actions/partners/update-program-enrollment-preferences.ts b/apps/web/lib/actions/partners/update-program-enrollment-preferences.tsnew file mode 100644index 0000000000..c89dbd21ca--- /dev/null+++ b/apps/web/lib/actions/partners/update-program-enrollment-preferences.ts@@ -0,0 +1,59 @@+"use server";++import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";+import { prisma } from "@/lib/prisma";+import { programEnrollmentPreferencesValueSchemas } from "@/lib/zod/schemas/programs";+import { Prisma } from "@prisma/client";+import * as z from "zod/v4";+import { authPartnerActionClient } from "../safe-action";++const schema = z.object({+ programId: z.string(),+ key: z.string(),+ value: z.any(),+});++// Update preferences for a partner's program enrollment+export const updateProgramEnrollmentPreferences = authPartnerActionClient+ .inputSchema(schema)+ .action(async ({ ctx, parsedInput }) => {+ const { partner } = ctx;+ const { programId, key, value } = parsedInput;++ const valueSchema =+ programEnrollmentPreferencesValueSchemas[+ key as keyof typeof programEnrollmentPreferencesValueSchemas+ ];++ if (!valueSchema) {+ throw new Error(`Invalid program enrollment preference key: ${key}`);+ }++ const parsedValue = valueSchema.parse(value);++ const programEnrollment = await getProgramEnrollmentOrThrow({+ partnerId: partner.id,+ programId,+ include: {},+ });++ const partnerPreferences =+ (programEnrollment.partnerPreferences as Record<+ string,+ unknown+ > | null) ?? {};++ await prisma.programEnrollment.update({+ where: {+ id: programEnrollment.id,+ },+ data: {+ partnerPreferences: {+ ...partnerPreferences,+ [key]: parsedValue,+ } as Prisma.InputJsonValue,+ },+ });++ return { ok: true };+ });diff --git a/apps/web/lib/analytics/get-analytics.ts b/apps/web/lib/analytics/get-analytics.tsindex 2e89b07d08..51f176a736 100644--- a/apps/web/lib/analytics/get-analytics.ts+++ b/apps/web/lib/analytics/get-analytics.ts@@ -41,6 +41,7 @@ export const getAnalytics = async (params: AnalyticsFilters) => {isDeprecatedClicksEndpoint = false,dataAvailableFrom,query,+ partnerId,} = params;const normalizedLinkId = ensureParsedFilter(linkId);@@ -217,7 +218,7 @@ export const getAnalytics = async (params: AnalyticsFilters) => {key: true,url: true,title: true,- partnerLinkTitle: true,+ ...(partnerId ? { partnerLinkTitle: true as const } : {}),comments: true,folderId: true,partnerId: true,@@ -237,7 +238,7 @@ export const getAnalytics = async (params: AnalyticsFilters) => {key: link.key,});- return analyticsResponse[groupBy].parse({+ const parsed = analyticsResponse[groupBy].parse({...link,link: link.id,key: punyEncode(link.key),@@ -248,6 +249,15 @@ export const getAnalytics = async (params: AnalyticsFilters) => {createdAt: link.createdAt.toISOString(),...item,});++ if (partnerId && "partnerLinkTitle" in link) {+ return {+ ...parsed,+ partnerLinkTitle: link.partnerLinkTitle,+ };+ }++ return parsed;}).filter((d) => d !== null);} else if (groupBy === "top_partners") {diff --git a/apps/web/lib/swr/use-partner-links-display.tsx b/apps/web/lib/swr/use-partner-links-display.tsxindex 1d09260737..4137a506c7 100644--- a/apps/web/lib/swr/use-partner-links-display.tsx+++ b/apps/web/lib/swr/use-partner-links-display.tsx@@ -1,6 +1,8 @@"use client";-import { useSyncedLocalStorage } from "@/lib/hooks/use-synced-local-storage";+import { updateProgramEnrollmentPreferences } from "@/lib/actions/partners/update-program-enrollment-preferences";+import useProgramEnrollment from "@/lib/swr/use-program-enrollment";+import { useAction } from "next-safe-action/hooks";import {createContext,Dispatch,@@ -10,30 +12,26 @@ import {useMemo,useState,} from "react";+import { mutate } from "swr";-export type PartnerLinksDisplayOption = "full" | "cards";-+type PartnerLinksDisplayOption = "full" | "cards";type PartnerLinksDisplayProperty = "link" | "title";-const DEFAULT_DISPLAY_PROPERTIES: PartnerLinksDisplayProperty[] = ["link"];-const STORAGE_KEY = "partnerLinksDisplay";-type PartnerLinksDisplayPreferences = {displayOption: PartnerLinksDisplayOption;displayProperties: PartnerLinksDisplayProperty[];};+const DEFAULT_DISPLAY_PROPERTIES: PartnerLinksDisplayProperty[] = ["link"];+/** Read persisted partner links display prefs (for Analytics / Events / filters). */export function usePartnerLinksDisplay() {- const [persisted] =- useSyncedLocalStorage<PartnerLinksDisplayPreferences | null>(- STORAGE_KEY,- null,- );+ const { programEnrollment } = useProgramEnrollment();return {displayProperties:- persisted?.displayProperties ?? DEFAULT_DISPLAY_PROPERTIES,+ programEnrollment?.partnerPreferences?.linksDisplay?.displayProperties ??+ DEFAULT_DISPLAY_PROPERTIES,};}@@ -41,9 +39,11 @@ type PartnerLinksDisplayContextValue = {displayOption: PartnerLinksDisplayOption;setDisplayOption: Dispatch<SetStateAction<PartnerLinksDisplayOption>>;displayProperties: PartnerLinksDisplayProperty[];- setDisplayProperties: Dispatch<SetStateAction<PartnerLinksDisplayProperty[]>>;+ setDisplayProperties: Dispatch<+ SetStateAction<PartnerLinksDisplayProperty[]>+ >;isDirty: boolean;- persist: () => void;+ persist: () => void | Promise<void>;reset: () => void;};@@ -66,11 +66,10 @@ export function PartnerLinksDisplayProvider({linksCount?: number;showDetailedAnalytics?: boolean;}>) {- const [persisted, setPersisted] =- useSyncedLocalStorage<PartnerLinksDisplayPreferences | null>(- STORAGE_KEY,- null,- );+ const { programEnrollment } = useProgramEnrollment();+ const { executeAsync } = useAction(updateProgramEnrollmentPreferences);++ const persisted = programEnrollment?.partnerPreferences?.linksDisplay ?? null;const resolvedPersisted = useMemo<PartnerLinksDisplayPreferences>(() => {if (persisted) return persisted;@@ -90,7 +89,7 @@ export function PartnerLinksDisplayProvider({PartnerLinksDisplayProperty[]>(resolvedPersisted.displayProperties);- // Sync draft when persisted defaults resolve (e.g. after links load)+ // Sync draft when persisted defaults resolve (e.g. after enrollment/links load)useEffect(() => {setDisplayOption(resolvedPersisted.displayOption);setDisplayProperties(resolvedPersisted.displayProperties);@@ -111,6 +110,24 @@ export function PartnerLinksDisplayProvider({? "cards": displayOption;+ const persist = async () => {+ if (!programEnrollment?.programId) return;++ await executeAsync({+ programId: programEnrollment.programId,+ key: "linksDisplay",+ value: {+ displayOption,+ displayProperties,+ },+ });++ const programSlug = programEnrollment.program?.slug;+ if (programSlug) {+ mutate(`/api/partner-profile/programs/${programSlug}`);+ }+ };+return (<PartnerLinksDisplayContext.Providervalue={{@@ -119,11 +136,7 @@ export function PartnerLinksDisplayProvider({displayProperties,setDisplayProperties,isDirty,- persist: () =>- setPersisted({- displayOption,- displayProperties,- }),+ persist,reset: () => {setDisplayOption(resolvedPersisted.displayOption);setDisplayProperties(resolvedPersisted.displayProperties);diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex a81a6a161b..2fab098749 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -388,7 +388,10 @@ export interface SAMLProviderProps {};}-export type NewLinkProps = z.infer<typeof createLinkBodySchema>;+export type NewLinkProps = z.infer<typeof createLinkBodySchema> & {+ partnerLinkTitle?: string | null;+ partnerLinkComments?: string | null;+};type ProcessedLinkOverrides = "domain" | "key" | "url" | "projectId";diff --git a/apps/web/lib/zod/schemas/analytics-response.ts b/apps/web/lib/zod/schemas/analytics-response.tsindex 758b3b7f06..2305bafbf9 100644--- a/apps/web/lib/zod/schemas/analytics-response.ts+++ b/apps/web/lib/zod/schemas/analytics-response.ts@@ -262,10 +262,6 @@ export const analyticsResponse = {.string().nullish().describe("The custom link preview title (og:title)"),- partnerLinkTitle: z- .string()- .nullish()- .describe("The partner's private display title for the short link"),comments: z.string().nullish().describe("The comments of the short link"),folderId: z.string()diff --git a/apps/web/lib/zod/schemas/customer-activity.ts b/apps/web/lib/zod/schemas/customer-activity.tsindex a792625102..032833b1e9 100644--- a/apps/web/lib/zod/schemas/customer-activity.ts+++ b/apps/web/lib/zod/schemas/customer-activity.ts@@ -8,6 +8,5 @@ export const customerActivityResponseSchema = z.object({domain: true,key: true,shortLink: true,- partnerLinkTitle: true,}).nullish(),});diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.tsindex 417b3b29c1..d9e60df919 100644--- a/apps/web/lib/zod/schemas/links.ts+++ b/apps/web/lib/zod/schemas/links.ts@@ -393,14 +393,6 @@ export const createLinkBodySchema = z.object({.nullish().describe("The unique ID existing folder to assign the short link to."),comments: z.string().nullish().describe("The comments for the short link."),- partnerLinkTitle: z- .string()- .nullish()- .describe("The partner's private display title for the short link."),- partnerLinkComments: z- .string()- .nullish()- .describe("The partner's private comments for the short link."),expiresAt: z.string().nullish()@@ -715,16 +707,6 @@ export const LinkSchema = z.string().nullable().describe("The comments for the short link."),- partnerLinkTitle: z- .string()- .nullish()- .default(null)- .describe("The partner's private display title for the short link."),- partnerLinkComments: z- .string()- .nullish()- .default(null)- .describe("The partner's private comments for the short link."),shortLink: z.url().describe(diff --git a/apps/web/lib/zod/schemas/partner-profile.ts b/apps/web/lib/zod/schemas/partner-profile.tsindex e8e97b5518..be9e858e13 100644--- a/apps/web/lib/zod/schemas/partner-profile.ts+++ b/apps/web/lib/zod/schemas/partner-profile.ts@@ -27,7 +27,7 @@ import { LinkSchema } from "./links";import { getPaginationQuerySchema } from "./misc";import { payoutsQuerySchema } from "./payouts";import { submittedLeadFormDataSchema } from "./submitted-lead-form";-import { centsSchema } from "./utils";+import { centsSchema, parseUrlSchema } from "./utils";export const PartnerEarningsSchema = CommissionSchema.omit({userId: true,@@ -44,8 +44,11 @@ export const PartnerEarningsSchema = CommissionSchema.omit({id: true,shortLink: true,url: true,- partnerLinkTitle: true,- }).nullish(),+ })+ .extend({+ partnerLinkTitle: z.string().nullish(),+ })+ .nullish(),});export const getPartnerEarningsQuerySchema = getCommissionsQuerySchema@@ -83,6 +86,29 @@ export const getPartnerEarningsTimeseriesSchema =timezone: z.string().optional(),});+export const createPartnerProfileLinkSchema = z.object({+ url: parseUrlSchema+ .nullish()+ .describe(+ "The URL to shorten (if not provided, the program's default URL will be used).",+ ),+ key: z+ .string()+ .max(190)+ .optional()+ .describe(+ "The short link slug. If not provided, a random 7-character slug will be generated.",+ ),+ partnerLinkTitle: z+ .string()+ .nullish()+ .describe("The partner's private display title for the short link."),+ partnerLinkComments: z+ .string()+ .nullish()+ .describe("The partner's private comments for the short link."),+});+export const PartnerProfileLinkSchema = LinkSchema.pick({id: true,domain: true,@@ -93,9 +119,9 @@ export const PartnerProfileLinkSchema = LinkSchema.pick({leads: true,sales: true,saleAmount: true,- partnerLinkTitle: true,- partnerLinkComments: true,}).extend({+ partnerLinkTitle: z.string().nullish().default(null),+ partnerLinkComments: z.string().nullish().default(null),createdAt: z.string().or(z.date()),partnerGroupDefaultLinkId: z.string().nullish(),discountCode: z.string().nullable().default(null),@@ -110,7 +136,18 @@ export const PartnerProfileCustomerSchema = CustomerEnrichedSchema.pick({firstSaleAt: true,subscriptionCanceledAt: true,}).extend({- activity: customerActivityResponseSchema,+ activity: customerActivityResponseSchema.extend({+ link: LinkSchema.pick({+ id: true,+ domain: true,+ key: true,+ shortLink: true,+ })+ .extend({+ partnerLinkTitle: z.string().nullish(),+ })+ .nullish(),+ }),});export const partnerProfileAnalyticsQuerySchema = analyticsQuerySchema.omit({diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.tsindex f3ed67d33d..b7ffc40429 100644--- a/apps/web/lib/zod/schemas/partners.ts+++ b/apps/web/lib/zod/schemas/partners.ts@@ -815,14 +815,6 @@ export const createPartnerLinkSchema = partnerIdTenantIdSchema"The short link slug. If not provided, a random 7-character slug will be generated.",),comments: z.string().nullish().describe("The comments for the short link."),- partnerLinkTitle: z- .string()- .nullish()- .describe("The partner's private display title for the short link."),- partnerLinkComments: z- .string()- .nullish()- .describe("The partner's private comments for the short link."),}).extend(createPartnerSchema.pick({diff --git a/apps/web/lib/zod/schemas/programs.ts b/apps/web/lib/zod/schemas/programs.tsindex 0c1a5d1076..e0b4eb7e46 100644--- a/apps/web/lib/zod/schemas/programs.ts+++ b/apps/web/lib/zod/schemas/programs.ts@@ -137,6 +137,15 @@ export const ProgramEnrollmentApplicationSchema = z.object({.describe("When the application was approved or rejected."),});+const partnerLinksDisplaySchema = z.object({+ displayOption: z.enum(["full", "cards"]),+ displayProperties: z.array(z.enum(["link", "title"])),+});++export const programEnrollmentPreferencesValueSchemas = {+ linksDisplay: partnerLinksDisplaySchema.nullish(),+} as const;+export const ProgramEnrollmentSchema = z.object({programId: z.string().describe("The program's unique ID on Dub."),groupId: z.string().nullish().describe("The partner's group ID on Dub."),@@ -199,6 +208,11 @@ export const ProgramEnrollmentSchema = z.object({"Linked program application, including review outcome when applicable.",),riskMonitoringDisabledAt: z.date().nullable(),+ partnerPreferences: z+ .object({+ linksDisplay: partnerLinksDisplaySchema.nullish(),+ })+ .nullish(),});export const ProgramInviteSchema = z.object({diff --git a/apps/web/prisma/schema/program.prisma b/apps/web/prisma/schema/program.prismaindex 7d88029486..08a9421c98 100644--- a/apps/web/prisma/schema/program.prisma+++ b/apps/web/prisma/schema/program.prisma@@ -147,6 +147,8 @@ model ProgramEnrollment {reapplicationTimeframe ReapplicationTimeframe @default(standard)riskMonitoringDisabledAt DateTime?+ partnerPreferences Json? // UI prefs for this enrollment (e.g. linksDisplay)+partner Partner @relation(fields: [partnerId], references: [id], onUpdate: Cascade, onDelete: Cascade)program Program @relation(fields: [programId], references: [id], onUpdate: Cascade, onDelete: Cascade)links Link[]