The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Merge branch 'main' into wh-cache-update
Kiran K6755dd43f311
diff --cc apps/web/lib/tinybird/record-click.tsindex 7212bdf3f8,ad21cf01e1..0810b18013--- a/apps/web/lib/tinybird/record-click.ts+++ b/apps/web/lib/tinybird/record-click.ts@@@ -151,71 -154,33 +159,71 @@@ export async function recordClick("UPDATE Project p JOIN Link l ON p.id = l.projectId SET p.usage = p.usage + 1 WHERE l.id = ?",[linkId],),++ // fetch the workspace usage for the workspace+ workspaceId && hasWebhooks+ ? conn.execute(+ "SELECT usage, usageLimit FROM Project WHERE id = ? LIMIT 1",+ [workspaceId],+ )+ : null,]);- // Send webhook events if link has webhooks enabled- if (webhookIds && webhookIds.length > 0) {- const webhooks = await webhookCache.mget(webhookIds);+ const workspace =+ workspaceRows && workspaceRows.rows.length > 0+ ? (workspaceRows.rows[0] as Pick<WorkspaceProps, "usage" | "usageLimit">)+ : null;++ const hasExceededUsageLimit =+ workspace && workspace.usage >= workspace.usageLimit;- const linkWebhooks = webhooks.filter(- (webhook) =>- webhook.triggers &&- Array.isArray(webhook.triggers) &&- webhook.triggers.includes("link.clicked"),+ // Send webhook events if link has webhooks enabled and the workspace usage has not exceeded the limit+ if (hasWebhooks && !hasExceededUsageLimit) {+ await sendLinkClickWebhooks({ webhookIds, linkId, clickData });+ }+}++async function sendLinkClickWebhooks({+ webhookIds,+ linkId,+ clickData,+}: {+ webhookIds: string[];+ linkId: string;+ clickData: any;+}) {+ const webhooks = await webhookCache.mget(webhookIds);++ // Couldn't find webhooks in the cache+ // TODO: Should we look them up in the database?+ if (!webhooks || webhooks.length === 0) {+ return;+ }++ const activeLinkWebhooks = webhooks.filter((webhook) => {+ return (+ !webhook.disabledAt &&+ webhook.triggers &&+ Array.isArray(webhook.triggers) &&+ webhook.triggers.includes("link.clicked"));+ });- if (linkWebhooks.length > 0) {- const link = await conn- .execute("SELECT * FROM Link WHERE id = ?", [linkId])- .then((res) => res.rows[0]);-- await sendWebhooks({- trigger: "link.clicked",- webhooks: linkWebhooks,- // @ts-ignore – bot & qr should be boolean- data: transformClickEventData({- ...clickData,- link: transformLink(link as ExpandedLink),- }),- });- }+ if (activeLinkWebhooks.length === 0) {+ return;}++ const link = await conn+ .execute("SELECT * FROM Link WHERE id = ?", [linkId])+ .then((res) => res.rows[0]);++ await sendWebhooks({+ trigger: "link.clicked",+ webhooks: activeLinkWebhooks,+ // @ts-ignore – bot & qr should be boolean+ data: transformClickEventData({+ ...clickData,- link: transformLink(link as LinkWithTags),++ link: transformLink(link as ExpandedLink),+ }),+ });}diff --cc apps/web/ui/webhooks/webhook-header.tsxindex bbd45435ce,d74482b3fb..21dfc5e7ba--- a/apps/web/ui/webhooks/webhook-header.tsx+++ b/apps/web/ui/webhooks/webhook-header.tsx@@@ -13,10 -12,10 +13,11 @@@ importPopover,TabSelect,TokenAvatar,+ useCopyToClipboard,} from "@dub/ui";import { fetcher } from "@dub/utils";-import { ChevronLeft, Send, Trash } from "lucide-react";+import { ChevronLeft, CircleX, Send, Trash } from "lucide-react";+import { useAction } from "next-safe-action/hooks";import Link from "next/link";import { notFound, useRouter, useSelectedLayoutSegment } from "next/navigation";import { useState } from "react";