import { useTranslation } from "react-i18next"; import { faInfoCircle, faPlug, faPlus } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { format } from "date-fns"; import { createNotification } from "@app/components/notifications"; import { ProjectPermissionCan } from "@app/components/permissions"; import { Button, DeleteActionModal, EmptyState, Table, TableContainer, TableSkeleton, TBody, Td, THead, Tooltip, Tr } from "@app/components/v2"; import { ProjectPermissionActions, ProjectPermissionSub, useWorkspace } from "@app/context"; import { withProjectPermission } from "@app/hoc"; import { usePopUp } from "@app/hooks"; import { useCreateWebhook, useDeleteWebhook, useGetWebhooks, useTestWebhook, useUpdateWebhook } from "@app/hooks/api"; import { AddWebhookForm, TFormSchema } from "./AddWebhookForm"; export const WebhooksTab = withProjectPermission( () => { const { t } = useTranslation(); const { currentWorkspace } = useWorkspace(); const workspaceId = currentWorkspace?.id || ""; const { popUp, handlePopUpOpen, handlePopUpToggle, handlePopUpClose } = usePopUp([ "addWebhook", "deleteWebhook" ] as const); const { data: webhooks, isPending: isWebhooksLoading } = useGetWebhooks(workspaceId); // mutation const { mutateAsync: createWebhook } = useCreateWebhook(); const { mutateAsync: testWebhook, variables: testWebhookVars, isPending: isTestWebhookSubmitting } = useTestWebhook(); const { mutateAsync: updateWebhook, variables: updateWebhookVars, isPending: isUpdateWebhookSubmitting } = useUpdateWebhook(); const { mutateAsync: deleteWebhook } = useDeleteWebhook(); const handleWebhookCreate = async (data: TFormSchema) => { try { await createWebhook({ ...data, workspaceId }); handlePopUpClose("addWebhook"); createNotification({ type: "success", text: "Successfully created webhook" }); } catch (err) { console.log(err); createNotification({ type: "error", text: "Failed to create webhook" }); } }; const handleWebhookDisable = async (webhookId: string, isDisabled: boolean) => { try { await updateWebhook({ webhookId, workspaceId, isDisabled }); createNotification({ type: "success", text: "Successfully updated webhook" }); } catch (err) { console.log(err); createNotification({ type: "error", text: "Failed to update webhook" }); } }; const handleWebhookDelete = async () => { try { const webhookId = popUp?.deleteWebhook?.data as string; await deleteWebhook({ webhookId, workspaceId }); handlePopUpClose("deleteWebhook"); createNotification({ type: "success", text: "Successfully deleted webhook" }); } catch (err) { console.log(err); createNotification({ type: "error", text: "Failed to delete webhook" }); } }; const handleWebhookTest = async (webhookId: string) => { try { await testWebhook({ webhookId, workspaceId }); createNotification({ type: "success", text: "Successfully triggered webhook" }); } catch (err) { console.log(err); createNotification({ type: "error", text: "Failed to trigger webhook" }); } }; return (

{t("settings.webhooks.title")}

{(isAllowed) => ( )}

{t("settings.webhooks.description")}

{isWebhooksLoading && } {!isWebhooksLoading && webhooks && webhooks?.length === 0 && ( )} {!isWebhooksLoading && webhooks?.map( ({ id, url, environment, secretPath, lastStatus, isDisabled, updatedAt, lastRunErrorMessage }) => ( ) )}
URL Environment Secret Path Status Action
{url}

{id}

{environment.slug} {secretPath} {!lastStatus ? ( "-" ) : (
{lastStatus}{" "}
Updated At:{" "} {format(new Date(updatedAt), "yyyy-MM-dd, hh:mm aaa")}
{lastRunErrorMessage && (
Error: {lastRunErrorMessage}
)}
} > )}
{(isAllowed) => ( )} {(isAllowed) => ( )} {(isAllowed) => ( )}
handlePopUpToggle("addWebhook", isOpen)} onCreateWebhook={handleWebhookCreate} /> handlePopUpToggle("deleteWebhook", isOpen)} onClose={() => handlePopUpClose("deleteWebhook")} onDeleteApproved={handleWebhookDelete} />
); }, { action: ProjectPermissionActions.Read, subject: ProjectPermissionSub.Webhooks } );