Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Merge branch 'main' into webhooks-ga

Kiran Ke9e4da9f6076
diff --cc apps/web/lib/tinybird/record-click.ts
index 7212bdf3f8,bd56bfd2df..c4cb483d71
--- a/apps/web/lib/tinybird/record-click.ts
+++ b/apps/web/lib/tinybird/record-click.ts
@@@ -151,71 -146,33 +151,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.tsx
index 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 @@@ import
Popover,
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";