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";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { Button, EmptyScreen, Icon, showToast, Switch, Tooltip } from "@calcom/ui";
|
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";
|
2022-08-31 19:42:37 +00:00
|
|
|
import { WorkflowType } from "./WorkflowListPage";
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
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:
|
|
|
|
sendTo.add(t("organizer_name_workflow"));
|
|
|
|
break;
|
|
|
|
case WorkflowActions.EMAIL_ATTENDEE:
|
|
|
|
sendTo.add(t("attendee_name_workflow"));
|
|
|
|
break;
|
|
|
|
case WorkflowActions.SMS_ATTENDEE:
|
|
|
|
sendTo.add(t("attendee_name_workflow"));
|
|
|
|
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 (
|
2022-09-07 14:05:25 +00:00
|
|
|
<div className="mb-4 flex w-full items-center overflow-hidden rounded-md border border-gray-200 p-6 px-3 md:p-6">
|
|
|
|
<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",
|
2022-08-31 19:42:37 +00:00
|
|
|
workflow.name && isActive ? "text-gray-900" : "text-neutral-500"
|
|
|
|
)}>
|
|
|
|
{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">
|
2022-08-31 19:42:37 +00:00
|
|
|
<Link href={`/workflows/${workflow.id}`} passHref={true}>
|
|
|
|
<a target="_blank">
|
2022-09-07 14:05:25 +00:00
|
|
|
<Button type="button" color="minimal" className="mr-4">
|
2023-01-04 07:38:45 +00:00
|
|
|
<div className="hidden ltr:mr-2 rtl:ml-2 sm:block">{t("edit")}</div>
|
2022-09-06 03:29:00 +00:00
|
|
|
<Icon.FiExternalLink className="-mt-[2px] h-4 w-4 stroke-2 text-gray-600" />
|
2022-08-31 19:42:37 +00:00
|
|
|
</Button>
|
|
|
|
</a>
|
|
|
|
</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;
|
|
|
|
};
|
|
|
|
workflows: WorkflowType[];
|
|
|
|
};
|
|
|
|
|
|
|
|
function EventWorkflowsTab(props: Props) {
|
|
|
|
const { workflows } = props;
|
|
|
|
const { t } = useLocale();
|
2022-11-10 23:40:01 +00:00
|
|
|
const { data, isLoading } = trpc.viewer.workflows.list.useQuery();
|
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]);
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const createMutation = trpc.viewer.workflows.createV2.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 ? (
|
2022-09-05 15:15:26 +00:00
|
|
|
<div className="mt-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
|
|
|
|
Icon={Icon.FiZap}
|
|
|
|
headline={t("workflows")}
|
|
|
|
description={t("no_workflows_description")}
|
|
|
|
buttonRaw={
|
|
|
|
<Button
|
|
|
|
target="_blank"
|
|
|
|
color="secondary"
|
|
|
|
onClick={() => createMutation.mutate()}
|
|
|
|
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;
|