import { useEffect, useState } from "react"; import { useBackend } from "../../layouts"; import { AutomationRun } from "@onvo-ai/js"; import { Empty } from "../Empty"; import { ArrowPathIcon, BoltSlashIcon, CheckCircleIcon, EnvelopeIcon, ExclamationTriangleIcon } from "@heroicons/react/20/solid"; import React from "react"; import { Title, Text } from "../../tremor/Text"; import { Icon } from "../../tremor/Icon"; import { useAutomationsModal } from "./useAutomationsModal"; import dayjs from "dayjs"; import relativeTime from "dayjs/plugin/relativeTime"; import utc from "dayjs/plugin/utc"; import timezone from "dayjs/plugin/timezone"; import { Button } from "../../tremor/Button"; import { CalendarIcon, LinkIcon } from "@heroicons/react/24/outline"; import { toast } from "sonner"; import { Badge } from "../../tremor/Badge"; import { getSchedule, stringToArray } from "cron-converter"; import { Tooltip } from "../../tremor/Tooltip"; dayjs.extend(relativeTime); dayjs.extend(utc); dayjs.extend(timezone); export const AutomationHistory: React.FC<{}> = ({ }) => { const { selectedAutomation } = useAutomationsModal(); const { backend } = useBackend(); const [loading, setLoading] = useState(false); const [automationRuns, setAutomationRuns] = useState([]); useEffect(() => { if (!selectedAutomation) return; getAutomationRuns(selectedAutomation.id); }, [selectedAutomation]); const getAutomationRuns = async (id: string) => { if (!backend || !id) return; const runs = await backend.automation(id).getRuns(); setAutomationRuns(runs || []); } const testAutomation = () => { const id = selectedAutomation?.id; if (!backend || !id) return; if (!selectedAutomation) return; setLoading(true); const arr = stringToArray(selectedAutomation.schedule); let sch = getSchedule(arr, new Date(), selectedAutomation.timezone); let next_run_at = sch.next().toISO(); toast.promise(async () => { await backend.automations.update(selectedAutomation.id, { ...selectedAutomation, next_run_at: next_run_at }); return backend?.automation(id).test(); }, { loading: "Running automation...", success: () => { setLoading(false); getAutomationRuns(id); return "Automation ran successfully!" }, error: (err) => { setLoading(false); return "Failed to run automation: " + err.message; } }) } return (
Automation history
{(automationRuns.length === 0 && !loading) ? ( } /> ) : (
{loading && (
Testing automation...
)} { automationRuns.map((a) => (
{a.status === "success" ? ( ) : ( )}
{a.status === "success" ? "Ran successfully " : "Run failed "}{`${dayjs(new Date(a.run_at)).fromNow()}`}
{a.status === "failure" && ( Automation failed )} {a.status === "success" && a.conditions_passed === false && ( Conditions failed )} {a.status === "success" && a.conditions_passed === true && ( Conditions passed )} {a.status === "success" && a.recipient_emails && a.recipient_emails.length >= 0 && ( {(a.recipient_emails || []).map(email => (
  • {email}
  • ))} }> {(a.recipient_emails || []).length} emails sent
    )} {a.status === "success" && a.webhook_sent === true && ( Webhook sent )}
    )) }
    )}
    ) }