2023-03-14 12:34:41 +00:00
|
|
|
import type { WebhookTriggerEvents } from "@prisma/client";
|
2022-12-20 22:34:37 +00:00
|
|
|
|
2022-09-12 19:07:52 +00:00
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-03-29 05:44:45 +00:00
|
|
|
import {
|
|
|
|
Badge,
|
|
|
|
Button,
|
|
|
|
Dropdown,
|
|
|
|
DropdownItem,
|
|
|
|
DropdownMenuContent,
|
|
|
|
DropdownMenuItem,
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
DropdownMenuSeparator,
|
|
|
|
showToast,
|
|
|
|
Switch,
|
|
|
|
Tooltip,
|
|
|
|
} from "@calcom/ui";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { AlertCircle, Edit, MoreHorizontal, Trash } from "@calcom/ui/components/icon";
|
2022-09-12 19:07:52 +00:00
|
|
|
|
2022-12-20 22:34:37 +00:00
|
|
|
type WebhookProps = {
|
|
|
|
id: string;
|
|
|
|
subscriberUrl: string;
|
|
|
|
payloadTemplate: string | null;
|
|
|
|
active: boolean;
|
|
|
|
eventTriggers: WebhookTriggerEvents[];
|
|
|
|
secret: string | null;
|
|
|
|
eventTypeId: number | null;
|
|
|
|
};
|
2022-09-12 19:07:52 +00:00
|
|
|
|
|
|
|
export default function WebhookListItem(props: {
|
2022-12-20 22:34:37 +00:00
|
|
|
webhook: WebhookProps;
|
2023-04-13 02:10:23 +00:00
|
|
|
canEditWebhook?: boolean;
|
2022-09-12 19:07:52 +00:00
|
|
|
onEditWebhook: () => void;
|
|
|
|
lastItem: boolean;
|
|
|
|
}) {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
const { webhook } = props;
|
2022-11-10 23:40:01 +00:00
|
|
|
const deleteWebhook = trpc.viewer.webhook.delete.useMutation({
|
2022-09-12 19:07:52 +00:00
|
|
|
async onSuccess() {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.webhook.list.invalidate();
|
2022-09-22 17:23:43 +00:00
|
|
|
showToast(t("webhook_removed_successfully"), "success");
|
2022-09-12 19:07:52 +00:00
|
|
|
},
|
|
|
|
});
|
2022-11-10 23:40:01 +00:00
|
|
|
const toggleWebhook = trpc.viewer.webhook.edit.useMutation({
|
2022-09-12 19:07:52 +00:00
|
|
|
async onSuccess(data) {
|
|
|
|
console.log("data", data);
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.webhook.list.invalidate();
|
2022-09-12 19:07:52 +00:00
|
|
|
// TODO: Better success message
|
|
|
|
showToast(t(data?.active ? "enabled" : "disabled"), "success");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-03-29 05:44:45 +00:00
|
|
|
const onDeleteWebhook = () => {
|
|
|
|
// TODO: Confimation dialog before deleting
|
|
|
|
deleteWebhook.mutate({ id: webhook.id, eventTypeId: webhook.eventTypeId || undefined });
|
|
|
|
};
|
|
|
|
|
2022-09-12 19:07:52 +00:00
|
|
|
return (
|
2023-04-11 12:15:10 +00:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
"flex w-full justify-between p-4",
|
|
|
|
props.lastItem ? "" : "border-subtle border-b"
|
|
|
|
)}>
|
2023-03-29 05:44:45 +00:00
|
|
|
<div className="w-full truncate">
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-emphasis truncate text-sm font-medium">{webhook.subscriberUrl}</p>
|
2022-09-22 17:23:43 +00:00
|
|
|
<Tooltip content={t("triggers_when")}>
|
2023-03-29 05:44:45 +00:00
|
|
|
<div className="flex w-4/5 flex-wrap">
|
2022-09-22 17:23:43 +00:00
|
|
|
{webhook.eventTriggers.map((trigger) => (
|
2023-03-29 05:44:45 +00:00
|
|
|
<Badge
|
|
|
|
key={trigger}
|
|
|
|
className="mt-2.5 basis-1/5 ltr:mr-2 rtl:ml-2"
|
|
|
|
variant="gray"
|
2023-04-12 15:26:31 +00:00
|
|
|
startIcon={AlertCircle}>
|
2022-09-22 17:23:43 +00:00
|
|
|
{t(`${trigger.toLowerCase()}`)}
|
|
|
|
</Badge>
|
|
|
|
))}
|
2022-09-12 19:07:52 +00:00
|
|
|
</div>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
2023-03-29 05:44:45 +00:00
|
|
|
<div className="ml-2 flex items-center space-x-4">
|
2022-09-12 19:07:52 +00:00
|
|
|
<Switch
|
|
|
|
defaultChecked={webhook.active}
|
2023-04-13 02:10:23 +00:00
|
|
|
disabled={!props.canEditWebhook}
|
2022-09-12 19:07:52 +00:00
|
|
|
onCheckedChange={() =>
|
|
|
|
toggleWebhook.mutate({
|
|
|
|
id: webhook.id,
|
|
|
|
active: !webhook.active,
|
|
|
|
payloadTemplate: webhook.payloadTemplate,
|
2022-12-20 22:34:37 +00:00
|
|
|
eventTypeId: webhook.eventTypeId || undefined,
|
2022-09-12 19:07:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
2023-03-29 05:44:45 +00:00
|
|
|
<Button className="hidden lg:flex" color="secondary" onClick={props.onEditWebhook}>
|
2022-09-12 19:07:52 +00:00
|
|
|
{t("edit")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
2023-03-29 05:44:45 +00:00
|
|
|
className="hidden lg:flex"
|
2022-09-12 19:07:52 +00:00
|
|
|
color="destructive"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Trash}
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2023-03-29 05:44:45 +00:00
|
|
|
onClick={onDeleteWebhook}
|
2022-09-12 19:07:52 +00:00
|
|
|
/>
|
2023-03-29 05:44:45 +00:00
|
|
|
<Dropdown>
|
|
|
|
<DropdownMenuTrigger asChild>
|
2023-04-12 15:26:31 +00:00
|
|
|
<Button className="lg:hidden" StartIcon={MoreHorizontal} variant="icon" color="secondary" />
|
2023-03-29 05:44:45 +00:00
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent>
|
|
|
|
<DropdownMenuItem>
|
2023-04-12 15:26:31 +00:00
|
|
|
<DropdownItem StartIcon={Edit} color="secondary" onClick={props.onEditWebhook}>
|
2023-03-29 05:44:45 +00:00
|
|
|
{t("edit")}
|
|
|
|
</DropdownItem>
|
|
|
|
</DropdownMenuItem>
|
|
|
|
<DropdownMenuSeparator />
|
|
|
|
|
|
|
|
<DropdownMenuItem>
|
2023-04-12 15:26:31 +00:00
|
|
|
<DropdownItem StartIcon={Trash} color="destructive" onClick={onDeleteWebhook}>
|
2023-03-29 05:44:45 +00:00
|
|
|
{t("delete")}
|
|
|
|
</DropdownItem>
|
|
|
|
</DropdownMenuItem>
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</Dropdown>
|
2022-09-12 19:07:52 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|