2022-08-31 19:42:37 +00:00
|
|
|
import { WorkflowActions } from "@prisma/client";
|
|
|
|
import Link from "next/link";
|
|
|
|
import { useRouter } from "next/router";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { useEffect, useState } from "react";
|
2022-08-31 19:42:37 +00:00
|
|
|
|
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { Button, EmptyScreen, showToast, Switch, Tooltip } from "@calcom/ui";
|
|
|
|
import { FiExternalLink, FiZap } from "@calcom/ui/components/icon";
|
2022-08-31 19:42:37 +00:00
|
|
|
|
2022-11-23 02:55:25 +00:00
|
|
|
import LicenseRequired from "../../common/components/v2/LicenseRequired";
|
|
|
|
import { getActionIcon } from "../lib/getActionIcon";
|
2022-09-30 18:39:23 +00:00
|
|
|
import SkeletonLoader from "./SkeletonLoaderEventWorkflowsTab";
|
2023-02-27 07:24:43 +00:00
|
|
|
import type { WorkflowType } from "./WorkflowListPage";
|
2022-08-31 19:42:37 +00:00
|
|
|
|
|
|
|
type ItemProps = {
|
|
|
|
workflow: WorkflowType;
|
|
|
|
eventType: {
|
|
|
|
id: number;
|
|
|
|
title: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const WorkflowListItem = (props: ItemProps) => {
|
|
|
|
const { workflow, eventType } = props;
|
|
|
|
const { t } = useLocale();
|
|
|
|
|
|
|
|
const [activeEventTypeIds, setActiveEventTypeIds] = useState(
|
|
|
|
workflow.activeOn.map((active) => {
|
|
|
|
if (active.eventType) {
|
|
|
|
return active.eventType.id;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const isActive = activeEventTypeIds.includes(eventType.id);
|
2023-03-02 18:15:28 +00:00
|
|
|
const utils = trpc.useContext();
|
2022-08-31 19:42:37 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const activateEventTypeMutation = trpc.viewer.workflows.activateEventType.useMutation({
|
2022-09-06 03:29:00 +00:00
|
|
|
onSuccess: async () => {
|
|
|
|
let offOn = "";
|
|
|
|
if (activeEventTypeIds.includes(eventType.id)) {
|
|
|
|
const newActiveEventTypeIds = activeEventTypeIds.filter((id) => {
|
|
|
|
return id !== eventType.id;
|
|
|
|
});
|
|
|
|
setActiveEventTypeIds(newActiveEventTypeIds);
|
|
|
|
offOn = "off";
|
|
|
|
} else {
|
|
|
|
const newActiveEventTypeIds = activeEventTypeIds;
|
|
|
|
newActiveEventTypeIds.push(eventType.id);
|
|
|
|
setActiveEventTypeIds(newActiveEventTypeIds);
|
|
|
|
offOn = "on";
|
|
|
|
}
|
2023-03-02 18:15:28 +00:00
|
|
|
await utils.viewer.eventTypes.get.invalidate({ id: eventType.id });
|
2022-09-06 03:29:00 +00:00
|
|
|
showToast(
|
|
|
|
t("workflow_turned_on_successfully", {
|
|
|
|
workflowName: workflow.name,
|
|
|
|
offOn,
|
|
|
|
}),
|
|
|
|
"success"
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onError: (err) => {
|
|
|
|
if (err instanceof HttpError) {
|
|
|
|
const message = `${err.statusCode}: ${err.message}`;
|
|
|
|
showToast(message, "error");
|
|
|
|
}
|
2023-02-27 07:24:43 +00:00
|
|
|
if (err.data?.code === "UNAUTHORIZED") {
|
|
|
|
// TODO: Add missing translation
|
|
|
|
const message = `${err.data.code}: You are not authorized to enable or disable this workflow`;
|
|
|
|
showToast(message, "error");
|
|
|
|
}
|
2022-09-06 03:29:00 +00:00
|
|
|
},
|
|
|
|
});
|
2022-08-31 19:42:37 +00:00
|
|
|
|
|
|
|
const sendTo: Set<string> = new Set();
|
|
|
|
|
|
|
|
workflow.steps.forEach((step) => {
|
|
|
|
switch (step.action) {
|
|
|
|
case WorkflowActions.EMAIL_HOST:
|
2023-01-09 14:15:11 +00:00
|
|
|
sendTo.add(t("organizer_name_variable"));
|
2022-08-31 19:42:37 +00:00
|
|
|
break;
|
|
|
|
case WorkflowActions.EMAIL_ATTENDEE:
|
2023-01-09 14:15:11 +00:00
|
|
|
sendTo.add(t("attendee_name_variable"));
|
2022-08-31 19:42:37 +00:00
|
|
|
break;
|
|
|
|
case WorkflowActions.SMS_ATTENDEE:
|
2023-01-09 14:15:11 +00:00
|
|
|
sendTo.add(t("attendee_name_variable"));
|
2022-08-31 19:42:37 +00:00
|
|
|
break;
|
|
|
|
case WorkflowActions.SMS_NUMBER:
|
|
|
|
sendTo.add(step.sendTo || "");
|
|
|
|
break;
|
2022-10-10 13:40:20 +00:00
|
|
|
case WorkflowActions.EMAIL_ADDRESS:
|
|
|
|
sendTo.add(step.sendTo || "");
|
|
|
|
break;
|
2022-08-31 19:42:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
2023-02-05 23:09:27 +00:00
|
|
|
<div className="flex w-full items-center overflow-hidden rounded-md border border-gray-200 p-6 px-3 md:p-6">
|
2022-09-07 14:05:25 +00:00
|
|
|
<div className="mr-4 flex h-10 w-10 items-center justify-center rounded-full bg-gray-100 text-xs font-medium">
|
2022-08-31 19:42:37 +00:00
|
|
|
{getActionIcon(
|
|
|
|
workflow.steps,
|
2022-09-07 14:05:25 +00:00
|
|
|
isActive ? "h-6 w-6 stroke-[1.5px] text-gray-700" : "h-6 w-6 stroke-[1.5px] text-gray-400"
|
2022-08-31 19:42:37 +00:00
|
|
|
)}
|
|
|
|
</div>
|
2022-09-07 14:05:25 +00:00
|
|
|
<div className=" grow">
|
2022-08-31 19:42:37 +00:00
|
|
|
<div
|
|
|
|
className={classNames(
|
2022-09-07 14:05:25 +00:00
|
|
|
"mb-1 w-full truncate text-base font-medium leading-4 text-gray-900 md:max-w-max",
|
2023-01-12 16:57:43 +00:00
|
|
|
workflow.name && isActive ? "text-gray-900" : "text-gray-500"
|
2022-08-31 19:42:37 +00:00
|
|
|
)}>
|
|
|
|
{workflow.name
|
|
|
|
? workflow.name
|
|
|
|
: "Untitled (" +
|
|
|
|
`${t(`${workflow.steps[0].action.toLowerCase()}_action`)}`.charAt(0).toUpperCase() +
|
|
|
|
`${t(`${workflow.steps[0].action.toLowerCase()}_action`)}`.slice(1) +
|
|
|
|
")"}
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className={classNames(
|
2022-09-07 14:05:25 +00:00
|
|
|
" flex w-fit items-center whitespace-nowrap rounded-sm text-sm leading-4",
|
2022-08-31 19:42:37 +00:00
|
|
|
isActive ? "text-gray-600" : "text-gray-400"
|
|
|
|
)}>
|
|
|
|
<span className="mr-1">{t("to")}:</span>
|
|
|
|
{Array.from(sendTo).map((sendToPerson, index) => {
|
|
|
|
return <span key={index}>{`${index ? ", " : ""}${sendToPerson}`}</span>;
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-06 03:29:00 +00:00
|
|
|
<div className="flex-none">
|
2023-01-06 12:13:56 +00:00
|
|
|
<Link href={`/workflows/${workflow.id}`} passHref={true} target="_blank">
|
|
|
|
<Button type="button" color="minimal" className="mr-4">
|
|
|
|
<div className="hidden ltr:mr-2 rtl:ml-2 sm:block">{t("edit")}</div>
|
2023-01-23 23:08:01 +00:00
|
|
|
<FiExternalLink className="-mt-[2px] h-4 w-4 stroke-2 text-gray-600" />
|
2023-01-06 12:13:56 +00:00
|
|
|
</Button>
|
2022-08-31 19:42:37 +00:00
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
<Tooltip content={t("turn_off") as string}>
|
2023-01-04 07:38:45 +00:00
|
|
|
<div className="ltr:mr-2 rtl:ml-2">
|
2022-08-31 19:42:37 +00:00
|
|
|
<Switch
|
|
|
|
checked={isActive}
|
|
|
|
onCheckedChange={() => {
|
|
|
|
activateEventTypeMutation.mutate({ workflowId: workflow.id, eventTypeId: eventType.id });
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
eventType: {
|
|
|
|
id: number;
|
|
|
|
title: string;
|
2023-02-27 07:24:43 +00:00
|
|
|
userId: number | null;
|
|
|
|
team: {
|
|
|
|
id?: number;
|
|
|
|
} | null;
|
2022-08-31 19:42:37 +00:00
|
|
|
};
|
|
|
|
workflows: WorkflowType[];
|
|
|
|
};
|
|
|
|
|
|
|
|
function EventWorkflowsTab(props: Props) {
|
2023-02-27 07:24:43 +00:00
|
|
|
const { workflows, eventType } = props;
|
2022-08-31 19:42:37 +00:00
|
|
|
const { t } = useLocale();
|
2023-02-27 07:24:43 +00:00
|
|
|
const { data, isLoading } = trpc.viewer.workflows.list.useQuery({
|
|
|
|
teamId: eventType.team?.id,
|
|
|
|
userId: eventType.userId || undefined,
|
|
|
|
});
|
2022-08-31 19:42:37 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const [sortedWorkflows, setSortedWorkflows] = useState<Array<WorkflowType>>([]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (data?.workflows) {
|
|
|
|
const activeWorkflows = workflows.map((workflowOnEventType) => {
|
|
|
|
return workflowOnEventType;
|
|
|
|
});
|
|
|
|
const disabledWorkflows = data.workflows.filter(
|
|
|
|
(workflow) =>
|
|
|
|
!workflows
|
|
|
|
.map((workflow) => {
|
|
|
|
return workflow.id;
|
|
|
|
})
|
|
|
|
.includes(workflow.id)
|
|
|
|
);
|
|
|
|
setSortedWorkflows(activeWorkflows.concat(disabledWorkflows));
|
|
|
|
}
|
|
|
|
}, [isLoading]);
|
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
const createMutation = trpc.viewer.workflows.create.useMutation({
|
2022-08-31 19:42:37 +00:00
|
|
|
onSuccess: async ({ workflow }) => {
|
|
|
|
await router.replace("/workflows/" + workflow.id);
|
|
|
|
},
|
|
|
|
onError: (err) => {
|
|
|
|
if (err instanceof HttpError) {
|
|
|
|
const message = `${err.statusCode}: ${err.message}`;
|
|
|
|
showToast(message, "error");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err.data?.code === "UNAUTHORIZED") {
|
|
|
|
const message = `${err.data.code}: You are not able to create this workflow`;
|
|
|
|
showToast(message, "error");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<LicenseRequired>
|
|
|
|
{!isLoading ? (
|
|
|
|
data?.workflows && data?.workflows.length > 0 ? (
|
2023-02-05 23:09:27 +00:00
|
|
|
<div className="space-y-4">
|
2022-08-31 19:42:37 +00:00
|
|
|
{sortedWorkflows.map((workflow) => {
|
|
|
|
return <WorkflowListItem key={workflow.id} workflow={workflow} eventType={props.eventType} />;
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
) : (
|
2022-10-07 09:25:30 +00:00
|
|
|
<div className="pt-4 before:border-0">
|
|
|
|
<EmptyScreen
|
2023-01-23 23:08:01 +00:00
|
|
|
Icon={FiZap}
|
2022-10-07 09:25:30 +00:00
|
|
|
headline={t("workflows")}
|
|
|
|
description={t("no_workflows_description")}
|
|
|
|
buttonRaw={
|
|
|
|
<Button
|
|
|
|
target="_blank"
|
|
|
|
color="secondary"
|
2023-02-27 07:24:43 +00:00
|
|
|
onClick={() => createMutation.mutate({ teamId: eventType.team?.id })}
|
2022-10-07 09:25:30 +00:00
|
|
|
loading={createMutation.isLoading}>
|
|
|
|
{t("create_workflow")}
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-08-31 19:42:37 +00:00
|
|
|
)
|
|
|
|
) : (
|
2022-09-30 18:39:23 +00:00
|
|
|
<SkeletonLoader />
|
2022-08-31 19:42:37 +00:00
|
|
|
)}
|
|
|
|
</LicenseRequired>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default EventWorkflowsTab;
|