import { CodeBracketIcon, TrashIcon } from "@heroicons/react/24/outline"; import { useBackend } from "../../layouts"; import { useAutomationsModal } from "./useAutomationsModal"; import { Icon } from "../../tremor/Icon"; import { Card } from "../../tremor/Card"; import React, { useMemo, useState } from "react"; import { Title, Text } from "../../tremor/Text"; import { Button } from "../../tremor/Button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../tremor/Select"; import { useDashboard } from "../../layouts/Dashboard/useDashboard"; import { Tabs, TabsList, TabsTrigger } from "../../tremor/Tabs"; import { Input } from "../../tremor/Input"; import { AutomationCondition } from "@onvo-ai/js"; const CONDITIONS = [{ value: "lt", label: "Lesser than" }, { value: "gt", label: "Greater than" }, { value: "eq", label: "Equal to" }, { value: "neq", label: "Not equal to" }, { value: "lte", label: "Lesser than or equal to" }, { value: "gte", label: "Greater than or equal to" }]; export const AutomationConditionCard: React.FC<{}> = ({ }) => { const { selectedAutomation, setSelectedAutomation } = useAutomationsModal(); const { widgets } = useDashboard(); const options = useMemo(() => { return widgets.filter(a => a && a.type === "metric").map(a => ({ label: a.title, value: a.id })) }, [widgets]); const updateCondition = (index: number, condition: Partial) => { const newConditions = (conditions.map((c, i) => { if (i === index) { return { ...c, ...condition } } return c; })); setSelectedAutomation({ conditions: newConditions }); } const addCondition = () => { const newConditions = [...conditions, { operand_id: options[0].value, reference_type: "static" as const, operator: "lt", reference_value: "10" }] setSelectedAutomation({ conditions: newConditions }); } const conditions = selectedAutomation?.conditions || []; return (
Condition
{conditions.length > 0 && (
{conditions.map((condition, index) => (
updateCondition(index, { reference_type: value as "static" | "dynamic" })}> Static Dynamic { condition.reference_type === "static" ? ( updateCondition(index, { reference_value: e.target.value })} />) : () } setSelectedAutomation({ conditions: conditions.filter((_, i) => i !== index) })} variant="shadow" className="!onvo-text-red-500 onvo-cursor-pointer" icon={TrashIcon} />
))}
)} {conditions.length > 1 && (
setSelectedAutomation({ condition_operator: value })}> AND OR {(selectedAutomation?.condition_operator || "and") === "and" ? (Report is only sent when all conditions are met) : (Report is sent when any condition is met)}
)}
); }