// 告警静默规则管理 Tab — 列表 + 创建/编辑/删除/启停 // Logan.Li import React, { useState, useEffect, useCallback } from 'react'; import { VolumeX, Plus, RefreshCw, Clock, Calendar, Repeat, Edit, Trash2 } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Card, CardContent } from '@/components/ui/card'; import { Switch } from '@/components/ui/switch'; import { toast } from 'sonner'; import { useConfirm } from '@/components/ui/confirm-dialog'; import { alertCenterApi } from '@/api'; import { SilenceDialog } from './SilenceDialog'; const SILENCE_TYPE_LABELS: Record = { onetime: '一次性', daily: '每天', weekly: '每周', }; const WEEKDAY_LABELS = ['日', '一', '二', '三', '四', '五', '六']; function formatTime(t: string | Date): string { if (!t) return '—'; const d = typeof t === 'string' ? new Date(t) : t; if (isNaN(d.getTime())) return '—'; return d.toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', }); } const SilenceRuleTab: React.FC = () => { const confirm = useConfirm(); const [silences, setSilences] = useState([]); const [loading, setLoading] = useState(false); const [dialogOpen, setDialogOpen] = useState(false); const [editing, setEditing] = useState(null); const fetchSilences = useCallback(async () => { setLoading(true); try { const res: Record = await alertCenterApi.getSilences(); const list = res?.list || res?.data?.list || []; setSilences(list); } catch { toast.error('获取静默规则失败'); } finally { setLoading(false); } }, []); useEffect(() => { fetchSilences(); }, [fetchSilences]); const handleAdd = () => { setEditing(null); setDialogOpen(true); }; const handleEdit = (silence: any) => { setEditing(silence); setDialogOpen(true); }; const handleDelete = async (silence: any) => { const ok = await confirm({ title: '删除静默规则', description: `确定删除「${silence.name}」?删除后该规则匹配的告警将恢复通知。`, confirmText: '删除', variant: 'destructive', }); if (!ok) return; try { await alertCenterApi.deleteSilence(silence.id); toast.success('已删除'); fetchSilences(); } catch { toast.error('删除失败'); } }; const handleToggle = async (silence: any) => { try { await alertCenterApi.toggleSilence(silence.id); toast.success(silence.status === 1 ? '已禁用' : '已启用'); fetchSilences(); } catch { toast.error('操作失败'); } }; const isCurrentlyActive = (s: any): boolean => { if (s.is_active !== undefined) return s.is_active; if (s.status !== 1) return false; const now = new Date(); const st = new Date(s.start_time); const et = new Date(s.end_time); return now >= st && now <= et; }; return (
{/* 操作栏 */}
共 {silences.length} 条规则
{/* 规则列表 */} {loading && silences.length === 0 ? (

加载中...

) : silences.length === 0 ? (

暂无静默规则

点击「添加静默规则」创建维护窗口或免打扰时段

) : (
{silences.map((s) => { const active = isCurrentlyActive(s); const enabled = s.status === 1; const sType = s.silence_type || 'onetime'; const weekdayList: number[] = s.weekday_list || (() => { try { return JSON.parse(s.weekdays || '[]'); } catch { return []; } })(); return (
{/* 左侧信息 */}
{s.name} {/* 类型 Badge */} {sType === 'onetime' && } {sType === 'daily' && } {sType === 'weekly' && } {SILENCE_TYPE_LABELS[sType] || sType} {/* 活跃状态 */} {active && ( 生效中 )} {!enabled && ( 已禁用 )}
{s.description && (

{s.description}

)} {/* 时间窗口 */}
{sType === 'onetime' ? `${formatTime(s.start_time)} — ${formatTime(s.end_time)}` : `每日 ${formatTime(s.start_time)} — ${formatTime(s.end_time)}` } {sType === 'weekly' && weekdayList.length > 0 && ( {weekdayList.sort((a,b)=>a-b).map(d => ( {WEEKDAY_LABELS[d]} ))} )}
{/* 创建时间 */}
创建于 {formatTime(s.created_at)}
{/* 右侧操作 */}
handleToggle(s)} />
); })}
)} {/* 创建/编辑弹框 */}
); }; export default SilenceRuleTab;