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.

display the webhook logs

Kiran K05e84b35de75
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/[webhookId]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/[webhookId]/page-client.tsx
index 1c2495b677..5dd2248ab4 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/[webhookId]/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/[webhookId]/page-client.tsx
@@ -2,8 +2,10 @@
import { clientAccessCheck } from "@/lib/api/tokens/permissions";
import useWorkspace from "@/lib/swr/use-workspace";
-import z from "@/lib/zod";
-import { webhookEventSchemaTB } from "@/lib/zod/schemas/webhooks";
+import { WebhookEventProps } from "@/lib/types";
+import { WebhookEventListSkeleton } from "@/ui/webhooks/loading-events-skelton";
+import { NoEventsPlaceholder } from "@/ui/webhooks/no-events-placeholder";
+import { WebhookEventList } from "@/ui/webhooks/webhook-events";
import WebhookHeader from "@/ui/webhooks/webhook-header";
import { MaxWidthWrapper } from "@dub/ui";
import { fetcher } from "@dub/utils";
@@ -26,11 +28,7 @@ export default function WebhookLogsPageClient({
redirect(`/${slug}/settings`);
}
- const {
- data: events,
- isLoading,
- error,
- } = useSWR<z.infer<typeof webhookEventSchemaTB>[]>(
+ const { data: events, isLoading } = useSWR<WebhookEventProps[]>(
`/api/webhooks/${webhookId}/events?workspaceId=${workspaceId}`,
fetcher,
{
@@ -38,35 +36,20 @@ export default function WebhookLogsPageClient({
},
);
- console.log(events);
+ // TODO: Get total events from API
+ const totalEvents = events?.length || 100;
return (
<>
<WebhookHeader webhookId={webhookId} page="events" />
<MaxWidthWrapper className="max-w-screen-lg space-y-6">
- {/* display events table */}
- <div>
- <table>
- <thead>
- <tr>
- <th>Status</th>
- <th>Event Type</th>
- <th>HTTP Status</th>
- <th>Created At</th>
- </tr>
- </thead>
- <tbody>
- {events?.map((event) => (
- <tr key={event.event_id}>
- <td>{event.http_status}</td>
- <td>{event.event}</td>
- <td>{event.http_status}</td>
- <td>{event.timestamp}</td>
- </tr>
- ))}
- </tbody>
- </table>
- </div>
+ {isLoading ? (
+ <WebhookEventListSkeleton />
+ ) : events && events.length === 0 ? (
+ <NoEventsPlaceholder />
+ ) : (
+ <WebhookEventList totalEvents={totalEvents} events={events || []} />
+ )}
</MaxWidthWrapper>
</>
);
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/[webhookId]/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/[webhookId]/page.tsx
index 714f01599c..968e0cec75 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/[webhookId]/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/[webhookId]/page.tsx
@@ -1,11 +1,11 @@
-import WebhookLogsPageClient from "./page-client";
+import WebhookEventsPageClient from "./page-client";
-export default async function WebhookLogsPage({
+export default async function WebhookEventsPage({
params,
}: {
params: { webhookId: string };
}) {
const { webhookId } = params;
- return <WebhookLogsPageClient webhookId={webhookId} />;
+ return <WebhookEventsPageClient webhookId={webhookId} />;
}
diff --git a/apps/web/lib/tinybird/record-webhook-event.ts b/apps/web/lib/tinybird/record-webhook-event.ts
index ee81243c5c..10790fc06a 100644
--- a/apps/web/lib/tinybird/record-webhook-event.ts
+++ b/apps/web/lib/tinybird/record-webhook-event.ts
@@ -3,7 +3,5 @@ import { tb } from "./client";
export const recordWebhookEvent = tb.buildIngestEndpoint({
datasource: "dub_webhook_events",
- event: webhookEventSchemaTB.omit({
- timestamp: true,
- }),
+ event: webhookEventSchemaTB,
});
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index e79628b7a1..f45ad66e44 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -7,7 +7,11 @@ import { integrationSchema } from "./zod/schemas/integration";
import { createLinkBodySchema } from "./zod/schemas/links";
import { oAuthAppSchema } from "./zod/schemas/oauth";
import { tokenSchema } from "./zod/schemas/token";
-import { createWebhookSchema, webhookSchema } from "./zod/schemas/webhooks";
+import {
+ createWebhookSchema,
+ webhookEventSchemaTB,
+ webhookSchema,
+} from "./zod/schemas/webhooks";
export type LinkProps = Link;
@@ -257,3 +261,5 @@ export type WebhookTrigger = keyof typeof WEBHOOK_TRIGGER_DESCRIPTIONS;
export type WebhookProps = z.infer<typeof webhookSchema>;
export type NewWebhook = z.infer<typeof createWebhookSchema>;
+
+export type WebhookEventProps = z.infer<typeof webhookEventSchemaTB>;
diff --git a/apps/web/lib/zod/schemas/webhooks.ts b/apps/web/lib/zod/schemas/webhooks.ts
index 58d37fd697..cc0b6f9068 100644
--- a/apps/web/lib/zod/schemas/webhooks.ts
+++ b/apps/web/lib/zod/schemas/webhooks.ts
@@ -43,7 +43,7 @@ export const webhookCallbackSchema = z.object({
// Webhook event schema for the webhook logs
export const webhookEventSchemaTB = z.object({
- timestamp: z.string(),
+ timestamp: z.string().default(new Date(Date.now()).toISOString()),
event_id: z.string(),
webhook_id: z.string(),
message_id: z.string(), // QStash message ID
diff --git a/apps/web/ui/webhooks/loading-events-skelton.tsx b/apps/web/ui/webhooks/loading-events-skelton.tsx
new file mode 100644
index 0000000000..6f78afcd01
--- /dev/null
+++ b/apps/web/ui/webhooks/loading-events-skelton.tsx
@@ -0,0 +1,19 @@
+export const WebhookEventListSkeleton = () => {
+ return (
+ <div className="rounded-xl border border-gray-200">
+ <div className="flex flex-col divide-y divide-gray-200">
+ {[...Array(5)].map((_, index) => (
+ <div
+ className="flex items-center justify-between gap-5 px-3.5 py-4"
+ key={index}
+ >
+ <div className="flex items-center gap-2.5 text-gray-500">
+ <div className="h-4 w-4 animate-pulse rounded-full bg-gray-200" />
+ <div className="h-4 w-28 animate-pulse rounded-full bg-gray-200" />
+ </div>
+ </div>
+ ))}
+ </div>
+ </div>
+ );
+}
\ No newline at end of file
diff --git a/apps/web/ui/webhooks/no-events-placeholder.tsx b/apps/web/ui/webhooks/no-events-placeholder.tsx
new file mode 100644
index 0000000000..7f8843b071
--- /dev/null
+++ b/apps/web/ui/webhooks/no-events-placeholder.tsx
@@ -0,0 +1,14 @@
+import { Webhook } from "lucide-react";
+import EmptyState from "../shared/empty-state";
+
+export const NoEventsPlaceholder = () => {
+ return (
+ <div className="rounded-xl border border-gray-200 py-10">
+ <EmptyState
+ icon={Webhook}
+ title="No events"
+ description="No events have been logged for this webhook. Events will appear as they are logged."
+ />
+ </div>
+ );
+};
diff --git a/apps/web/ui/webhooks/webhook-events.tsx b/apps/web/ui/webhooks/webhook-events.tsx
new file mode 100644
index 0000000000..206d68917b
--- /dev/null
+++ b/apps/web/ui/webhooks/webhook-events.tsx
@@ -0,0 +1,105 @@
+"use client";
+
+import { WebhookEventProps } from "@/lib/types";
+import { nFormatter, PAGINATION_LIMIT } from "@dub/utils";
+import { CircleCheck, Clock4 } from "lucide-react";
+import { PropsWithChildren } from "react";
+import usePagination from "../analytics/events/use-pagination";
+
+export type EventListProps = PropsWithChildren<{
+ events: WebhookEventProps[];
+ totalEvents: number;
+}>;
+
+const buttonClassName = [
+ "flex h-7 items-center justify-center gap-2 whitespace-nowrap rounded-md border border-gray-200 bg-white px-2 text-sm text-gray-600",
+ "outline-none hover:bg-gray-50 focus-visible:border-gray-500",
+ "disabled:cursor-not-allowed disabled:border-gray-200 disabled:text-gray-400 disabled:bg-white",
+].join(" ");
+
+const isSuccess = (status: number) => status >= 200 && status < 300;
+
+export function WebhookEventList({ events, totalEvents }: EventListProps) {
+ const { pagination, setPagination } = usePagination(PAGINATION_LIMIT);
+
+ return (
+ <div className="rounded-xl border border-gray-200">
+ {/* Webhook Events */}
+ <div className="flex flex-col divide-y divide-gray-200">
+ {events.map((event, index) => (
+ <div
+ key={index}
+ className="flex items-center justify-between gap-5 px-3.5 py-4"
+ >
+ <div className="flex items-center gap-2.5">
+ <div>
+ {isSuccess(event.http_status) ? (
+ <CircleCheck className="size-4 text-green-500" />
+ ) : (
+ <Clock4 className="size-4 text-red-500" />
+ )}
+ </div>
+ <div className="text-sm text-gray-500">{event.event}</div>
+ </div>
+ <div className="text-xs text-gray-400">
+ {new Date(event.timestamp).toLocaleString()}
+ </div>
+ </div>
+ ))}
+ </div>
+
+ {/* Pagination */}
+ <div className="sticky bottom-0 flex items-center justify-between gap-2 rounded-b-[inherit] border-t border-gray-200 bg-white px-3.5 py-2 text-sm leading-6 text-gray-600">
+ <div>
+ <span className="hidden sm:inline-block">Viewing</span>{" "}
+ <span className="font-medium">
+ {pagination.pageIndex * pagination.pageSize + 1}-
+ {Math.min(
+ pagination.pageIndex * pagination.pageSize + pagination.pageSize,
+ totalEvents,
+ )}
+ </span>{" "}
+ of{" "}
+ <span className="font-medium">
+ {nFormatter(totalEvents, { full: true })}
+ </span>{" "}
+ events
+ </div>
+ <div className="flex items-center gap-2">
+ <button
+ type="button"
+ className={buttonClassName}
+ onClick={() =>
+ setPagination({
+ pageIndex: pagination.pageIndex - 1,
+ pageSize: pagination.pageSize,
+ })
+ }
+ disabled={pagination.pageIndex === 0}
+ >
+ Previous
+ </button>
+ <button
+ type="button"
+ className={buttonClassName}
+ onClick={() =>
+ setPagination({
+ pageIndex: pagination.pageIndex + 1,
+ pageSize: pagination.pageSize,
+ })
+ }
+ disabled={
+ pagination.pageIndex * pagination.pageSize +
+ pagination.pageSize >=
+ totalEvents
+ }
+ >
+ Next
+ </button>
+ </div>
+ </div>
+ </div>
+ );
+}
+
+