The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
control the display of the fom or the table
Kiran K67959c324f62
diff --git a/apps/web/app/api/embed/referrals/links/[linkId]/route.ts b/apps/web/app/api/embed/referrals/links/[linkId]/route.tsnew file mode 100644index 0000000000..9ae977a83c--- /dev/null+++ b/apps/web/app/api/embed/referrals/links/[linkId]/route.ts@@ -0,0 +1,98 @@+import { DubApiError, ErrorCodes } from "@/lib/api/errors";+import { processLink, updateLink } from "@/lib/api/links";+import { parseRequestBody } from "@/lib/api/utils";+import { withReferralsEmbedToken } from "@/lib/embed/referrals/auth";+import { LinkSchema } from "@/lib/zod/schemas/links";+import { createPartnerLinkSchema } from "@/lib/zod/schemas/partners";+import { getApexDomain } from "@dub/utils";+import { NextResponse } from "next/server";++// PATCH /api/embed/referrals/links/[linkId] - update a link for a partner+export const PATCH = withReferralsEmbedToken(+ async ({ req, params, programEnrollment, program, links }) => {+ const { url, key } = createPartnerLinkSchema+ .pick({ url: true, key: true })+ .parse(await parseRequestBody(req));++ if (programEnrollment.status === "banned") {+ throw new DubApiError({+ code: "forbidden",+ message: "You are banned from this program hence cannot create links.",+ });+ }++ const link = links.find((link) => link.id === params.linkId);++ if (!link) {+ throw new DubApiError({+ code: "not_found",+ message: "Link not found.",+ });+ }++ if (!program.domain || !program.url) {+ throw new DubApiError({+ code: "bad_request",+ message:+ "This program needs a domain and URL set before creating a link.",+ });+ }++ if (url && getApexDomain(url) !== getApexDomain(program.url)) {+ throw new DubApiError({+ code: "bad_request",+ message: `The provided URL domain (${getApexDomain(url)}) does not match the program's domain (${getApexDomain(program.url)}).`,+ });+ }++ // if domain and key are the same, we don't need to check if the key exists+ const skipKeyChecks = link.key.toLowerCase() === key?.toLowerCase();++ const {+ link: processedLink,+ error,+ code,+ } = await processLink({+ // @ts-expect-error+ payload: {+ ...link,+ key: key || undefined,+ url: url || program.url,+ },+ workspace: {+ id: program.workspaceId,+ plan: "business",+ },+ userId: link.userId!,+ skipKeyChecks,+ skipFolderChecks: true, // can't be changed by the partner+ skipProgramChecks: true, // can't be changed by the partner+ skipExternalIdChecks: true, // can't be changed by the partner+ });++ if (error != null) {+ throw new DubApiError({+ code: code as ErrorCodes,+ message: error,+ });+ }++ const partnerLink = await updateLink({+ oldLink: {+ domain: link.domain,+ key: link.key,+ image: link.image,+ },+ updatedLink: processedLink,+ });++ const updatedLink = LinkSchema.pick({+ id: true,+ domain: true,+ key: true,+ url: true,+ }).parse(partnerLink);++ return NextResponse.json(updatedLink);+ },+);diff --git a/apps/web/app/api/embed/referrals/links/route.ts b/apps/web/app/api/embed/referrals/links/route.tsindex 01acbc4291..de80905bf3 100644--- a/apps/web/app/api/embed/referrals/links/route.ts+++ b/apps/web/app/api/embed/referrals/links/route.ts@@ -77,13 +77,15 @@ export const POST = withReferralsEmbedToken(});}- const partnerLink = LinkSchema.pick({+ const partnerLink = await createLink(link);++ const createdLink = LinkSchema.pick({id: true,domain: true,key: true,url: true,- }).parse(await createLink(link));+ }).parse(partnerLink);- return NextResponse.json(partnerLink, { status: 201 });+ return NextResponse.json(createdLink, { status: 201 });},);diff --git a/apps/web/app/app.dub.co/embed/referrals/create-link.tsx b/apps/web/app/app.dub.co/embed/referrals/create-update-link.tsxsimilarity index 98%rename from apps/web/app/app.dub.co/embed/referrals/create-link.tsxrename to apps/web/app/app.dub.co/embed/referrals/create-update-link.tsxindex f15f18711e..407c87a60e 100644--- a/apps/web/app/app.dub.co/embed/referrals/create-link.tsx+++ b/apps/web/app/app.dub.co/embed/referrals/create-update-link.tsx@@ -14,6 +14,7 @@ import { useForm } from "react-hook-form";interface Props {destinationDomain: string;shortLinkDomain: string;+ onCancel: () => void;}interface FormData {@@ -21,9 +22,10 @@ interface FormData {key: string;}-export function ReferralsEmbedCreateLink({+export function ReferralsEmbedCreateUpdateLink({destinationDomain,shortLinkDomain,+ onCancel,}: Props) {const { isMobile } = useMediaQuery();const [, copyToClipboard] = useCopyToClipboard();@@ -89,6 +91,7 @@ export function ReferralsEmbedCreateLink({variant="secondary"type="button"className="h-9"+ onClick={onCancel}/><Buttontext="Create link"diff --git a/apps/web/app/app.dub.co/embed/referrals/links-list.tsx b/apps/web/app/app.dub.co/embed/referrals/links-list.tsxindex 8587796ca1..0337f5cc0e 100644--- a/apps/web/app/app.dub.co/embed/referrals/links-list.tsx+++ b/apps/web/app/app.dub.co/embed/referrals/links-list.tsx@@ -5,7 +5,17 @@ import { Pen2, Plus2 } from "@dub/ui/icons";import { getPrettyUrl, TAB_ITEM_ANIMATION_SETTINGS } from "@dub/utils";import { motion } from "framer-motion";-export function ReferralsEmbedLinksList({ links }: { links: Link[] }) {+interface Props {+ links: Link[];+ onCreateLink: () => void;+ onEditLink: (link: Link) => void;+}++export function ReferralsEmbedLinksList({+ links,+ onCreateLink,+ onEditLink,+}: Props) {const { table, ...tableProps } = useTable({data: links,columns: [@@ -52,6 +62,7 @@ export function ReferralsEmbedLinksList({ links }: { links: Link[] }) {variant="primary"className="h-7 w-7 p-1.5"icon={<Plus2 className="text-white" />}+ onClick={onCreateLink}/>),minSize: 60,@@ -62,6 +73,7 @@ export function ReferralsEmbedLinksList({ links }: { links: Link[] }) {variant="secondary"className="h-7 w-7 p-1.5"icon={<Pen2 className="size-4" />}+ onClick={() => onEditLink(row.original)}/>);},diff --git a/apps/web/app/app.dub.co/embed/referrals/links.tsx b/apps/web/app/app.dub.co/embed/referrals/links.tsxindex 2e16a3500f..20e2b91203 100644--- a/apps/web/app/app.dub.co/embed/referrals/links.tsx+++ b/apps/web/app/app.dub.co/embed/referrals/links.tsx@@ -1,5 +1,6 @@import { Link } from "@dub/prisma/client";-import { ReferralsEmbedCreateLink } from "./create-link";+import { useState } from "react";+import { ReferralsEmbedCreateUpdateLink } from "./create-update-link";import { ReferralsEmbedLinksList } from "./links-list";interface Props {@@ -13,13 +14,27 @@ export default function ReferralsEmbedLinks({destinationDomain,shortLinkDomain,}: Props) {+ const [createLink, setCreateLink] = useState(false);++ const handleEditLink = (link: Link) => {+ console.log("Edit link:", link);+ };+return (<div className="flex flex-col space-y-6">- <ReferralsEmbedCreateLink- destinationDomain={destinationDomain}- shortLinkDomain={shortLinkDomain}- />- <ReferralsEmbedLinksList links={links} />+ {createLink ? (+ <ReferralsEmbedCreateUpdateLink+ destinationDomain={destinationDomain}+ shortLinkDomain={shortLinkDomain}+ onCancel={() => setCreateLink(false)}+ />+ ) : (+ <ReferralsEmbedLinksList+ links={links}+ onEditLink={handleEditLink}+ onCreateLink={() => setCreateLink(true)}+ />+ )}</div>);}