The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add generation for existing landing pages
Tim Wilson6ed675a85a7d
diff --git a/apps/web/lib/actions/partners/generate-lander.ts b/apps/web/lib/actions/partners/generate-lander.tsindex f6ed1242dc..64a9a441a6 100644--- a/apps/web/lib/actions/partners/generate-lander.ts+++ b/apps/web/lib/actions/partners/generate-lander.ts@@ -1,7 +1,10 @@"use server";import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";-import { programLanderSimpleSchema } from "@/lib/zod/schemas/program-lander";+import {+ programLanderSchema,+ programLanderSimpleSchema,+} from "@/lib/zod/schemas/program-lander";import { formatDiscountDescription } from "@/ui/partners/format-discount-description";import { formatRewardDescription } from "@/ui/partners/format-reward-description";import { anthropic } from "@ai-sdk/anthropic";@@ -17,14 +20,15 @@ import { authActionClient } from "../safe-action";const schema = z.object({workspaceId: z.string(),websiteUrl: z.string().url(),- landerData: programLanderSimpleSchema.optional(),+ landerData: programLanderSchema.optional(),+ prompt: z.string().optional(),});export const generateLanderAction = authActionClient.schema(schema).action(async ({ parsedInput, ctx }) => {const { workspace } = ctx;- const { websiteUrl, landerData } = parsedInput;+ const { websiteUrl, landerData, prompt } = parsedInput;const programId = getDefaultProgramIdOrThrow(workspace);const program = await getProgramOrThrow(@@ -70,18 +74,23 @@ export const generateLanderAction = authActionClientconst { object } = await generateObject({model: anthropic("claude-sonnet-4-20250514"),- schema: programLanderSimpleSchema,+ schema: landerData ? programLanderSchema : programLanderSimpleSchema,prompt:// Instructions`Generate a basic landing page for an affiliate program powered by Dub Partners based on a company website. ` +`Do not include any initial header/hero content because the landing page will already have an initial title and subtitle. ` +`Do not make any assumptions about the terms or rewards associated with the program. ` +(scrapeResult.metadata?.ogImage- ? `You may include an image block in the landing page, only using the OG image here: ${scrapeResult.metadata?.ogImage}. `+ ? `You ${landerData ? "could" : "may"} include an image block in the landing page, only using the OG image here: ${scrapeResult.metadata?.ogImage}. `: "") +- `If you have product pricing information, include an earnings calculator block, using the highest non-enterprise tier for the product price. ` ++ `Do not add any file blocks. ` ++ `If you have product pricing information, ${landerData ? "you could" : "you should"} include an earnings calculator block, using the highest non-enterprise tier for the product price. ` +`Markdown is supported in "text" blocks, but use it sparingly. ` +`Avoid using links. Relevant CTA links are already on the landing page. ` ++ // Additional instructions+ (prompt+ ? `\n\nAdditional instructions are provided by the user. If they specify a specific action, do not do anything more than that action: "${prompt}"`+ : "") +// Program details`\n\nProgram details:` +`\n\nName: ${program.name}\n` +@@ -94,6 +103,12 @@ export const generateLanderAction = authActionClient`Dub Partners enables businesses to create scalable referral and affiliate programs to drive revenue through incentivized user and partner networks. ` +`With Dub Partners, you can build powerful, scalable referral and affiliate programs with 1-click global payouts and white-labeling functionality. ` +`You may reference Dub Partners in the landing page, but do not focus on it or devote any full section of the page to it.` ++ // Existing page+ (landerData+ ? `\n\nThis landing page already has existing content. DO NOT update the existing content, only add new content (unless otherwise directed). ` ++ `Absolutely do not update file or image blocks, just maintain them. Existing content:` ++ `\n${JSON.stringify(landerData, null, 2)}`+ : "") +// Website content`\n\nCompany website to base the landing page on:\n\n${mainPageMarkdown}` +(pricingPageMarkdown@@ -102,7 +117,7 @@ export const generateLanderAction = authActionClienttemperature: 0.4,});- return object;+ return programLanderSchema.parse(object);});function cleanMarkdown(markdown: string) {diff --git a/apps/web/ui/partners/design/lander-ai-banner.tsx b/apps/web/ui/partners/design/lander-ai-banner.tsxindex 1a3989e120..52ce8a06ca 100644--- a/apps/web/ui/partners/design/lander-ai-banner.tsx+++ b/apps/web/ui/partners/design/lander-ai-banner.tsx@@ -107,7 +107,7 @@ export function LanderAIBanner() {const data = programLanderSimpleSchema.parse(result?.data);if (!data.blocks.length) throw new Error("No blocks generated");- setValue("landerData", data, { shouldDirty: true });+ setValue("landerData.blocks", data.blocks, { shouldDirty: true });} catch (e) {console.error("Error generating program lander", e);toast.error(diff --git a/apps/web/ui/partners/design/lander-preview-controls.tsx b/apps/web/ui/partners/design/lander-preview-controls.tsxnew file mode 100644index 0000000000..46c828ac6e--- /dev/null+++ b/apps/web/ui/partners/design/lander-preview-controls.tsx@@ -0,0 +1,103 @@+"use client";++import { generateLanderAction } from "@/lib/actions/partners/generate-lander";+import useWorkspace from "@/lib/swr/use-workspace";+import { programLanderSchema } from "@/lib/zod/schemas/program-lander";+import { Button, LoadingSpinner, Sparkle3 } from "@dub/ui";+import { cn } from "@dub/utils";+import { useAction } from "next-safe-action/hooks";+import { useState } from "react";+import { useWatch } from "react-hook-form";+import { toast } from "sonner";+import { useBrandingContext } from "./branding-context-provider";+import { useBrandingFormContext } from "./branding-form";+import { GenerateLanderModal } from "./modals/generate-lander-modal";++export function LanderPreviewControls() {+ const { id: workspaceId } = useWorkspace();++ const { setValue, getValues } = useBrandingFormContext();+ const { landerData } = {+ ...useWatch(),+ ...getValues(),+ };++ const { isGeneratingLander, setIsGeneratingLander, isGenerateBannerHidden } =+ useBrandingContext();++ const [showGenerateLanderModal, setShowGenerateLanderModal] = useState(false);++ const { executeAsync } = useAction(generateLanderAction, {+ async onSuccess() {+ toast.success("Landing page updated.");+ },+ onError({ error }) {+ console.error(error);+ },+ });++ const showGenerateButton =+ isGenerateBannerHidden || landerData?.blocks.length !== 0;++ return (+ <>+ <div+ className={cn(+ "pointer-events-none w-0 translate-y-1 overflow-hidden opacity-0 transition-[opacity,transform]",+ showGenerateButton &&+ "pointer-events-auto w-auto translate-y-0 opacity-100",+ )}+ {...{ inert: showGenerateButton ? undefined : "" }}+ >+ <Button+ type="button"+ variant="success"+ text={+ <div className="flex items-center gap-1">+ Generate+ {isGeneratingLander ? (+ <LoadingSpinner className="size-3" />+ ) : (+ <Sparkle3 className="size-3" />+ )}+ </div>+ }+ disabled={isGeneratingLander}+ className="animate-fade-in h-7 w-fit px-2 hover:ring-0"+ onClick={() => setShowGenerateLanderModal(true)}+ />+ </div>+ {(showGenerateButton || showGenerateLanderModal) && (+ <GenerateLanderModal+ showGenerateLanderModal={showGenerateLanderModal}+ setShowGenerateLanderModal={setShowGenerateLanderModal}+ landerData={landerData}+ onGenerate={async ({ websiteUrl, prompt }) => {+ setIsGeneratingLander(true);++ const result = await executeAsync({+ workspaceId: workspaceId!,+ websiteUrl,+ prompt,+ landerData,+ });++ try {+ const data = programLanderSchema.parse(result?.data);+ if (!data.blocks.length) throw new Error("No blocks generated");++ setValue("landerData.blocks", data.blocks, { shouldDirty: true });+ } catch (e) {+ console.error("Error generating program lander content", e);+ toast.error(+ "Failed to generate landing page content. Please try again later.",+ );+ } finally {+ setIsGeneratingLander(false);+ }+ }}+ />+ )}+ </>+ );+}diff --git a/apps/web/ui/partners/design/modals/generate-lander-modal.tsx b/apps/web/ui/partners/design/modals/generate-lander-modal.tsxindex f7d81eeb84..b25976315d 100644--- a/apps/web/ui/partners/design/modals/generate-lander-modal.tsx+++ b/apps/web/ui/partners/design/modals/generate-lander-modal.tsx@@ -1,12 +1,19 @@import useProgram from "@/lib/swr/use-program";-import { Button, Modal, useMediaQuery } from "@dub/ui";+import { ProgramLanderData } from "@/lib/types";+import { Button, Modal, useEnterSubmit, useMediaQuery } from "@dub/ui";import { Dispatch, SetStateAction } from "react";import { useForm } from "react-hook-form";+type GenerateLanderFormData = {+ websiteUrl: string;+ prompt?: string;+};+type GenerateLanderModalProps = {showGenerateLanderModal: boolean;setShowGenerateLanderModal: Dispatch<SetStateAction<boolean>>;- onGenerate: (data: { websiteUrl: string }) => void;+ onGenerate: (data: GenerateLanderFormData) => void;+ landerData?: ProgramLanderData;};export function GenerateLanderModal(props: GenerateLanderModalProps) {@@ -20,7 +27,11 @@ export function GenerateLanderModal(props: GenerateLanderModalProps) {);}-function GenerateLanderModalInner({ setShowGenerateLanderModal, onGenerate }) {+function GenerateLanderModalInner({+ setShowGenerateLanderModal,+ onGenerate,+ landerData,+}: GenerateLanderModalProps) {const { isMobile } = useMediaQuery();const { program } = useProgram();@@ -28,30 +39,38 @@ function GenerateLanderModalInner({ setShowGenerateLanderModal, onGenerate }) {register,handleSubmit,formState: { isSubmitting, isSubmitSuccessful },- } = useForm<{ websiteUrl: string }>({+ } = useForm<GenerateLanderFormData>({defaultValues: {websiteUrl: program?.url ?? "",+ prompt: "",},});+ const { handleKeyDown } = useEnterSubmit();++ const updating = !!landerData?.blocks.length;+return (<><formclassName="p-4 pt-3"onSubmit={(e) => {e.stopPropagation();- handleSubmit(async ({ websiteUrl }) => {+ handleSubmit(async ({ websiteUrl, prompt }) => {setShowGenerateLanderModal(false);- onGenerate({ websiteUrl });+ onGenerate({ websiteUrl, prompt });})(e);}}><h3 className="text-base font-semibold leading-6 text-neutral-800">- Generate a new landing page+ {updating+ ? "Generate landing page content"+ : "Generate a new landing page"}</h3><p className="text-content-subtle mt-2 text-sm">- We'll use AI to generate a new landing page for your program, based on- content from your own website.+ {updating+ ? "We'll use AI to update your program's landing page, based on content from your own website."+ : "We'll use AI to generate a new landing page for your program, based on content from your own website."}</p><div className="mt-4 flex flex-col gap-6">@@ -70,6 +89,24 @@ function GenerateLanderModalInner({ setShowGenerateLanderModal, onGenerate }) {/></div></label>++ {/* Instructions */}+ {updating && (+ <label>+ <span className="block text-sm font-medium text-neutral-700">+ Instructions (optional)+ </span>+ <div className="mt-2 rounded-md shadow-sm">+ <textarea+ placeholder="Any additional instructions for the AI"+ rows={2}+ className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"+ onKeyDown={handleKeyDown}+ {...register("prompt")}+ />+ </div>+ </label>+ )}</div><div className="mt-4 flex items-center justify-end gap-2">diff --git a/apps/web/ui/partners/design/previews/lander-preview.tsx b/apps/web/ui/partners/design/previews/lander-preview.tsxindex 35fc4fcb4f..9ae70885fc 100644--- a/apps/web/ui/partners/design/previews/lander-preview.tsx+++ b/apps/web/ui/partners/design/previews/lander-preview.tsx@@ -12,7 +12,6 @@ import {LoadingSpinner,Pen2,Plus2,- Sparkle3,Tooltip,Trash,useMediaQuery,@@ -33,6 +32,7 @@ import { useWatch } from "react-hook-form";import { useBrandingContext } from "../branding-context-provider";import { useBrandingFormContext } from "../branding-form";import { LanderAIBanner } from "../lander-ai-banner";+import { LanderPreviewControls } from "../lander-preview-controls";import { AddBlockModal, DESIGNER_BLOCKS } from "../modals/add-block-modal";import { useEditRewardsModal } from "../modals/edit-rewards-modal";import { RewardsDiscountsPreview } from "../rewards-discounts-preview";@@ -47,8 +47,7 @@ export function LanderPreview({const scrollRef = useRef<HTMLDivElement>(null);const scrolled = useScroll(0, { container: scrollRef });- let { isGeneratingLander, isGenerateBannerHidden } = useBrandingContext();- // isGeneratingLander = true; // TODO+ const { isGeneratingLander } = useBrandingContext();const { setValue, getValues } = useBrandingFormContext();const { landerData, brandColor, logo, wordmark } = {@@ -94,9 +93,6 @@ export function LanderPreview({string | "hero" | "rewards" | null>(null);- const showGenerateButton =- isGenerateBannerHidden || landerData?.blocks.length !== 0;-return (<>{editingBlock && editingBlockMeta && (@@ -130,29 +126,7 @@ export function LanderPreview({<PreviewWindowurl={`${PARTNERS_DOMAIN}/${program?.slug}`}scrollRef={scrollRef}- controls={- <div- className={cn(- "pointer-events-none w-0 translate-y-1 overflow-hidden opacity-0 transition-[opacity,transform]",- showGenerateButton &&- "pointer-events-auto w-auto translate-y-0 opacity-100",- )}- {...{ inert: showGenerateButton ? undefined : "" }}- >- <Button- type="button"- variant="secondary"- text={- <div className="flex items-center gap-1">- Generate- <Sparkle3 className="size-3" />- </div>- }- className="animate-fade-in h-7 w-fit px-2"- onClick={() => alert("wip")}- />- </div>- }+ controls={<LanderPreviewControls />}overlay={<divclassName={cn(