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.

add webook selector

Kiran K5a08b73b83b4
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/page-client.tsx
index 9d8a855717..d2c91ebaf4 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/page-client.tsx
@@ -1,37 +1,25 @@
"use client";
import { clientAccessCheck } from "@/lib/api/tokens/permissions";
+import useWebhooks from "@/lib/swr/use-webhooks";
import useWorkspace from "@/lib/swr/use-workspace";
-import { WebhookProps } from "@/lib/types";
import EmptyState from "@/ui/shared/empty-state";
import WebhookCard from "@/ui/webhooks/webhook-card";
import WebhookPlaceholder from "@/ui/webhooks/webhook-placeholder";
import { Button, TooltipContent } from "@dub/ui";
import { InfoTooltip } from "@dub/ui/src/tooltip";
-import { fetcher } from "@dub/utils";
import { Webhook } from "lucide-react";
import { redirect, useRouter } from "next/navigation";
-import useSWR from "swr";
export default function WebhooksPageClient() {
const router = useRouter();
- const {
- id: workspaceId,
- slug,
- plan,
- role,
- conversionEnabled,
- flags,
- } = useWorkspace();
+ const { slug, plan, role, conversionEnabled, flags } = useWorkspace();
if (!flags?.webhooks) {
redirect(`/${slug}/settings`);
}
- const { data: webhooks, isLoading } = useSWR<WebhookProps[]>(
- `/api/webhooks?workspaceId=${workspaceId}`,
- fetcher,
- );
+ const { webhooks, isLoading } = useWebhooks();
const { error: permissionsError } = clientAccessCheck({
action: "webhooks.write",
diff --git a/apps/web/lib/swr/use-webhooks.ts b/apps/web/lib/swr/use-webhooks.ts
new file mode 100644
index 0000000000..1541ce1d3a
--- /dev/null
+++ b/apps/web/lib/swr/use-webhooks.ts
@@ -0,0 +1,22 @@
+import { WebhookProps } from "@/lib/types";
+import { fetcher } from "@dub/utils";
+import useSWR from "swr";
+import useWorkspace from "./use-workspace";
+
+export default function useWebhooks() {
+ const { id } = useWorkspace();
+
+ const {
+ data: webhooks,
+ isValidating,
+ isLoading,
+ } = useSWR<WebhookProps[]>(id && `/api/webhooks?workspaceId=${id}`, fetcher, {
+ dedupingInterval: 60000,
+ });
+
+ return {
+ webhooks,
+ isLoading,
+ isValidating,
+ };
+}
diff --git a/apps/web/ui/modals/link-builder/index.tsx b/apps/web/ui/modals/link-builder/index.tsx
index c89dda6c27..f2adb32909 100644
--- a/apps/web/ui/modals/link-builder/index.tsx
+++ b/apps/web/ui/modals/link-builder/index.tsx
@@ -60,6 +60,7 @@ import { TagSelect } from "./tag-select";
import { useTargetingModal } from "./targeting-modal";
import { useMetatags } from "./use-metatags";
import { useUTMModal } from "./utm-modal";
+import { WebhookSelect } from "./webhook-select";
export const LinkModalContext = createContext<{
workspaceId?: string;
@@ -69,7 +70,9 @@ export const LinkModalContext = createContext<{
generatingMetatags: boolean;
}>({ generatingMetatags: false });
-export type LinkFormData = LinkWithTagsProps;
+export type LinkFormData = LinkWithTagsProps & {
+ webhookIds?: string[];
+};
type LinkBuilderProps = {
showLinkBuilder: boolean;
@@ -463,6 +466,7 @@ function LinkBuilderInner({
</div>
<div className="flex items-center justify-between gap-2 border-t border-gray-100 bg-gray-50 p-4">
<div className="flex min-w-0 items-center gap-2">
+ <WebhookSelect />
<UTMButton />
<div className="flex items-center gap-2 max-sm:hidden">
<ExpirationButton />
diff --git a/apps/web/ui/modals/link-builder/webhook-select.tsx b/apps/web/ui/modals/link-builder/webhook-select.tsx
new file mode 100644
index 0000000000..0936fdb76b
--- /dev/null
+++ b/apps/web/ui/modals/link-builder/webhook-select.tsx
@@ -0,0 +1,78 @@
+import useWebhooks from "@/lib/swr/use-webhooks";
+import { Combobox, useKeyboardShortcut } from "@dub/ui";
+import { cn } from "@dub/utils";
+import { Webhook } from "lucide-react";
+import { useMemo, useState } from "react";
+import { useFormContext } from "react-hook-form";
+import { LinkFormData } from ".";
+
+export function WebhookSelect() {
+ const { webhooks } = useWebhooks();
+ const [isOpen, setIsOpen] = useState(false);
+ const { watch, setValue } = useFormContext<LinkFormData>();
+ useKeyboardShortcut("w", () => setIsOpen(true), { modal: true });
+
+ const webhookIds = watch("webhookIds");
+
+ const linkLevelWebhooks = webhooks?.filter((webhook) =>
+ webhook.triggers.includes("link.clicked"),
+ );
+
+ const options = useMemo(
+ () =>
+ linkLevelWebhooks?.map((webhook) => ({
+ label: webhook.name,
+ value: webhook.id,
+ icon: <Webhook className="size-4" />,
+ })),
+ [webhooks],
+ );
+
+ const selectedWebhooks = useMemo(
+ () =>
+ webhooks
+ ?.filter((webhook) => webhookIds?.includes(webhook.id))
+ .map((webhook) => ({
+ label: webhook.name,
+ value: webhook.id,
+ icon: <Webhook className="size-4" />,
+ })) || [],
+
+ [webhookIds, webhooks],
+ );
+
+ const hasSelectedWebhooks = selectedWebhooks.length > 0;
+
+ return (
+ <Combobox
+ multiple
+ selected={selectedWebhooks || []}
+ setSelected={(webhooks) => {
+ const selectedIds = webhooks.map(({ value }) => value);
+ setValue("webhookIds", selectedIds, { shouldDirty: true });
+ }}
+ options={options}
+ icon={
+ <Webhook
+ className={cn("size-4", hasSelectedWebhooks && "text-blue-500")}
+ />
+ }
+ side="bottom"
+ placeholder="Webhooks"
+ searchPlaceholder="Search webhooks..."
+ shortcutHint="W"
+ buttonProps={{
+ className:
+ "h-9 px-2.5 w-fit font-medium text-gray-700 max-w-48 min-w-0",
+ }}
+ open={isOpen}
+ onOpenChange={setIsOpen}
+ >
+ {selectedWebhooks.length > 0
+ ? selectedWebhooks.length === 1
+ ? selectedWebhooks[0].label
+ : `${selectedWebhooks.length} Webhooks`
+ : "Webhooks"}
+ </Combobox>
+ );
+}