import { Button, Input } from "@usememos/mui"; import { XIcon } from "lucide-react"; import React, { useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import { webhookServiceClient } from "@/grpcweb"; import useLoading from "@/hooks/useLoading"; import { useTranslate } from "@/utils/i18n"; import { generateDialog } from "./Dialog"; interface Props extends DialogProps { webhookName?: string; onConfirm: () => void; } interface State { displayName: string; url: string; } const CreateWebhookDialog: React.FC = (props: Props) => { const { webhookName, destroy, onConfirm } = props; const t = useTranslate(); const [state, setState] = useState({ displayName: "", url: "", }); const requestState = useLoading(false); const isCreating = webhookName === undefined; useEffect(() => { if (webhookName) { webhookServiceClient .getWebhook({ name: webhookName, }) .then((webhook) => { setState({ displayName: webhook.displayName, url: webhook.url, }); }); } }, []); const setPartialState = (partialState: Partial) => { setState({ ...state, ...partialState, }); }; const handleTitleInputChange = (e: React.ChangeEvent) => { setPartialState({ displayName: e.target.value, }); }; const handleUrlInputChange = (e: React.ChangeEvent) => { setPartialState({ url: e.target.value, }); }; const handleSaveBtnClick = async () => { if (!state.displayName || !state.url) { toast.error(t("message.fill-all-required-fields")); return; } try { if (isCreating) { await webhookServiceClient.createWebhook({ webhook: { displayName: state.displayName, url: state.url, }, }); } else { await webhookServiceClient.updateWebhook({ webhook: { name: webhookName, displayName: state.displayName, url: state.url, }, updateMask: ["display_name", "url"], }); } onConfirm(); destroy(); } catch (error: any) { console.error(error); toast.error(error.details); } }; return (

{isCreating ? t("setting.webhook-section.create-dialog.create-webhook") : t("setting.webhook-section.create-dialog.edit-webhook")}

{t("setting.webhook-section.create-dialog.title")} *
{t("setting.webhook-section.create-dialog.payload-url")} *
); }; function showCreateWebhookDialog(onConfirm: () => void) { generateDialog( { className: "create-webhook-dialog", dialogName: "create-webhook-dialog", }, CreateWebhookDialog, { onConfirm, }, ); } export default showCreateWebhookDialog;