TelegramNotifications.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { InputNumber } from 'antd';
  2. import { CloudServerOutlined, ThunderboltOutlined, DesktopOutlined, DashboardOutlined, SafetyOutlined } from '@ant-design/icons';
  3. import type { AllSetting } from '@/models/setting';
  4. import { NotificationLayout } from './NotificationLayout';
  5. import { NotificationGroup } from './NotificationGroup';
  6. import type { NotificationGroupConfig } from './types';
  7. const GROUPS: NotificationGroupConfig[] = [
  8. {
  9. icon: <CloudServerOutlined />,
  10. title: 'eventGroupOutbound',
  11. events: [
  12. { key: 'outbound.down', label: 'eventOutboundDown', settingKey: '' },
  13. { key: 'outbound.up', label: 'eventOutboundUp', settingKey: '' },
  14. ],
  15. },
  16. {
  17. icon: <ThunderboltOutlined />,
  18. title: 'eventGroupXray',
  19. events: [
  20. { key: 'xray.crash', label: 'eventXrayCrash', settingKey: '' },
  21. ],
  22. },
  23. {
  24. icon: <DesktopOutlined />,
  25. title: 'eventGroupNode',
  26. events: [
  27. { key: 'node.down', label: 'eventNodeDown', settingKey: '' },
  28. { key: 'node.up', label: 'eventNodeUp', settingKey: '' },
  29. ],
  30. },
  31. {
  32. icon: <DashboardOutlined />,
  33. title: 'eventGroupSystem',
  34. events: [
  35. {
  36. key: 'cpu.high',
  37. label: 'eventCPUHigh',
  38. settingKey: 'tgCpu',
  39. extra: ({ value, onChange }) => (
  40. <InputNumber size="small" min={0} max={100} value={value} onChange={onChange} style={{ width: 80 }} />
  41. ),
  42. },
  43. {
  44. key: 'memory.high',
  45. label: 'eventMemoryHigh',
  46. settingKey: 'tgMemory',
  47. extra: ({ value, onChange }) => (
  48. <InputNumber size="small" min={0} max={100} value={value} onChange={onChange} style={{ width: 80 }} />
  49. ),
  50. },
  51. ],
  52. },
  53. {
  54. icon: <SafetyOutlined />,
  55. title: 'eventGroupSecurity',
  56. events: [
  57. { key: 'login.attempt', label: 'eventLoginAttempt', settingKey: '' },
  58. ],
  59. },
  60. ];
  61. interface Props {
  62. allSetting: AllSetting;
  63. updateSetting: (patch: Partial<AllSetting>) => void;
  64. }
  65. export function TelegramNotifications({ allSetting, updateSetting }: Props) {
  66. const events = allSetting.tgEnabledEvents || '';
  67. const selected = events ? events.split(',').map((s) => s.trim()).filter(Boolean) : [];
  68. function toggle(key: string) {
  69. const next = selected.includes(key)
  70. ? selected.filter((e) => e !== key)
  71. : [...selected, key];
  72. updateSetting({ tgEnabledEvents: next.join(',') });
  73. }
  74. function toggleAll(keys: string[]) {
  75. const allSelected = keys.every((v) => selected.includes(v));
  76. const next = allSelected
  77. ? selected.filter((v) => !keys.includes(v))
  78. : [...new Set([...selected, ...keys])];
  79. updateSetting({ tgEnabledEvents: next.join(',') });
  80. }
  81. return (
  82. <NotificationLayout>
  83. {GROUPS.map((group, i) => (
  84. <NotificationGroup
  85. key={i}
  86. config={group}
  87. selected={selected}
  88. onToggle={toggle}
  89. onToggleAll={toggleAll}
  90. allSetting={allSetting}
  91. updateSetting={updateSetting}
  92. />
  93. ))}
  94. </NotificationLayout>
  95. );
  96. }