import React, { useState } from "react"; import { Icon } from "../../tremor/Icon"; import { Label, Text, Title } from "../../tremor/Text"; import { Card } from "../../tremor/Card"; import { Button } from "../../tremor/Button"; import { useBackend } from "../../layouts"; import { Automation } from "@onvo-ai/js"; import { SearchSelect } from "../../tremor/SearchSelect"; import { CronInput } from "../../tremor/CronInput"; import { Input } from "../../tremor/Input"; import { Timezones } from "./timezones"; import { Switch } from "../../tremor/Switch"; import { getSchedule, stringToArray } from "cron-converter"; import dayjs from "dayjs"; import relativeTime from "dayjs/plugin/relativeTime"; import utc from "dayjs/plugin/utc"; import timezone from "dayjs/plugin/timezone"; import { toast } from "sonner"; import { Panel, PanelGroup, PanelResizeHandle } from "react-resizable-panels"; import { useAutomationsModal } from "./useAutomationsModal"; import { AutomationHistory } from "./AutomationHistory"; import { ClockIcon, PhotoIcon, TableCellsIcon, DocumentIcon, PresentationChartBarIcon, DocumentChartBarIcon, TrashIcon } from "@heroicons/react/24/outline"; import { AutomationConditionCard } from "./AutomationConditionCard"; import { AutomationEmailCard } from "./AutomationEmailCard"; import { AutomationWebhookCard } from "./AutomationWebhookCard"; import { Popover, PopoverTrigger, PopoverContent, PopoverClose } from "../../tremor/Popover"; dayjs.extend(relativeTime); dayjs.extend(utc); dayjs.extend(timezone); export const AutomationEditor: React.FC<{}> = ({ }) => { const { selectedAutomation, setSelectedAutomation, getAllAutomations } = useAutomationsModal(); const { backend } = useBackend(); const [loading, setLoading] = useState(false); const updateAutomation = async () => { if (!backend) return; if (!selectedAutomation) return; const arr = stringToArray(selectedAutomation.schedule); let sch = getSchedule(arr, new Date(), selectedAutomation.timezone); let next_run_at = sch.next().toISO(); toast.promise(() => { return backend.automations.update(selectedAutomation.id, { ...selectedAutomation, next_run_at: next_run_at }) }, { loading: "Updating automation...", success: () => { return "Automation updated" }, error: (e: any) => { return "Error updating automation: " + e.message } }); } const updateSelectedAutomation = (update: Partial) => { // @ts-ignore setSelectedAutomation({ ...selectedAutomation, ...update }); } const deleteAutomation = async () => { if (!backend || !selectedAutomation) return; setLoading(true); await backend.automations.delete(selectedAutomation.id); getAllAutomations(backend, selectedAutomation.dashboard); setSelectedAutomation(null); } return (
Title updateSelectedAutomation({ title: e.target.value })} /> Are you sure you want to delete this automation?
updateSelectedAutomation({ enabled: val })} />
When
updateSelectedAutomation({ schedule: val })} />
Timezone
{ updateSelectedAutomation({ timezone: val }) }} items={Timezones} />
Report format
{[{ title: "PDF", icon: DocumentIcon, id: "pdf" }, { title: "Powerpoint", icon: PresentationChartBarIcon, id: "pptx" }, { title: "Excel", icon: TableCellsIcon, id: "xlsx" }, { title: "CSV", icon: TableCellsIcon, id: "csv" }, { title: "Image", icon: PhotoIcon, id: "png" }].map(format => ( { updateSelectedAutomation({ output_format: format.id }) }} className={"onvo-cursor-pointer onvo-flex-grow !onvo-px-0 !onvo-py-2 onvo-flex onvo-flex-col onvo-justify-center onvo-items-center " + (selectedAutomation?.output_format === format.id ? "!onvo-border-blue-500 !onvo-shadow-blue-500 !onvo-text-blue-500" : "dark:onvo-text-slate-400")}> {format.title} ))}
{selectedAutomation?.method === "email" ? : }
) }