2022-09-12 19:07:52 +00:00
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-05-02 11:44:05 +00:00
|
|
|
import type { WebhookTriggerEvents } from "@calcom/prisma/enums";
|
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-09-28 11:59:06 +00:00
|
|
|
import { Edit, MoreHorizontal, Trash, Zap } 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;
|
2023-05-23 01:15:29 +00:00
|
|
|
teamId: number | null;
|
2022-12-20 22:34:37 +00:00
|
|
|
};
|
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;
|
2023-05-23 01:15:29 +00:00
|
|
|
readOnly?: boolean;
|
2022-09-12 19:07:52 +00:00
|
|
|
}) {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
const { webhook } = props;
|
2023-05-23 01:15:29 +00:00
|
|
|
const canEditWebhook = props.canEditWebhook ?? true;
|
|
|
|
|
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() {
|
2023-05-23 01:15:29 +00:00
|
|
|
await utils.viewer.webhook.getByViewer.invalidate();
|
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) {
|
2023-05-23 01:15:29 +00:00
|
|
|
await utils.viewer.webhook.getByViewer.invalidate();
|
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
|
2023-05-23 01:15:29 +00:00
|
|
|
deleteWebhook.mutate({
|
|
|
|
id: webhook.id,
|
|
|
|
eventTypeId: webhook.eventTypeId || undefined,
|
|
|
|
teamId: webhook.teamId || undefined,
|
|
|
|
});
|
2023-03-29 05:44:45 +00:00
|
|
|
};
|
|
|
|
|
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-05-23 01:15:29 +00:00
|
|
|
<div className="flex">
|
2023-10-20 14:38:52 +00:00
|
|
|
<Tooltip content={webhook.subscriberUrl}>
|
|
|
|
<p className="text-emphasis max-w-[600px] truncate text-sm font-medium">
|
|
|
|
{webhook.subscriberUrl}
|
|
|
|
</p>
|
|
|
|
</Tooltip>
|
2023-05-23 01:15:29 +00:00
|
|
|
{!!props.readOnly && (
|
|
|
|
<Badge variant="gray" className="ml-2 ">
|
|
|
|
{t("readonly")}
|
|
|
|
</Badge>
|
|
|
|
)}
|
|
|
|
</div>
|
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-09-28 11:59:06 +00:00
|
|
|
startIcon={Zap}>
|
2022-09-22 17:23:43 +00:00
|
|
|
{t(`${trigger.toLowerCase()}`)}
|
|
|
|
</Badge>
|
|
|
|
))}
|
2022-09-12 19:07:52 +00:00
|
|
|
</div>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
2023-05-23 01:15:29 +00:00
|
|
|
{!props.readOnly && (
|
|
|
|
<div className="ml-2 flex items-center space-x-4">
|
|
|
|
<Switch
|
|
|
|
defaultChecked={webhook.active}
|
|
|
|
disabled={!canEditWebhook}
|
|
|
|
onCheckedChange={() =>
|
|
|
|
toggleWebhook.mutate({
|
|
|
|
id: webhook.id,
|
|
|
|
active: !webhook.active,
|
|
|
|
payloadTemplate: webhook.payloadTemplate,
|
|
|
|
eventTypeId: webhook.eventTypeId || undefined,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
2023-03-29 05:44:45 +00:00
|
|
|
|
2023-05-23 01:15:29 +00:00
|
|
|
<Button className="hidden lg:flex" color="secondary" onClick={props.onEditWebhook}>
|
|
|
|
{t("edit")}
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
<Button
|
|
|
|
className="hidden lg:flex"
|
|
|
|
color="destructive"
|
|
|
|
StartIcon={Trash}
|
|
|
|
variant="icon"
|
|
|
|
onClick={onDeleteWebhook}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Dropdown>
|
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
<Button className="lg:hidden" StartIcon={MoreHorizontal} variant="icon" color="secondary" />
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent>
|
|
|
|
<DropdownMenuItem>
|
|
|
|
<DropdownItem StartIcon={Edit} color="secondary" onClick={props.onEditWebhook}>
|
|
|
|
{t("edit")}
|
|
|
|
</DropdownItem>
|
|
|
|
</DropdownMenuItem>
|
|
|
|
<DropdownMenuSeparator />
|
|
|
|
|
|
|
|
<DropdownMenuItem>
|
|
|
|
<DropdownItem StartIcon={Trash} color="destructive" onClick={onDeleteWebhook}>
|
|
|
|
{t("delete")}
|
|
|
|
</DropdownItem>
|
|
|
|
</DropdownMenuItem>
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</Dropdown>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-09-12 19:07:52 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|