The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Rename webhook scope to linkTarget and refactor link.clicked plumbing
Kiran K98d938c444c8
diff --git a/apps/web/app/api/webhooks/[webhookId]/folders/route.ts b/apps/web/app/api/webhooks/[webhookId]/folders/route.tsindex fdeded846c..0811b789ee 100644--- a/apps/web/app/api/webhooks/[webhookId]/folders/route.ts+++ b/apps/web/app/api/webhooks/[webhookId]/folders/route.ts@@ -23,7 +23,7 @@ export const GET = withWorkspace(async ({ workspace, params }) => {select: {folderId: true,},- take: 100, // TODO: Enforce this limit other places too+ take: 1000,});return NextResponse.json(folders.map(({ folderId }) => folderId));diff --git a/apps/web/app/api/webhooks/[webhookId]/links/route.ts b/apps/web/app/api/webhooks/[webhookId]/links/route.tsindex 6be8e55441..132755409d 100644--- a/apps/web/app/api/webhooks/[webhookId]/links/route.ts+++ b/apps/web/app/api/webhooks/[webhookId]/links/route.ts@@ -13,16 +13,15 @@ export const GET = withWorkspace(async ({ workspace, params }) => {},});- const links = await prisma.link.findMany({+ const links = await prisma.linkWebhook.findMany({where: {- webhooks: {- some: {- webhookId: webhook.id,- },- },+ webhookId: webhook.id,+ },+ select: {+ linkId: true,},take: 1000,});- return NextResponse.json(links.map((link) => link.id));+ return NextResponse.json(links.map((link) => link.linkId));});diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.tsindex 45a146f3db..b086283425 100644--- a/apps/web/app/api/webhooks/[webhookId]/route.ts+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts@@ -52,12 +52,20 @@ export const PATCH = withWorkspace(projectId: workspace.id,},include: {- links: true,- folders: true,+ links: {+ select: {+ linkId: true,+ },+ },+ folders: {+ select: {+ folderId: true,+ },+ },},});- const { name, url, triggers, scope, linkIds, folderIds } = input;+ const { name, url, triggers, linkTarget, linkIds, folderIds } = input;// If the webhook is managed by an integration, only the linkIds & triggers can be updated manually.if (existingWebhook.installationId && (name || url)) {@@ -76,17 +84,17 @@ export const PATCH = withWorkspace(LINK_CLICK_WEBHOOK_TRIGGER,);- const nextScope = hasLinkClickedTrigger- ? scope !== undefined- ? scope- : existingWebhook.scope+ const nextLinkTarget = hasLinkClickedTrigger+ ? linkTarget !== undefined+ ? linkTarget+ : existingWebhook.linkTarget: null;const nextWebhook: Partial<NewWebhook> = {...existingWebhook,...input,triggers: finalTriggers,- scope: nextScope,+ linkTarget: nextLinkTarget,};await validateWebhook({@@ -96,22 +104,22 @@ export const PATCH = withWorkspace(user: session.user,});- const existingLinkIds = existingWebhook.links.map((link) => link.linkId);+ const existingLinkIds = existingWebhook.links.map(({ linkId }) => linkId);const existingFolderIds = existingWebhook.folders.map(- (folder) => folder.folderId,+ ({ folderId }) => folderId,);- const scopeChanged = nextScope !== existingWebhook.scope;+ const linkTargetChanged = nextLinkTarget !== existingWebhook.linkTarget;const shouldSyncLinks =- (nextWebhook.scope !== "links" && existingLinkIds.length > 0) ||- (nextWebhook.scope === "links" &&+ (nextWebhook.linkTarget !== "links" && existingLinkIds.length > 0) ||+ (nextWebhook.linkTarget === "links" &&linkIds !== undefined &&!arrayEqual(existingLinkIds, linkIds ?? []));const shouldSyncFolders =- (nextWebhook.scope !== "folders" && existingFolderIds.length > 0) ||- (nextWebhook.scope === "folders" &&+ (nextWebhook.linkTarget !== "folders" && existingFolderIds.length > 0) ||+ (nextWebhook.linkTarget === "folders" &&folderIds !== undefined &&!arrayEqual(existingFolderIds, folderIds ?? []));@@ -124,7 +132,7 @@ export const PATCH = withWorkspace(...(name !== undefined && { name }),...(url !== undefined && { url }),...(triggers !== undefined && { triggers }),- ...(scopeChanged && { scope: nextWebhook.scope }),+ ...(linkTargetChanged && { linkTarget: nextWebhook.linkTarget }),},});@@ -135,7 +143,7 @@ export const PATCH = withWorkspace(},});- if (nextWebhook.scope === "links" && linkIds?.length) {+ if (nextWebhook.linkTarget === "links" && linkIds?.length) {await tx.linkWebhook.createMany({data: linkIds.map((linkId) => ({linkId,@@ -152,7 +160,7 @@ export const PATCH = withWorkspace(},});- if (nextWebhook.scope === "folders" && folderIds?.length) {+ if (nextWebhook.linkTarget === "folders" && folderIds?.length) {await tx.folderWebhook.createMany({data: folderIds.map((folderId) => ({folderId,@@ -187,7 +195,7 @@ export const DELETE = withWorkspace(async ({ workspace, params }) => {const { webhookId } = params;- const existingWebhook = await prisma.webhook.findUniqueOrThrow({+ await prisma.webhook.findUniqueOrThrow({where: {id: webhookId,projectId: workspace.id,diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.tsindex 8836a467b5..6b2be6b580 100644--- a/apps/web/app/api/webhooks/route.ts+++ b/apps/web/app/api/webhooks/route.ts@@ -49,7 +49,7 @@ export const POST = withWorkspace(user: session.user,});- const { name, url, triggers, scope, linkIds, folderIds } = input;+ const { name, url, triggers, linkTarget, linkIds, folderIds } = input;// Zapier use this endpoint to create webhooks from their appconst isZapierWebhook =@@ -72,7 +72,7 @@ export const POST = withWorkspace(url,receiver: isZapierWebhook ? WebhookReceiver.zapier : WebhookReceiver.user,triggers,- scope,+ linkTarget,linkIds,folderIds,workspace,diff --git a/apps/web/lib/integrations/common/ui/configure-webhook.tsx b/apps/web/lib/integrations/common/ui/configure-webhook.tsxindex e7d53d2dd8..9b6df7fa70 100644--- a/apps/web/lib/integrations/common/ui/configure-webhook.tsx+++ b/apps/web/lib/integrations/common/ui/configure-webhook.tsx@@ -39,7 +39,7 @@ export function ConfigureWebhook({const [triggerSelection, setTriggerSelection] =useState<WebhookTriggerSelectorValue>({triggers: [],- scope: "all",+ linkTarget: "workspace",linkIds: [],folderIds: [],});@@ -49,7 +49,7 @@ export function ConfigureWebhook({setTriggerSelection((prev) => ({...prev,triggers: webhook.triggers,- scope: webhook.scope ?? "all",+ linkTarget: webhook.linkTarget ?? "workspace",}));}}, [webhook]);@@ -64,7 +64,7 @@ export function ConfigureWebhook({setSaving(true);- const { triggers, scope, linkIds, folderIds } = triggerSelection;+ const { triggers, linkTarget, linkIds, folderIds } = triggerSelection;const hasLinkClicked = triggers.includes(LINK_CLICK_WEBHOOK_TRIGGER);const response = await fetch(@@ -77,9 +77,9 @@ export function ConfigureWebhook({body: JSON.stringify({triggers,...(hasLinkClicked && {- scope,- ...(scope === "links" && { linkIds }),- ...(scope === "folders" && { folderIds }),+ linkTarget,+ ...(linkTarget === "links" && { linkIds }),+ ...(linkTarget === "folders" && { folderIds }),}),}),},@@ -98,12 +98,12 @@ export function ConfigureWebhook({await Promise.all([mutate(`/api/webhooks/${webhookId}/links?workspaceId=${workspaceId}`,- result.scope === "links" ? linkIds : [],+ result.linkTarget === "links" ? linkIds : [],{ revalidate: true },),mutate(`/api/webhooks/${webhookId}/folders?workspaceId=${workspaceId}`,- result.scope === "folders" ? folderIds : [],+ result.linkTarget === "folders" ? folderIds : [],{ revalidate: true },),]);@@ -140,7 +140,7 @@ export function ConfigureWebhook({availableTriggers={availableTriggers}disabled={!canManageWebhook}webhookId={webhookId}- savedScope={webhook?.scope}+ savedLinkTarget={webhook?.linkTarget}/></div>diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.tsindex 797701bd3a..e6618167d3 100644--- a/apps/web/lib/tinybird/record-click.ts+++ b/apps/web/lib/tinybird/record-click.ts@@ -15,7 +15,7 @@ import { getIdentityHash } from "../middleware/utils/get-identity-hash";import { conn } from "../planetscale";import { redis } from "../upstash";import { publishPartnerActivityEvent } from "../upstash/redis-streams/partner-activity";-import { publishClickEvent } from "../upstash/redis-streams/workspace-clicks";+import { publishWorkspaceClickEvent } from "../upstash/redis-streams/workspace-click-events";import { publishWorkspaceClicksUsageEvent } from "../upstash/redis-streams/workspace-clicks-usage";import { clickEventSchemaTB } from "../zod/schemas/clicks";@@ -223,7 +223,8 @@ export async function recordClick({);}),- publishClickEvent(clickEventSchemaTB.parse(clickData)),+ // Publish the click event+ publishWorkspaceClickEvent(clickEventSchemaTB.parse(clickData)),]);// Find the rejected promises and log them@@ -237,6 +238,7 @@ export async function recordClick({"Link clicks increment","Workspace usage increment","Program enrollment totalClicks increment",+ "Workspace click event publish",];return {operation: operations[index] || `Operation ${index}`,diff --git a/apps/web/lib/upstash/redis-streams/workspace-click-events.ts b/apps/web/lib/upstash/redis-streams/workspace-click-events.tsnew file mode 100644index 0000000000..e6413f6e9c--- /dev/null+++ b/apps/web/lib/upstash/redis-streams/workspace-click-events.ts@@ -0,0 +1,42 @@+import { logger } from "@/lib/axiom/server";+import { clickWebhookWorkspaces } from "@/lib/webhook/click-webhook-workspaces";+import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";+import * as z from "zod/v4";+import { redis } from "../redis";+import { RedisStream } from "./client";++const STREAM_KEY = "workspace:click:events";++export const workspaceClickEventStream = new RedisStream(STREAM_KEY);++export const publishWorkspaceClickEvent = async (+ event: z.infer<typeof clickEventSchemaTB>,+) => {+ const { workspace_id: workspaceId } = event;++ const hasWebhooks = await clickWebhookWorkspaces.has(workspaceId);++ if (!hasWebhooks) {+ return;+ }++ try {+ return await redis.xadd(STREAM_KEY, "*", event);+ } catch (error) {+ logger.error("workspaceClickEvent.publish_failed", {+ service: "redis",+ event: "workspaceClickEvent.publish_failed",+ streamKey: STREAM_KEY,+ errorName: error instanceof Error ? error.name : undefined,+ errorStack: error instanceof Error ? error.stack : undefined,+ correlation: {+ workspaceId: event.workspace_id,+ clickId: event.click_id,+ linkId: event.link_id,+ },+ });++ await logger.flush();+ throw error;+ }+};diff --git a/apps/web/lib/upstash/redis-streams/workspace-clicks.ts b/apps/web/lib/upstash/redis-streams/workspace-clicks.tsdeleted file mode 100644index b0ceb4c365..0000000000--- a/apps/web/lib/upstash/redis-streams/workspace-clicks.ts+++ /dev/null@@ -1,30 +0,0 @@-import { clickWebhookWorkspaces } from "@/lib/webhook/click-webhook-workspaces";-import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";-import * as z from "zod/v4";-import { redis } from "../redis";-import { RedisStream } from "./client";--const STREAM_KEY = "workspace:click:events";--export const clickEventStream = new RedisStream(STREAM_KEY);--export const publishClickEvent = async (- event: z.infer<typeof clickEventSchemaTB>,-) => {- const { workspace_id: workspaceId } = event;-- const hasWebhooks = await clickWebhookWorkspaces.has(workspaceId);-- if (!hasWebhooks) {- return;- }-- try {- return await redis.xadd(STREAM_KEY, "*", event);- } catch (error) {- throw error;-- // TODO- // Log to Axiom- }-};diff --git a/apps/web/lib/webhook/constants.ts b/apps/web/lib/webhook/constants.tsindex 1cf99251ae..3bef55b543 100644--- a/apps/web/lib/webhook/constants.ts+++ b/apps/web/lib/webhook/constants.ts@@ -1,4 +1,4 @@-import { WebhookScope } from "@prisma/client";+import { LinkTarget } from "@prisma/client";import type { WebhookTrigger } from "./types";export const WEBHOOK_SECRET_LENGTH = 16;@@ -28,9 +28,9 @@ export const PROGRAM_LEVEL_WEBHOOK_TRIGGERS = ["payout.confirmed",] as const;-export const CLICK_WEBHOOK_SCOPE_OPTIONS = [+export const CLICK_WEBHOOK_LINK_TARGET_OPTIONS = [{- value: "all",+ value: "workspace",label: "All links",description: "Trigger webhooks for all links in your workspace",},@@ -45,7 +45,7 @@ export const CLICK_WEBHOOK_SCOPE_OPTIONS = [description: "Trigger webhooks for the links you explicitly select",},] as const satisfies ReadonlyArray<{- value: WebhookScope;+ value: LinkTarget;label: string;description: string;}>;diff --git a/apps/web/lib/webhook/create-webhook.ts b/apps/web/lib/webhook/create-webhook.tsindex 050d1bcc45..ab69c700cd 100644--- a/apps/web/lib/webhook/create-webhook.ts+++ b/apps/web/lib/webhook/create-webhook.ts@@ -13,7 +13,7 @@ export async function createWebhook({url,secret,triggers,- scope,+ linkTarget,linkIds,folderIds,workspace,@@ -40,11 +40,11 @@ export async function createWebhook({installationId,projectId: workspace.id,secret: secret || createWebhookSecret(),- scope,+ linkTarget,},});- if (scope === "links" && linkIds && linkIds.length > 0) {+ if (linkTarget === "links" && linkIds && linkIds.length > 0) {await prisma.linkWebhook.createMany({data: linkIds.map((linkId) => ({linkId,@@ -53,7 +53,7 @@ export async function createWebhook({});}- if (scope === "folders" && folderIds && folderIds.length > 0) {+ if (linkTarget === "folders" && folderIds && folderIds.length > 0) {await prisma.folderWebhook.createMany({data: folderIds.map((folderId) => ({folderId,diff --git a/apps/web/lib/webhook/get-webhooks.ts b/apps/web/lib/webhook/get-webhooks.tsindex 590cb0dbe6..461c98a55e 100644--- a/apps/web/lib/webhook/get-webhooks.ts+++ b/apps/web/lib/webhook/get-webhooks.ts@@ -31,7 +31,7 @@ export async function getWebhooks({url: true,secret: true,triggers: true,- scope: true,+ linkTarget: true,disabledAt: true,receiver: true,installationId: true,diff --git a/apps/web/lib/webhook/validate-webhook.ts b/apps/web/lib/webhook/validate-webhook.tsindex 789b64358c..51640d46c6 100644--- a/apps/web/lib/webhook/validate-webhook.ts+++ b/apps/web/lib/webhook/validate-webhook.ts@@ -19,7 +19,7 @@ export async function validateWebhook({webhook?: Webhook;user: Session["user"];}) {- const { url, scope, linkIds, folderIds, triggers } = input;+ const { url, linkTarget, linkIds, folderIds, triggers } = input;// payout.confirmed trigger requires external payouts enabledif (triggers && triggers.includes("payout.confirmed")) {@@ -64,7 +64,7 @@ export async function validateWebhook({}}- if (scope === "links" && linkIds && linkIds.length > 0) {+ if (linkTarget === "links" && linkIds && linkIds.length > 0) {const folders = await getFolders({workspaceId: workspace.id,userId: user.id,@@ -95,7 +95,7 @@ export async function validateWebhook({}}- if (scope === "folders" && folderIds && folderIds.length > 0) {+ if (linkTarget === "folders" && folderIds && folderIds.length > 0) {const folders = await getFolders({workspaceId: workspace.id,userId: user.id,diff --git a/apps/web/lib/zod/schemas/webhooks.ts b/apps/web/lib/zod/schemas/webhooks.tsindex b1a486aa47..723f2d9cb6 100644--- a/apps/web/lib/zod/schemas/webhooks.ts+++ b/apps/web/lib/zod/schemas/webhooks.ts@@ -1,6 +1,6 @@import { WEBHOOK_TRIGGERS } from "@/lib/webhook/constants";import { WebhookTrigger } from "@/lib/webhook/types";-import { WebhookScope } from "@prisma/client";+import { LinkTarget } from "@prisma/client";import * as z from "zod/v4";import { parseUrlSchema } from "./utils";@@ -10,7 +10,7 @@ export const WebhookSchema = z.object({url: z.string(),secret: z.string(),triggers: z.array(z.enum(WEBHOOK_TRIGGERS)),- scope: z.enum(WebhookScope).nullable(),+ linkTarget: z.enum(LinkTarget).nullable(),disabledAt: z.date().nullable(),installationId: z.string().nullable(),});@@ -18,7 +18,7 @@ export const WebhookSchema = z.object({const validateWebhook = (data: {triggers: WebhookTrigger[];- scope?: WebhookScope | null;+ linkTarget?: LinkTarget | null;linkIds?: string[] | null;folderIds?: string[] | null;},@@ -30,26 +30,26 @@ const validateWebhook = (}if (!data.triggers.includes("link.clicked")) {- if (data.scope) {+ if (data.linkTarget) {ctx.addIssue({code: "custom",- path: ["scope"],- message: `"scope" can only be set when the "link.clicked" trigger is enabled.`,+ path: ["linkTarget"],+ message: `"linkTarget" can only be set when the "link.clicked" trigger is enabled.`,});}return;}- if (!data.scope) {+ if (!data.linkTarget) {ctx.addIssue({code: "custom",- path: ["scope"],- message: `"scope" is required when the "link.clicked" trigger is enabled.`,+ path: ["linkTarget"],+ message: `"linkTarget" is required when the "link.clicked" trigger is enabled.`,});return;}- if (data.scope === "links" && data.linkIds?.length === 0) {+ if (data.linkTarget === "links" && data.linkIds?.length === 0) {ctx.addIssue({code: "custom",path: ["linkIds"],@@ -57,7 +57,7 @@ const validateWebhook = (});}- if (data.scope === "folders" && data.folderIds?.length === 0) {+ if (data.linkTarget === "folders" && data.folderIds?.length === 0) {ctx.addIssue({code: "custom",path: ["folderIds"],@@ -70,7 +70,7 @@ export const createWebhookSchemaBase = z.object({name: z.string().min(1).max(40),url: parseUrlSchema,triggers: z.array(z.enum(WEBHOOK_TRIGGERS)),- scope: z.enum(WebhookScope).nullish(),+ linkTarget: z.enum(LinkTarget).nullish(),linkIds: z.array(z.string()).nullish(),folderIds: z.array(z.string()).nullish(),});diff --git a/apps/web/prisma/schema/webhook.prisma b/apps/web/prisma/schema/webhook.prismaindex 474a9223a4..7550cdf9a1 100644--- a/apps/web/prisma/schema/webhook.prisma+++ b/apps/web/prisma/schema/webhook.prisma@@ -6,8 +6,8 @@ enum WebhookReceiver {segment}-enum WebhookScope {- all // All links in the workspace+enum LinkTarget {+ workspace // All links in the workspacefolders // Links inside specific folderslinks // Specific links}@@ -21,7 +21,7 @@ model Webhook {url String @db.LongTextsecret Stringtriggers Json- scope WebhookScope? // Only set for webhooks with the link.clicked trigger+ linkTarget LinkTarget? // Only set for webhooks with the link.clicked triggerconsecutiveFailures Int @default(0)lastFailedAt DateTime?disabledAt DateTime?diff --git a/apps/web/scripts/migrations/backfill-webhook-link-scope.ts b/apps/web/scripts/migrations/backfill-webhook-link-scope.tsindex 9c9c0d2676..12f87a6e98 100644--- a/apps/web/scripts/migrations/backfill-webhook-link-scope.ts+++ b/apps/web/scripts/migrations/backfill-webhook-link-scope.ts@@ -1,19 +1,19 @@import { prisma } from "@/lib/prisma";import "dotenv-flow/config";-// Existing link.clicked webhooks are always scoped to specific links,-// so backfill their linkScope to "links".+// Existing link.clicked webhooks are always targeted to specific links,+// so backfill their linkTarget to "links".async function main() {const { count } = await prisma.webhook.updateMany({where: {- scope: null,+ linkTarget: null,triggers: {array_contains: ["link.clicked"],},},data: {- scope: "links",+ linkTarget: "links",},});diff --git a/apps/web/ui/webhooks/add-edit-webhook-form.tsx b/apps/web/ui/webhooks/add-edit-webhook-form.tsxindex b885a96002..39b5dfdf73 100644--- a/apps/web/ui/webhooks/add-edit-webhook-form.tsx+++ b/apps/web/ui/webhooks/add-edit-webhook-form.tsx@@ -39,7 +39,7 @@ export default function AddEditWebhookForm({name: webhook?.name ?? "",url: webhook?.url ?? "",triggers: webhook?.triggers ?? [],- scope: webhook?.scope ?? "all",+ linkTarget: webhook?.linkTarget ?? "workspace",linkIds: [],folderIds: [],},@@ -54,11 +54,11 @@ export default function AddEditWebhookForm({formState: { isSubmitting },} = methods;- const [name, url, triggers, scope, linkIds, folderIds] = watch([+ const [name, url, triggers, linkTarget, linkIds, folderIds] = watch(["name","url","triggers",- "scope",+ "linkTarget","linkIds","folderIds",]);@@ -97,9 +97,9 @@ export default function AddEditWebhookForm({url: data.url,triggers: data.triggers,...(hasLinkClicked && {- scope: data.scope,- ...(data.scope === "links" && { linkIds: data.linkIds }),- ...(data.scope === "folders" && { folderIds: data.folderIds }),+ linkTarget: data.linkTarget,+ ...(data.linkTarget === "links" && { linkIds: data.linkIds }),+ ...(data.linkTarget === "folders" && { folderIds: data.folderIds }),}),}),});@@ -120,12 +120,12 @@ export default function AddEditWebhookForm({await Promise.all([mutate(`/api/webhooks/${result.id}/links?workspaceId=${workspaceId}`,- result.scope === "links" ? data.linkIds ?? [] : [],+ result.linkTarget === "links" ? data.linkIds ?? [] : [],{ revalidate: true },),mutate(`/api/webhooks/${result.id}/folders?workspaceId=${workspaceId}`,- result.scope === "folders" ? data.folderIds ?? [] : [],+ result.linkTarget === "folders" ? data.folderIds ?? [] : [],{ revalidate: true },),]);@@ -134,24 +134,24 @@ export default function AddEditWebhookForm({name: result.name,url: result.url,triggers: result.triggers,- scope: result.scope ?? "all",- linkIds: result.scope === "links" ? data.linkIds ?? [] : [],- folderIds: result.scope === "folders" ? data.folderIds ?? [] : [],+ linkTarget: result.linkTarget ?? "workspace",+ linkIds: result.linkTarget === "links" ? data.linkIds ?? [] : [],+ folderIds: result.linkTarget === "folders" ? data.folderIds ?? [] : [],});}toast.success(endpoint.successMessage);};- const scopeInvalid = isWebhookTriggerSelectionInvalid({+ const linkTargetInvalid = isWebhookTriggerSelectionInvalid({triggers,- scope: scope ?? "all",+ linkTarget: linkTarget ?? "workspace",linkIds: linkIds ?? [],folderIds: folderIds ?? [],});const buttonDisabled =- !name || !url || !triggers.length || isSubmitting || scopeInvalid;+ !name || !url || !triggers.length || isSubmitting || linkTargetInvalid;const updateDisabled =(webhook && webhook?.installationId !== null) || permissionsError !== false;@@ -256,20 +256,20 @@ export default function AddEditWebhookForm({<WebhookTriggerSelectorvalue={{triggers,- scope: scope ?? "all",+ linkTarget: linkTarget ?? "workspace",linkIds: linkIds ?? [],folderIds: folderIds ?? [],}}onChange={(next) => {setValue("triggers", next.triggers);- setValue("scope", next.scope);+ setValue("linkTarget", next.linkTarget);setValue("linkIds", next.linkIds);setValue("folderIds", next.folderIds);}}availableTriggers={allWebhookTriggers}disabled={updateDisabled}webhookId={webhook?.id}- savedScope={webhook?.scope}+ savedLinkTarget={webhook?.linkTarget}/></div></div>diff --git a/apps/web/ui/webhooks/webhook-trigger-selector.tsx b/apps/web/ui/webhooks/webhook-trigger-selector.tsxindex f8cdec53a8..9dcbca6494 100644--- a/apps/web/ui/webhooks/webhook-trigger-selector.tsx+++ b/apps/web/ui/webhooks/webhook-trigger-selector.tsx@@ -2,14 +2,14 @@import useWorkspace from "@/lib/swr/use-workspace";import {- CLICK_WEBHOOK_SCOPE_OPTIONS,+ CLICK_WEBHOOK_LINK_TARGET_OPTIONS,LINK_CLICK_WEBHOOK_TRIGGER,WEBHOOK_TRIGGER_DESCRIPTIONS,} from "@/lib/webhook/constants";import type { WebhookTrigger } from "@/lib/webhook/types";import { Checkbox, RadioGroup, RadioGroupItem } from "@dub/ui";import { arrayEqual, cn, fetcher } from "@dub/utils";-import { WebhookScope } from "@prisma/client";+import { LinkTarget } from "@prisma/client";import { useEffect } from "react";import useSWR from "swr";import { FoldersSelector } from "./folder-selector";@@ -17,7 +17,7 @@ import { LinksSelector } from "./link-selector";export type WebhookTriggerSelectorValue = {triggers: WebhookTrigger[];- scope: WebhookScope;+ linkTarget: LinkTarget;linkIds: string[];folderIds: string[];};@@ -32,8 +32,8 @@ export function isWebhookTriggerSelectionInvalid(}return (- (value.scope === "links" && value.linkIds.length === 0) ||- (value.scope === "folders" && value.folderIds.length === 0)+ (value.linkTarget === "links" && value.linkIds.length === 0) ||+ (value.linkTarget === "folders" && value.folderIds.length === 0));}@@ -43,77 +43,77 @@ export function WebhookTriggerSelector({availableTriggers,disabled,webhookId,- savedScope,+ savedLinkTarget,}: {value: WebhookTriggerSelectorValue;onChange: (value: WebhookTriggerSelectorValue) => void;availableTriggers: WebhookTrigger[];disabled?: boolean;webhookId?: string;- savedScope?: WebhookScope | null;+ savedLinkTarget?: LinkTarget | null;}) {const { id: workspaceId } = useWorkspace();- const { triggers, scope, linkIds, folderIds } = value;+ const { triggers, linkTarget, linkIds, folderIds } = value;const hasLinkClicked = triggers.includes(LINK_CLICK_WEBHOOK_TRIGGER);const { data: fetchedLinkIds } = useSWR<string[]>(- webhookId && savedScope === "links"+ webhookId && savedLinkTarget === "links"? `/api/webhooks/${webhookId}/links?workspaceId=${workspaceId}`: null,fetcher,);const { data: fetchedFolderIds } = useSWR<string[]>(- webhookId && savedScope === "folders"+ webhookId && savedLinkTarget === "folders"? `/api/webhooks/${webhookId}/folders?workspaceId=${workspaceId}`: null,fetcher,);useEffect(() => {- if (scope === "links" && fetchedLinkIds) {+ if (linkTarget === "links" && fetchedLinkIds) {if (!arrayEqual(linkIds, fetchedLinkIds)) {onChange({triggers,- scope,+ linkTarget,linkIds: fetchedLinkIds,folderIds,});}- } else if (scope !== "links" && linkIds.length > 0) {+ } else if (linkTarget !== "links" && linkIds.length > 0) {onChange({triggers,- scope,+ linkTarget,linkIds: [],folderIds,});}- // Only sync from server when scope or fetched ids change — not on manual selection+ // Only sync from server when linkTarget or fetched ids change — not on manual selection// eslint-disable-next-line react-hooks/exhaustive-deps- }, [scope, fetchedLinkIds]);+ }, [linkTarget, fetchedLinkIds]);useEffect(() => {- if (scope === "folders" && fetchedFolderIds) {+ if (linkTarget === "folders" && fetchedFolderIds) {if (!arrayEqual(folderIds, fetchedFolderIds)) {onChange({triggers,- scope,+ linkTarget,linkIds,folderIds: fetchedFolderIds,});}- } else if (scope !== "folders" && folderIds.length > 0) {+ } else if (linkTarget !== "folders" && folderIds.length > 0) {onChange({triggers,- scope,+ linkTarget,linkIds,folderIds: [],});}- // Only sync from server when scope or fetched ids change — not on manual selection+ // Only sync from server when linkTarget or fetched ids change — not on manual selection// eslint-disable-next-line react-hooks/exhaustive-deps- }, [scope, fetchedFolderIds]);+ }, [linkTarget, fetchedFolderIds]);const toggleTrigger = (trigger: WebhookTrigger,@@ -131,12 +131,12 @@ export function WebhookTriggerSelector({});};- const handleScopeChange = (nextScope: WebhookScope) => {+ const handleLinkTargetChange = (nextLinkTarget: LinkTarget) => {onChange({...value,- scope: nextScope,- linkIds: nextScope === "links" ? linkIds : [],- folderIds: nextScope === "folders" ? folderIds : [],+ linkTarget: nextLinkTarget,+ linkIds: nextLinkTarget === "links" ? linkIds : [],+ folderIds: nextLinkTarget === "folders" ? folderIds : [],});};@@ -163,15 +163,15 @@ export function WebhookTriggerSelector({{trigger === LINK_CLICK_WEBHOOK_TRIGGER && hasLinkClicked ? (<div className="ml-6 mt-3 rounded-md border border-neutral-200 bg-neutral-50 p-4"><RadioGroup- value={scope}+ value={linkTarget}onValueChange={(nextValue) =>- handleScopeChange(nextValue as WebhookScope)+ handleLinkTargetChange(nextValue as LinkTarget)}className="flex flex-col gap-2"disabled={disabled}>- {CLICK_WEBHOOK_SCOPE_OPTIONS.map((option) => {- const isSelected = scope === option.value;+ {CLICK_WEBHOOK_LINK_TARGET_OPTIONS.map((option) => {+ const isSelected = linkTarget === option.value;return (<div key={option.value}>@@ -186,7 +186,7 @@ export function WebhookTriggerSelector({><RadioGroupItemvalue={option.value}- id={`scope-${option.value}`}+ id={`linkTarget-${option.value}`}className="mt-0.5"disabled={disabled}/>