Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/dashboard/src/@/hooks/useEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ export function useEngineUpdateAccessToken(params: {
});
}

export type CreateWebhookInput = {
type CreateWebhookInput = {
url: string;
name: string;
eventType: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { PlusIcon } from "lucide-react";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import {
Flex,
Form,
FormControl,
Input,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
Select,
useDisclosure,
} from "@chakra-ui/react";
import { Button } from "chakra/button";
import { FormLabel } from "chakra/form";
import { CirclePlusIcon } from "lucide-react";
import { useForm } from "react-hook-form";
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Spinner } from "@/components/ui/Spinner/Spinner";
import {
type CreateWebhookInput,
useEngineCreateWebhook,
} from "@/hooks/useEngine";
import { useTxNotifications } from "@/hooks/useTxNotifications";
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { useEngineCreateWebhook } from "@/hooks/useEngine";
import { beautifyString } from "./webhooks-table";

interface AddWebhookButtonProps {
instanceUrl: string;
authToken: string;
}

const WEBHOOK_EVENT_TYPES = [
"all_transactions",
"sent_transaction",
Expand All @@ -38,97 +43,156 @@ const WEBHOOK_EVENT_TYPES = [
"auth",
];

export const AddWebhookButton: React.FC<AddWebhookButtonProps> = ({
const webhookFormSchema = z.object({
eventType: z.string().min(1, "Event type is required"),
name: z.string().min(1, "Name is required"),
url: z.string().url("Please enter a valid URL"),
});

type WebhookFormValues = z.infer<typeof webhookFormSchema>;

export function AddWebhookButton({
instanceUrl,
authToken,
}) => {
const { isOpen, onOpen, onClose } = useDisclosure();
const { mutate: createWebhook } = useEngineCreateWebhook({
}: {
instanceUrl: string;
authToken: string;
}) {
const [open, setOpen] = useState(false);
const createWebhook = useEngineCreateWebhook({
authToken,
instanceUrl,
});

const form = useForm<CreateWebhookInput>();
const form = useForm<WebhookFormValues>({
resolver: zodResolver(webhookFormSchema),
defaultValues: {
eventType: "",
name: "",
url: "",
},
mode: "onChange",
});

const { onSuccess, onError } = useTxNotifications(
"Webhook created successfully.",
"Failed to create webhook.",
);
const onSubmit = (data: WebhookFormValues) => {
createWebhook.mutate(data, {
onError: (error) => {
toast.error("Failed to create webhook", {
description: error.message,
});
console.error(error);
},
onSuccess: () => {
toast.success("Webhook created successfully");
setOpen(false);
form.reset();
},
});
};

return (
<>
<Button
colorScheme="primary"
leftIcon={<CirclePlusIcon className="size-6" />}
onClick={onOpen}
size="sm"
variant="ghost"
w="fit-content"
>
Create Webhook
</Button>
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button className="w-fit gap-2" onClick={() => setOpen(true)}>
<PlusIcon className="size-4" />
Create Webhook
</Button>
</DialogTrigger>

<Modal isCentered isOpen={isOpen} onClose={onClose}>
<ModalOverlay />
<ModalContent
as="form"
className="!bg-background rounded-lg border border-border"
onSubmit={form.handleSubmit((data) => {
createWebhook(data, {
onError: (error) => {
onError(error);
console.error(error);
},
onSuccess: () => {
onSuccess();
onClose();
},
});
})}
>
<ModalHeader>Create Webhook</ModalHeader>
<ModalCloseButton />
<ModalBody>
<Flex flexDir="column" gap={4}>
<FormControl isRequired>
<FormLabel>Event Type</FormLabel>
<Select {...form.register("eventType", { required: true })}>
{WEBHOOK_EVENT_TYPES.map((eventType) => (
<option key={eventType} value={eventType}>
{beautifyString(eventType)}
</option>
))}
</Select>
</FormControl>
<FormControl isRequired>
<FormLabel>Name</FormLabel>
<Input
placeholder="My webhook"
type="text"
{...form.register("name", { required: true })}
/>
</FormControl>
<FormControl isRequired>
<FormLabel>URL</FormLabel>
<Input
placeholder="https://"
type="url"
{...form.register("url", { required: true })}
/>
</FormControl>
</Flex>
</ModalBody>
<DialogContent className="p-0 gap-0 overflow-hidden">
<DialogHeader className="p-4 lg:p-6">
<DialogTitle>Create Webhook</DialogTitle>
<DialogDescription>
Create a new webhook to receive notifications for engine events.
</DialogDescription>
</DialogHeader>

<ModalFooter as={Flex} gap={3}>
<Button onClick={onClose} type="button" variant="ghost">
Cancel
</Button>
<Button colorScheme="primary" type="submit">
Create
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<div className="space-y-4 px-4 lg:px-6 pb-4">
<FormField
control={form.control}
name="eventType"
render={({ field }) => (
<FormItem>
<FormLabel>Event Type</FormLabel>
<Select onValueChange={field.onChange} value={field.value}>
<FormControl>
<SelectTrigger className="bg-card">
<SelectValue placeholder="Select event type" />
</SelectTrigger>
</FormControl>
<SelectContent>
{WEBHOOK_EVENT_TYPES.map((eventType) => (
<SelectItem key={eventType} value={eventType}>
{beautifyString(eventType)}
</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input
className="bg-card"
placeholder="My webhook"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name="url"
render={({ field }) => (
<FormItem>
<FormLabel>URL</FormLabel>
<FormControl>
<Input
className="bg-card"
placeholder="https://"
type="url"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>

<div className="flex justify-end gap-3 p-4 lg:p-6 bg-card border-t border-border">
<Button
type="button"
variant="outline"
onClick={() => setOpen(false)}
>
Cancel
</Button>
<Button
type="submit"
disabled={createWebhook.isPending}
className="gap-2"
>
{createWebhook.isPending && <Spinner className="size-4" />}
Create
</Button>
</div>
</form>
</Form>
</DialogContent>
</Dialog>
);
};
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
"use client";

import { Heading } from "chakra/heading";
import { Link } from "chakra/link";
import { Text } from "chakra/text";
import { UnderlineLink } from "@/components/ui/UnderlineLink";
import { useEngineWebhooks } from "@/hooks/useEngine";
import { AddWebhookButton } from "./add-webhook-button";
import { WebhooksTable } from "./webhooks-table";

interface EngineWebhooksProps {
instanceUrl: string;
authToken: string;
}

export const EngineWebhooks: React.FC<EngineWebhooksProps> = ({
export function EngineWebhooks({
instanceUrl,
authToken,
}) => {
}: {
instanceUrl: string;
authToken: string;
}) {
const webhooks = useEngineWebhooks({
authToken,
instanceUrl,
});

return (
<div className="flex flex-col gap-4">
<div className="flex flex-col gap-2">
<Heading size="title.md">Webhooks</Heading>
<Text>
Notify your app backend when transaction and backend wallet events
occur.{" "}
<Link
color="primary.500"
href="https://portal.thirdweb.com/infrastructure/engine/features/webhooks"
isExternal
>
Learn more about webhooks
</Link>
.
</Text>
</div>
<div>
<h2 className="text-2xl tracking-tight font-semibold mb-1">Webhooks</h2>
<p className="text-muted-foreground mb-4 text-sm">
Notify your app backend when transaction and backend wallet events
occur.{" "}
<UnderlineLink
href="https://portal.thirdweb.com/infrastructure/engine/features/webhooks"
rel="noopener noreferrer"
target="_blank"
>
Learn more about webhooks
</UnderlineLink>
.
</p>

<WebhooksTable
authToken={authToken}
instanceUrl={instanceUrl}
isFetched={webhooks.isFetched}
isPending={webhooks.isPending}
webhooks={webhooks.data || []}
/>
<AddWebhookButton authToken={authToken} instanceUrl={instanceUrl} />

<div className="mt-4 flex justify-end">
<AddWebhookButton authToken={authToken} instanceUrl={instanceUrl} />
</div>
</div>
);
};
}
Loading
Loading