The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
import { sendTestWebhookEvent } from "@/lib/actions/send-test-webhook";
import useWorkspace from "@/lib/swr/use-workspace";
import { WebhookProps } from "@/lib/types";
import type { WebhookTrigger } from "@/lib/webhook/types";
import { getAvailableWebhookTriggers } from "@/lib/webhook/utils";
import { Button, Combobox, ComboboxOption, Modal } from "@dub/ui";
import { useAction } from "next-safe-action/hooks";
import {
Dispatch,
SetStateAction,
useCallback,
useMemo,
useState,
} from "react";
import { toast } from "sonner";
function SendTestWebhookModal({
showSendTestWebhookModal,
setShowSendTestWebhookModal,
webhook,
}: {
showSendTestWebhookModal: boolean;
setShowSendTestWebhookModal: Dispatch<SetStateAction<boolean>>;
webhook: WebhookProps | undefined;
}) {
const { id: workspaceId } = useWorkspace();
const [selectedTrigger, setSelectedTrigger] = useState<ComboboxOption | null>(
null,
);
const availableTriggers = getAvailableWebhookTriggers();
const triggerOptions = availableTriggers.map((trigger) => ({
value: trigger,
label: trigger,
}));
const { execute, isPending } = useAction(sendTestWebhookEvent, {
onSuccess: () => {
toast.success("Webhook event sent.");
setShowSendTestWebhookModal(false);
},
onError: ({ error }) => {
toast.error(error.serverError);
},
});
return (
<Modal
showModal={showSendTestWebhookModal}
setShowModal={setShowSendTestWebhookModal}
>
<div className="space-y-2 border-b border-neutral-200 p-4 sm:p-6">
<h3 className="text-lg font-medium leading-none">
Send test webhook event
</h3>
</div>
<form
onSubmit={async (e) => {
e.preventDefault();
if (!selectedTrigger || !webhook) {
return;
}
execute({
workspaceId: workspaceId!,
webhookId: webhook.id,
trigger: selectedTrigger?.value as WebhookTrigger,
});
}}
>
<div className="bg-neutral-50 p-4 sm:p-6">
<p className="text-sm text-neutral-800">
Choose a webhook event to send to your receiver endpoint
</p>
<div className="mt-4">
<Combobox
options={triggerOptions}
selected={selectedTrigger}
setSelected={setSelectedTrigger}
placeholder="Select a webhook event"
matchTriggerWidth
caret
labelProps={{ className: "font-mono text-sm text-neutral-800" }}
optionClassName="font-mono"
/>
</div>
</div>
<div className="flex items-center justify-end gap-2 border-t border-neutral-200 bg-neutral-50 px-4 py-5 sm:px-6">
<Button
onClick={() => setShowSendTestWebhookModal(false)}
variant="secondary"
text="Cancel"
className="h-8 w-fit px-3"
/>
<Button
disabled={!selectedTrigger}
text="Send test webhook"
loading={isPending}
className="h-8 w-fit px-3"
/>
</div>
</form>
</Modal>
);
}
export function useSendTestWebhookModal({
webhook,
}: {
webhook: WebhookProps | undefined;
}) {
const [showSendTestWebhookModal, setShowSendTestWebhookModal] =
useState(false);
const SendTestWebhookModalCallback = useCallback(() => {
return (
<SendTestWebhookModal
showSendTestWebhookModal={showSendTestWebhookModal}
setShowSendTestWebhookModal={setShowSendTestWebhookModal}
webhook={webhook}
/>
);
}, [showSendTestWebhookModal, setShowSendTestWebhookModal, webhook]);
return useMemo(
() => ({
setShowSendTestWebhookModal,
SendTestWebhookModal: SendTestWebhookModalCallback,
}),
[setShowSendTestWebhookModal, SendTestWebhookModalCallback],
);
}