"use client"; import { ActionBlock, AddActionPanel, AlertRuleSourceFields, AlertRuleTriggerFields } from "@app/components/alert-rule-editor/AlertRuleFields"; import { SettingsContainer } from "@app/components/Settings"; import { Button } from "@app/components/ui/button"; import { Card, CardContent } from "@app/components/ui/card"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "@app/components/ui/form"; import { Input } from "@app/components/ui/input"; import { toast } from "@app/hooks/useToast"; import { buildFormSchema, defaultFormValues, formValuesToApiPayload, type AlertRuleFormValues } from "@app/lib/alertRuleForm"; import { createApiClient, formatAxiosError } from "@app/lib/api"; import { useEnvContext } from "@app/hooks/useEnvContext"; import type { CreateAlertRuleResponse } from "@server/routers/alertRule/types"; import type { AxiosResponse } from "axios"; import { zodResolver } from "@hookform/resolvers/zod"; import { ChevronLeft, Cog, Flag, Zap } from "lucide-react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useMemo, useState, type ReactNode } from "react"; import { useFieldArray, useForm, type Resolver } from "react-hook-form"; import { useTranslations } from "next-intl"; import { PaidFeaturesAlert } from "@app/components/PaidFeaturesAlert"; import { SwitchInput } from "@app/components/SwitchInput"; import { tierMatrix } from "@server/lib/billing/tierMatrix"; import { Badge } from "../ui/badge"; const FORM_ID = "alert-rule-form"; type StepAccent = { labelClass: string; icon: typeof Flag; }; type AlertRuleGraphEditorProps = { orgId: string; alertRuleId?: number; initialValues: AlertRuleFormValues; isNew: boolean; disabled?: boolean; }; function VerticalRuleStep({ stepNumber, isLast, title, accent, children }: { stepNumber: number; isLast: boolean; title: string; accent: StepAccent; children: ReactNode; }) { const Icon = accent.icon; return (
  • {stepNumber}
    {!isLast && (
    )}
    {title}
    {children}
  • ); } export default function AlertRuleGraphEditor({ orgId, alertRuleId, initialValues, isNew, disabled = false }: AlertRuleGraphEditorProps) { const t = useTranslations(); const router = useRouter(); const api = createApiClient(useEnvContext()); const [isSaving, setIsSaving] = useState(false); const schema = useMemo(() => buildFormSchema(t), [t]); const form = useForm({ resolver: zodResolver(schema) as Resolver, defaultValues: initialValues ?? defaultFormValues() }); const { fields, append, remove, update } = useFieldArray({ control: form.control, name: "actions" }); const onSubmit = form.handleSubmit(async (values) => { setIsSaving(true); try { const payload = formValuesToApiPayload(values); if (isNew) { const res = await api.put< AxiosResponse >(`/org/${orgId}/alert-rule`, payload); toast({ title: t("alertingRuleSaved"), description: t("alertingRuleSavedCreatedDescription") }); router.replace( `/${orgId}/settings/alerting/${res.data.data.alertRuleId}` ); } else { await api.post( `/org/${orgId}/alert-rule/${alertRuleId}`, payload ); toast({ title: t("alertingRuleSaved"), description: t("alertingRuleSavedUpdatedDescription") }); } } catch (e) { toast({ title: t("error"), description: formatAxiosError(e), variant: "destructive" }); } finally { setIsSaving(false); } }); return (
      { if (type === "notify") { append({ type: "notify", userTags: [], roleTags: [], emailTags: [] }); } else { append({ type: "webhook", url: "", method: "POST", headers: [ { key: "", value: "" } ], authType: "none", bearerToken: "", basicCredentials: "", customHeaderName: "", customHeaderValue: "", useBodyTemplate: false, bodyTemplate: "" }); } }} /> {fields.length > 0 && (
      )} {fields.map((f, index) => (
      {index > 0 && (
      )} remove(index) } onUpdate={(val) => update(index, val) } canRemove />
      ))}
    ); }