"use client"; import ConfirmDeleteDialog from "@app/components/ConfirmDeleteDialog"; import { PaidFeaturesAlert } from "@app/components/PaidFeaturesAlert"; import { Button } from "@app/components/ui/button"; import { DataTable, ExtendedColumnDef } from "@app/components/ui/data-table"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@app/components/ui/dropdown-menu"; import { Switch } from "@app/components/ui/switch"; import { toast } from "@app/hooks/useToast"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { usePaidStatus } from "@app/hooks/usePaidStatus"; import { createApiClient, formatAxiosError } from "@app/lib/api"; import { orgQueries } from "@app/lib/queries"; import { tierMatrix } from "@server/lib/billing/tierMatrix"; import { ArrowUpDown, MoreHorizontal } from "lucide-react"; import moment from "moment"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useTranslations } from "next-intl"; import { useState } from "react"; import { useQuery, useQueryClient } from "@tanstack/react-query"; type AlertingRulesTableProps = { orgId: string; }; type AlertRuleRow = { alertRuleId: number; orgId: string; name: string; eventType: string; enabled: boolean; cooldownSeconds: number; lastTriggeredAt: number | null; createdAt: number; updatedAt: number; siteIds: number[]; healthCheckIds: number[]; }; function ruleHref(orgId: string, ruleId: number) { return `/${orgId}/settings/alerting/${ruleId}`; } function sourceSummary( rule: AlertRuleRow, t: (k: string, o?: Record) => string ) { if ( rule.eventType === "site_online" || rule.eventType === "site_offline" ) { return t("alertingSummarySites", { count: rule.siteIds.length }); } return t("alertingSummaryHealthChecks", { count: rule.healthCheckIds.length }); } function triggerLabel( rule: AlertRuleRow, t: (k: string) => string ) { switch (rule.eventType) { case "site_online": return t("alertingTriggerSiteOnline"); case "site_offline": return t("alertingTriggerSiteOffline"); case "site_toggle": return t("alertingTriggerSiteToggle"); case "health_check_healthy": return t("alertingTriggerHcHealthy"); case "health_check_unhealthy": return t("alertingTriggerHcUnhealthy"); case "health_check_toggle": return t("alertingTriggerHcToggle"); default: return rule.eventType; } } export default function AlertingRulesTable({ orgId }: AlertingRulesTableProps) { const router = useRouter(); const t = useTranslations(); const api = createApiClient(useEnvContext()); const queryClient = useQueryClient(); const { isPaidUser } = usePaidStatus(); const isPaid = isPaidUser(tierMatrix.alertingRules); const [deleteOpen, setDeleteOpen] = useState(false); const [selected, setSelected] = useState(null); const [togglingId, setTogglingId] = useState(null); const { data: rows = [], isLoading, refetch, isRefetching } = useQuery(orgQueries.alertRules({ orgId })); const invalidate = () => queryClient.invalidateQueries(orgQueries.alertRules({ orgId })); const setEnabled = async (rule: AlertRuleRow, enabled: boolean) => { setTogglingId(rule.alertRuleId); try { await api.post(`/org/${orgId}/alert-rule/${rule.alertRuleId}`, { enabled }); await invalidate(); } catch (e) { toast({ title: t("error"), description: formatAxiosError(e), variant: "destructive" }); } finally { setTogglingId(null); } }; const confirmDelete = async () => { if (!selected) return; try { await api.delete( `/org/${orgId}/alert-rule/${selected.alertRuleId}` ); await invalidate(); toast({ title: t("alertingRuleDeleted") }); } catch (e) { toast({ title: t("error"), description: formatAxiosError(e), variant: "destructive" }); } finally { setDeleteOpen(false); setSelected(null); } }; const columns: ExtendedColumnDef[] = [ { accessorKey: "name", enableHiding: false, friendlyName: t("name"), header: ({ column }) => ( ), cell: ({ row }) => ( {row.original.name} ) }, { id: "source", friendlyName: t("alertingColumnSource"), header: () => ( {t("alertingColumnSource")} ), cell: ({ row }) => {sourceSummary(row.original, t)} }, { id: "trigger", friendlyName: t("alertingColumnTrigger"), header: () => ( {t("alertingColumnTrigger")} ), cell: ({ row }) => {triggerLabel(row.original, t)} }, { accessorKey: "enabled", friendlyName: t("alertingColumnEnabled"), header: () => ( {t("alertingColumnEnabled")} ), cell: ({ row }) => { const r = row.original; return ( setEnabled(r, v)} /> ); } }, { accessorKey: "createdAt", friendlyName: t("createdAt"), header: () => {t("createdAt")}, cell: ({ row }) => ( {moment(row.original.createdAt).format("lll")} ) }, { id: "rowActions", enableHiding: false, header: () => , cell: ({ row }) => { const r = row.original; return (
{ setSelected(r); setDeleteOpen(true); }} > {t("delete")}
); } } ]; return ( <> {selected && ( { setDeleteOpen(val); if (!val) setSelected(null); }} dialog={

{t("alertingDeleteQuestion")}

} buttonText={t("delete")} onConfirm={confirmDelete} string={selected.name} title={t("alertingDeleteRule")} /> )} { router.push(`/${orgId}/settings/alerting/create`); }} onRefresh={() => refetch()} isRefreshing={isRefetching || isLoading} addButtonText={t("alertingAddRule")} enableColumnVisibility stickyLeftColumn="name" stickyRightColumn="rowActions" /> ); }