AddNotificationModal.tsx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. import { useState, useEffect } from 'react';
  2. import { useMutation, useQueryClient, useQuery } from '@tanstack/react-query';
  3. import { useTranslation } from 'react-i18next';
  4. import { X, Save, Loader2, Send, CheckCircle, XCircle } from 'lucide-react';
  5. import { api } from '../api/client';
  6. import type { NotificationProvider, NotificationProviderCreate, NotificationProviderUpdate, ProviderType } from '../api/client';
  7. import { Button } from './Button';
  8. import { Toggle } from './Toggle';
  9. interface AddNotificationModalProps {
  10. provider?: NotificationProvider | null;
  11. onClose: () => void;
  12. }
  13. const PROVIDER_VALUES: ProviderType[] = ['email', 'telegram', 'discord', 'ntfy', 'pushover', 'callmebot', 'webhook', 'homeassistant'];
  14. export function AddNotificationModal({ provider, onClose }: AddNotificationModalProps) {
  15. const { t } = useTranslation();
  16. const queryClient = useQueryClient();
  17. const isEditing = !!provider;
  18. const [name, setName] = useState(provider?.name || '');
  19. const [providerType, setProviderType] = useState<ProviderType>(provider?.provider_type || 'email');
  20. const [printerId, setPrinterId] = useState<number | null>(provider?.printer_id || null);
  21. const [quietHoursEnabled, setQuietHoursEnabled] = useState(provider?.quiet_hours_enabled || false);
  22. const [quietHoursStart, setQuietHoursStart] = useState(provider?.quiet_hours_start || '22:00');
  23. const [quietHoursEnd, setQuietHoursEnd] = useState(provider?.quiet_hours_end || '07:00');
  24. // Daily digest
  25. const [dailyDigestEnabled, setDailyDigestEnabled] = useState(provider?.daily_digest_enabled || false);
  26. const [dailyDigestTime, setDailyDigestTime] = useState(provider?.daily_digest_time || '08:00');
  27. // Event toggles
  28. const [onPrintStart, setOnPrintStart] = useState(provider?.on_print_start ?? false);
  29. const [onPrintComplete, setOnPrintComplete] = useState(provider?.on_print_complete ?? true);
  30. const [onPrintFailed, setOnPrintFailed] = useState(provider?.on_print_failed ?? true);
  31. const [onPrintStopped, setOnPrintStopped] = useState(provider?.on_print_stopped ?? true);
  32. const [onPrintProgress, setOnPrintProgress] = useState(provider?.on_print_progress ?? false);
  33. const [onPrinterOffline, setOnPrinterOffline] = useState(provider?.on_printer_offline ?? false);
  34. const [onPrinterError, setOnPrinterError] = useState(provider?.on_printer_error ?? false);
  35. const [onFilamentLow, setOnFilamentLow] = useState(provider?.on_filament_low ?? false);
  36. const [onMaintenanceDue, setOnMaintenanceDue] = useState(provider?.on_maintenance_due ?? false);
  37. const [onStockReorderAlert, setOnStockReorderAlert] = useState(provider?.on_stock_reorder_alert ?? false);
  38. const [onStockBreakAlert, setOnStockBreakAlert] = useState(provider?.on_stock_break_alert ?? false);
  39. const [onBedCooled, setOnBedCooled] = useState(provider?.on_bed_cooled ?? false);
  40. const [onFirstLayerComplete, setOnFirstLayerComplete] = useState(provider?.on_first_layer_complete ?? false);
  41. // Provider-specific config (scalar fields only — event_priorities is split out
  42. // into its own state because it's an object, not a string).
  43. const [config, setConfig] = useState<Record<string, string>>(
  44. provider?.config
  45. ? Object.fromEntries(
  46. Object.entries(provider.config)
  47. .filter(([k]) => k !== 'event_priorities')
  48. .map(([k, v]) => [k, String(v)]),
  49. )
  50. : {},
  51. );
  52. // Per-event ntfy priority (#990). Map of event key → 1-5. Persisted into
  53. // config.event_priorities on save; only sent when the provider is ntfy.
  54. const initialEventPriorities = (() => {
  55. const raw = provider?.config?.event_priorities;
  56. if (!raw || typeof raw !== 'object') return {} as Record<string, number>;
  57. const out: Record<string, number> = {};
  58. for (const [k, v] of Object.entries(raw as Record<string, unknown>)) {
  59. const n = Number(v);
  60. if (Number.isInteger(n) && n >= 1 && n <= 5) out[k] = n;
  61. }
  62. return out;
  63. })();
  64. const [eventPriorities, setEventPriorities] = useState<Record<string, number>>(initialEventPriorities);
  65. const [testResult, setTestResult] = useState<{ success: boolean; message: string } | null>(null);
  66. const [error, setError] = useState<string | null>(null);
  67. // Fetch printers for linking
  68. const { data: printers } = useQuery({
  69. queryKey: ['printers'],
  70. queryFn: api.getPrinters,
  71. });
  72. // Close on Escape key
  73. useEffect(() => {
  74. const handleKeyDown = (e: KeyboardEvent) => {
  75. if (e.key === 'Escape') onClose();
  76. };
  77. window.addEventListener('keydown', handleKeyDown);
  78. return () => window.removeEventListener('keydown', handleKeyDown);
  79. }, [onClose]);
  80. // Test configuration mutation
  81. const testMutation = useMutation({
  82. mutationFn: () => api.testNotificationConfig({ provider_type: providerType, config }),
  83. onSuccess: (result) => {
  84. setTestResult(result);
  85. setError(null);
  86. },
  87. onError: (err: Error) => {
  88. setTestResult({ success: false, message: err.message });
  89. },
  90. });
  91. // Create mutation
  92. const createMutation = useMutation({
  93. mutationFn: (data: NotificationProviderCreate) => api.createNotificationProvider(data),
  94. onSuccess: () => {
  95. queryClient.invalidateQueries({ queryKey: ['notification-providers'] });
  96. onClose();
  97. },
  98. onError: (err: Error) => {
  99. setError(err.message);
  100. },
  101. });
  102. // Update mutation
  103. const updateMutation = useMutation({
  104. mutationFn: (data: NotificationProviderUpdate) => api.updateNotificationProvider(provider!.id, data),
  105. onSuccess: () => {
  106. queryClient.invalidateQueries({ queryKey: ['notification-providers'] });
  107. onClose();
  108. },
  109. onError: (err: Error) => {
  110. setError(err.message);
  111. },
  112. });
  113. const handleSubmit = (e: React.FormEvent) => {
  114. e.preventDefault();
  115. setError(null);
  116. if (!name.trim()) {
  117. setError(t('notifications.nameRequired'));
  118. return;
  119. }
  120. // Validate provider-specific config
  121. const requiredFields = getRequiredFields(providerType);
  122. for (const field of requiredFields) {
  123. if (!config[field.key]?.trim()) {
  124. setError(t('notifications.fieldRequired', { field: field.label }));
  125. return;
  126. }
  127. }
  128. const finalConfig: Record<string, unknown> =
  129. providerType === 'ntfy' && Object.keys(eventPriorities).length > 0
  130. ? { ...config, event_priorities: eventPriorities }
  131. : config;
  132. const data = {
  133. name: name.trim(),
  134. provider_type: providerType,
  135. config: finalConfig,
  136. printer_id: printerId,
  137. quiet_hours_enabled: quietHoursEnabled,
  138. quiet_hours_start: quietHoursEnabled ? quietHoursStart : null,
  139. quiet_hours_end: quietHoursEnabled ? quietHoursEnd : null,
  140. // Daily digest
  141. daily_digest_enabled: dailyDigestEnabled,
  142. daily_digest_time: dailyDigestEnabled ? dailyDigestTime : null,
  143. // Event toggles
  144. on_print_start: onPrintStart,
  145. on_print_complete: onPrintComplete,
  146. on_print_failed: onPrintFailed,
  147. on_print_stopped: onPrintStopped,
  148. on_print_progress: onPrintProgress,
  149. on_printer_offline: onPrinterOffline,
  150. on_printer_error: onPrinterError,
  151. on_filament_low: onFilamentLow,
  152. on_maintenance_due: onMaintenanceDue,
  153. on_stock_reorder_alert: onStockReorderAlert,
  154. on_stock_break_alert: onStockBreakAlert,
  155. on_bed_cooled: onBedCooled,
  156. on_first_layer_complete: onFirstLayerComplete,
  157. };
  158. if (isEditing) {
  159. updateMutation.mutate(data);
  160. } else {
  161. createMutation.mutate(data);
  162. }
  163. };
  164. const isPending = createMutation.isPending || updateMutation.isPending;
  165. // Get config fields for each provider type
  166. const getConfigFields = (type: ProviderType) => {
  167. switch (type) {
  168. case 'callmebot':
  169. return [
  170. { key: 'phone', label: 'Phone Number', placeholder: '+1234567890', type: 'text', required: true },
  171. { key: 'apikey', label: 'API Key', placeholder: 'Your CallMeBot API key', type: 'text', required: true },
  172. ];
  173. case 'ntfy':
  174. return [
  175. { key: 'server', label: 'Server URL', placeholder: 'https://ntfy.sh', type: 'text', required: false },
  176. { key: 'topic', label: 'Topic', placeholder: 'my-bambuddy', type: 'text', required: true },
  177. { key: 'auth_token', label: 'Auth Token', placeholder: 'Optional authentication', type: 'password', required: false },
  178. ];
  179. case 'pushover':
  180. return [
  181. { key: 'user_key', label: 'User Key', placeholder: 'Your Pushover user key', type: 'text', required: true },
  182. { key: 'app_token', label: 'App Token', placeholder: 'Your Pushover app token', type: 'text', required: true },
  183. { key: 'priority', label: 'Priority', placeholder: '0 (normal)', type: 'number', required: false },
  184. ];
  185. case 'telegram':
  186. return [
  187. { key: 'bot_token', label: 'Bot Token', placeholder: 'Bot token from @BotFather', type: 'password', required: true },
  188. { key: 'chat_id', label: 'Chat ID', placeholder: 'Your chat or group ID', type: 'text', required: true },
  189. ];
  190. case 'email':
  191. return [
  192. { key: 'smtp_server', label: 'SMTP Server', placeholder: 'smtp.gmail.com', type: 'text', required: true },
  193. { key: 'smtp_port', label: 'SMTP Port', placeholder: '587', type: 'number', required: false },
  194. { key: 'security', label: 'Security', type: 'select', required: false, options: [
  195. { value: 'starttls', label: 'STARTTLS (Port 587)' },
  196. { value: 'ssl', label: 'SSL/TLS (Port 465)' },
  197. { value: 'none', label: 'None (Port 25)' },
  198. ]},
  199. { key: 'auth_enabled', label: 'Authentication', type: 'select', required: false, options: [
  200. { value: 'true', label: 'Enabled' },
  201. { value: 'false', label: 'Disabled' },
  202. ]},
  203. { key: 'username', label: 'Username', placeholder: 'your@email.com', type: 'text', required: false },
  204. { key: 'password', label: 'Password', placeholder: 'App password', type: 'password', required: false },
  205. { key: 'from_email', label: 'From Email', placeholder: 'your@email.com', type: 'text', required: true },
  206. { key: 'to_email', label: 'To Email', placeholder: 'recipient@email.com', type: 'text', required: true },
  207. ];
  208. case 'discord':
  209. return [
  210. { key: 'webhook_url', label: 'Webhook URL', placeholder: 'https://discord.com/api/webhooks/...', type: 'text', required: true },
  211. ];
  212. case 'webhook':
  213. return [
  214. { key: 'webhook_url', label: 'Webhook URL', placeholder: 'https://example.com/webhook', type: 'text', required: true },
  215. { key: 'payload_format', label: 'Payload Format', type: 'select', required: false, options: [
  216. { value: 'generic', label: 'Generic JSON' },
  217. { value: 'slack', label: 'Slack / Mattermost' },
  218. ]},
  219. { key: 'auth_header', label: 'Authorization', placeholder: 'Bearer token (optional)', type: 'password', required: false },
  220. { key: 'field_title', label: 'Title Field Name', placeholder: 'title', type: 'text', required: false, showIf: (cfg: Record<string, string>) => cfg.payload_format !== 'slack' },
  221. { key: 'field_message', label: 'Message Field Name', placeholder: 'message', type: 'text', required: false, showIf: (cfg: Record<string, string>) => cfg.payload_format !== 'slack' },
  222. ];
  223. case 'homeassistant':
  224. return [
  225. { key: 'service', label: 'Home Assistant Service', placeholder: 'notify.mobile_app_myphone', type: 'text', required: false },
  226. ];
  227. default:
  228. return [];
  229. }
  230. };
  231. const getRequiredFields = (type: ProviderType) => {
  232. return getConfigFields(type).filter(f => f.required);
  233. };
  234. const configFields = getConfigFields(providerType);
  235. return (
  236. <div
  237. className="fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4 overflow-y-auto"
  238. onClick={onClose}
  239. >
  240. <div
  241. className="bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary w-full max-w-lg my-8 max-h-[90vh] overflow-y-auto"
  242. onClick={(e) => e.stopPropagation()}
  243. >
  244. {/* Header */}
  245. <div className="flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary">
  246. <h2 className="text-lg font-semibold text-white">
  247. {isEditing ? t('notifications.editTitle') : t('notifications.addTitle')}
  248. </h2>
  249. <button
  250. onClick={onClose}
  251. className="text-bambu-gray hover:text-white transition-colors"
  252. >
  253. <X className="w-5 h-5" />
  254. </button>
  255. </div>
  256. {/* Form */}
  257. <form onSubmit={handleSubmit} className="p-6 space-y-4">
  258. {error && (
  259. <div className="p-3 bg-red-500/20 border border-red-500/50 rounded-lg text-sm text-red-400">
  260. {error}
  261. </div>
  262. )}
  263. {/* Name */}
  264. <div>
  265. <label className="block text-sm text-bambu-gray mb-1">{t('notifications.nameLabel')}</label>
  266. <input
  267. type="text"
  268. value={name}
  269. onChange={(e) => setName(e.target.value)}
  270. placeholder={t('notifications.namePlaceholder')}
  271. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  272. />
  273. </div>
  274. {/* Provider Type */}
  275. <div>
  276. <label className="block text-sm text-bambu-gray mb-1">{t('notifications.providerTypeLabel')}</label>
  277. <select
  278. value={providerType}
  279. onChange={(e) => {
  280. setProviderType(e.target.value as ProviderType);
  281. setConfig({}); // Reset config when changing type
  282. setTestResult(null);
  283. }}
  284. disabled={isEditing}
  285. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none disabled:opacity-50"
  286. >
  287. {PROVIDER_VALUES.map((value) => (
  288. <option key={value} value={value}>
  289. {t(`notifications.providerTypes.${value}`, value)}
  290. </option>
  291. ))}
  292. </select>
  293. <p className="text-xs text-bambu-gray mt-1">
  294. {t(`notifications.providerDescriptions.${providerType}`, '')}
  295. </p>
  296. </div>
  297. {/* Provider-specific configuration */}
  298. <div className="space-y-3">
  299. <p className="text-sm text-bambu-gray">{t('notifications.configuration')}</p>
  300. {configFields
  301. .filter((field) => !('showIf' in field) || (field as { showIf?: (cfg: Record<string, string>) => boolean }).showIf?.(config) !== false)
  302. .map((field) => (
  303. <div key={field.key}>
  304. <label className="block text-sm text-bambu-gray mb-1">
  305. {field.label} {field.required && '*'}
  306. </label>
  307. {field.type === 'select' && 'options' in field && field.options ? (
  308. <select
  309. value={config[field.key] || field.options[0]?.value || ''}
  310. onChange={(e) => {
  311. setConfig({ ...config, [field.key]: e.target.value });
  312. setTestResult(null);
  313. }}
  314. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  315. >
  316. {field.options.map((opt) => (
  317. <option key={opt.value} value={opt.value}>
  318. {opt.label}
  319. </option>
  320. ))}
  321. </select>
  322. ) : (
  323. <input
  324. type={field.type}
  325. value={config[field.key] || ''}
  326. onChange={(e) => {
  327. setConfig({ ...config, [field.key]: e.target.value });
  328. setTestResult(null);
  329. }}
  330. placeholder={field.placeholder}
  331. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  332. />
  333. )}
  334. </div>
  335. ))}
  336. </div>
  337. {/* Test Button */}
  338. <div className="flex gap-2">
  339. <Button
  340. type="button"
  341. variant="secondary"
  342. onClick={() => {
  343. setTestResult(null);
  344. testMutation.mutate();
  345. }}
  346. disabled={testMutation.isPending || (getRequiredFields(providerType).length > 0 && !config[getRequiredFields(providerType)[0]?.key])}
  347. className="flex-1"
  348. >
  349. {testMutation.isPending ? (
  350. <Loader2 className="w-4 h-4 animate-spin" />
  351. ) : (
  352. <Send className="w-4 h-4" />
  353. )}
  354. {t('notifications.testConfiguration')}
  355. </Button>
  356. </div>
  357. {/* Test Result */}
  358. {testResult && (
  359. <div className={`p-3 rounded-lg flex items-center gap-2 ${
  360. testResult.success
  361. ? 'bg-bambu-green/20 border border-bambu-green/50 text-bambu-green'
  362. : 'bg-red-500/20 border border-red-500/50 text-red-400'
  363. }`}>
  364. {testResult.success ? (
  365. <>
  366. <CheckCircle className="w-5 h-5" />
  367. <span>{testResult.message}</span>
  368. </>
  369. ) : (
  370. <>
  371. <XCircle className="w-5 h-5" />
  372. <span>{testResult.message}</span>
  373. </>
  374. )}
  375. </div>
  376. )}
  377. {/* Link to Printer */}
  378. <div>
  379. <label className="block text-sm text-bambu-gray mb-1">{t('notifications.printerFilter')}</label>
  380. <select
  381. value={printerId ?? ''}
  382. onChange={(e) => setPrinterId(e.target.value ? Number(e.target.value) : null)}
  383. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  384. >
  385. <option value="">{t('notifications.allPrinters')}</option>
  386. {printers?.map((p) => (
  387. <option key={p.id} value={p.id}>
  388. {p.name}
  389. </option>
  390. ))}
  391. </select>
  392. <p className="text-xs text-bambu-gray mt-1">
  393. {t('notifications.onlyFromPrinter')}
  394. </p>
  395. </div>
  396. {/* Quiet Hours */}
  397. <div className="space-y-2">
  398. <div className="flex items-center justify-between">
  399. <label className="text-sm text-white">{t('notifications.quietHoursDnd')}</label>
  400. <Toggle
  401. checked={quietHoursEnabled}
  402. onChange={setQuietHoursEnabled}
  403. />
  404. </div>
  405. {quietHoursEnabled && (
  406. <div className="grid grid-cols-2 gap-3">
  407. <div>
  408. <label className="block text-xs text-bambu-gray mb-1">{t('notifications.quietStart')}</label>
  409. <input
  410. type="time"
  411. value={quietHoursStart}
  412. onChange={(e) => setQuietHoursStart(e.target.value)}
  413. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  414. />
  415. </div>
  416. <div>
  417. <label className="block text-xs text-bambu-gray mb-1">{t('notifications.quietEnd')}</label>
  418. <input
  419. type="time"
  420. value={quietHoursEnd}
  421. onChange={(e) => setQuietHoursEnd(e.target.value)}
  422. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  423. />
  424. </div>
  425. </div>
  426. )}
  427. </div>
  428. {/* Daily Digest */}
  429. <div className="space-y-2">
  430. <div className="flex items-center justify-between">
  431. <div>
  432. <label className="text-sm text-white">{t('notifications.dailyDigestLabel')}</label>
  433. <p className="text-xs text-bambu-gray">{t('notifications.batchNotifications')}</p>
  434. </div>
  435. <Toggle
  436. checked={dailyDigestEnabled}
  437. onChange={setDailyDigestEnabled}
  438. />
  439. </div>
  440. {dailyDigestEnabled && (
  441. <div>
  442. <label className="block text-xs text-bambu-gray mb-1">{t('notifications.sendDigestAt')}</label>
  443. <input
  444. type="time"
  445. value={dailyDigestTime}
  446. onChange={(e) => setDailyDigestTime(e.target.value)}
  447. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  448. />
  449. <p className="text-xs text-bambu-gray mt-1">
  450. {t('notifications.digestCollected')}
  451. </p>
  452. </div>
  453. )}
  454. </div>
  455. {/* Event Toggles */}
  456. <div className="space-y-3">
  457. <p className="text-sm text-bambu-gray">{t('notifications.notificationEvents')}</p>
  458. {/* Print Events */}
  459. <div className="space-y-2 p-3 bg-bambu-dark rounded-lg">
  460. <p className="text-xs text-bambu-gray uppercase tracking-wide mb-2">{t('notifications.printEvents')}</p>
  461. <div className="grid grid-cols-2 gap-2">
  462. <div className="flex items-center justify-between">
  463. <span className="text-sm text-white">{t('notifications.start')}</span>
  464. <Toggle checked={onPrintStart} onChange={setOnPrintStart} />
  465. </div>
  466. <div className="flex items-center justify-between">
  467. <span className="text-sm text-white">{t('notifications.complete')}</span>
  468. <Toggle checked={onPrintComplete} onChange={setOnPrintComplete} />
  469. </div>
  470. <div className="flex items-center justify-between">
  471. <span className="text-sm text-white">{t('notifications.failed')}</span>
  472. <Toggle checked={onPrintFailed} onChange={setOnPrintFailed} />
  473. </div>
  474. <div className="flex items-center justify-between">
  475. <span className="text-sm text-white">{t('notifications.stopped')}</span>
  476. <Toggle checked={onPrintStopped} onChange={setOnPrintStopped} />
  477. </div>
  478. <div className="flex items-center justify-between col-span-2">
  479. <div>
  480. <span className="text-sm text-white">{t('notifications.progress')}</span>
  481. <span className="text-xs text-bambu-gray ml-1">{t('notifications.progressPercent')}</span>
  482. </div>
  483. <Toggle checked={onPrintProgress} onChange={setOnPrintProgress} />
  484. </div>
  485. <div className="flex items-center justify-between col-span-2">
  486. <div>
  487. <span className="text-sm text-white">{t('notifications.bedCooled')}</span>
  488. <span className="text-xs text-bambu-gray ml-1">{t('notifications.bedCooledAfterPrint')}</span>
  489. </div>
  490. <Toggle checked={onBedCooled} onChange={setOnBedCooled} />
  491. </div>
  492. <div className="flex items-center justify-between col-span-2">
  493. <div>
  494. <span className="text-sm text-white">{t('notifications.firstLayerCompleteLabel')}</span>
  495. <span className="text-xs text-bambu-gray ml-1">{t('notifications.firstLayerCompleteDescription')}</span>
  496. </div>
  497. <Toggle checked={onFirstLayerComplete} onChange={setOnFirstLayerComplete} />
  498. </div>
  499. </div>
  500. </div>
  501. {/* Printer Status Events */}
  502. <div className="space-y-2 p-3 bg-bambu-dark rounded-lg">
  503. <p className="text-xs text-bambu-gray uppercase tracking-wide mb-2">{t('notifications.printerStatus')}</p>
  504. <div className="grid grid-cols-2 gap-2">
  505. <div className="flex items-center justify-between">
  506. <span className="text-sm text-white">{t('notifications.offline')}</span>
  507. <Toggle checked={onPrinterOffline} onChange={setOnPrinterOffline} />
  508. </div>
  509. <div className="flex items-center justify-between">
  510. <span className="text-sm text-white">{t('notifications.error')}</span>
  511. <Toggle checked={onPrinterError} onChange={setOnPrinterError} />
  512. </div>
  513. <div className="flex items-center justify-between">
  514. <span className="text-sm text-white">{t('notifications.lowFilament')}</span>
  515. <Toggle checked={onFilamentLow} onChange={setOnFilamentLow} />
  516. </div>
  517. <div className="flex items-center justify-between">
  518. <span className="text-sm text-white">{t('notifications.maintenance')}</span>
  519. <Toggle checked={onMaintenanceDue} onChange={setOnMaintenanceDue} />
  520. </div>
  521. </div>
  522. </div>
  523. {/* Inventory Stock Alerts */}
  524. <div className="space-y-2 p-3 bg-bambu-dark rounded-lg">
  525. <p className="text-xs text-bambu-gray uppercase tracking-wide mb-2">{t('notifications.inventoryAlerts')}</p>
  526. <div className="grid grid-cols-1 gap-2">
  527. <div className="flex items-center justify-between">
  528. <div>
  529. <span className="text-sm text-white">{t('notifications.stockReorderAlert')}</span>
  530. <span className="text-xs text-bambu-gray ml-1">{t('notifications.stockReorderAlertDescription')}</span>
  531. </div>
  532. <Toggle checked={onStockReorderAlert} onChange={setOnStockReorderAlert} />
  533. </div>
  534. <div className="flex items-center justify-between">
  535. <div>
  536. <span className="text-sm text-white">{t('notifications.stockBreakAlert')}</span>
  537. <span className="text-xs text-bambu-gray ml-1">{t('notifications.stockBreakAlertDescription')}</span>
  538. </div>
  539. <Toggle checked={onStockBreakAlert} onChange={setOnStockBreakAlert} />
  540. </div>
  541. </div>
  542. </div>
  543. {/* Per-event ntfy priority (#990) */}
  544. {providerType === 'ntfy' && (() => {
  545. const enabledEvents: Array<{ key: string; label: string }> = [];
  546. if (onPrintStart) enabledEvents.push({ key: 'on_print_start', label: t('notifications.start') });
  547. if (onPrintComplete) enabledEvents.push({ key: 'on_print_complete', label: t('notifications.complete') });
  548. if (onPrintFailed) enabledEvents.push({ key: 'on_print_failed', label: t('notifications.failed') });
  549. if (onPrintStopped) enabledEvents.push({ key: 'on_print_stopped', label: t('notifications.stopped') });
  550. if (onPrintProgress) enabledEvents.push({ key: 'on_print_progress', label: t('notifications.progress') });
  551. if (onBedCooled) enabledEvents.push({ key: 'on_bed_cooled', label: t('notifications.bedCooled') });
  552. if (onFirstLayerComplete) enabledEvents.push({ key: 'on_first_layer_complete', label: t('notifications.firstLayerCompleteLabel') });
  553. if (onPrinterOffline) enabledEvents.push({ key: 'on_printer_offline', label: t('notifications.offline') });
  554. if (onPrinterError) enabledEvents.push({ key: 'on_printer_error', label: t('notifications.error') });
  555. if (onFilamentLow) enabledEvents.push({ key: 'on_filament_low', label: t('notifications.lowFilament') });
  556. if (onMaintenanceDue) enabledEvents.push({ key: 'on_maintenance_due', label: t('notifications.maintenance') });
  557. if (onStockReorderAlert) enabledEvents.push({ key: 'on_stock_reorder_alert', label: t('notifications.stockReorderAlert') });
  558. if (onStockBreakAlert) enabledEvents.push({ key: 'on_stock_break_alert', label: t('notifications.stockBreakAlert') });
  559. if (enabledEvents.length === 0) return null;
  560. return (
  561. <div className="space-y-2 p-3 bg-bambu-dark rounded-lg">
  562. <p className="text-xs text-bambu-gray uppercase tracking-wide mb-1">
  563. {t('notifications.eventPriority.sectionTitle')}
  564. </p>
  565. <p className="text-xs text-bambu-gray mb-2">{t('notifications.eventPriority.helpNtfy')}</p>
  566. <div className="space-y-2">
  567. {enabledEvents.map((ev) => (
  568. <div key={ev.key} className="flex items-center justify-between gap-3">
  569. <span className="text-sm text-white">{ev.label}</span>
  570. <select
  571. value={eventPriorities[ev.key] ?? 3}
  572. onChange={(e) => {
  573. const next = Number(e.target.value);
  574. setEventPriorities((prev) => ({ ...prev, [ev.key]: next }));
  575. }}
  576. className="px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-sm text-white focus:border-bambu-green focus:outline-none"
  577. >
  578. <option value={1}>{t('notifications.eventPriority.min')}</option>
  579. <option value={2}>{t('notifications.eventPriority.low')}</option>
  580. <option value={3}>{t('notifications.eventPriority.default')}</option>
  581. <option value={4}>{t('notifications.eventPriority.high')}</option>
  582. <option value={5}>{t('notifications.eventPriority.urgent')}</option>
  583. </select>
  584. </div>
  585. ))}
  586. </div>
  587. </div>
  588. );
  589. })()}
  590. </div>
  591. {/* Actions */}
  592. <div className="flex gap-3 pt-2">
  593. <Button
  594. type="button"
  595. variant="secondary"
  596. onClick={onClose}
  597. className="flex-1"
  598. >
  599. {t('notifications.cancel')}
  600. </Button>
  601. <Button
  602. type="submit"
  603. disabled={isPending}
  604. className="flex-1"
  605. >
  606. {isPending ? (
  607. <Loader2 className="w-4 h-4 animate-spin" />
  608. ) : (
  609. <Save className="w-4 h-4" />
  610. )}
  611. {isEditing ? t('notifications.save') : t('notifications.add')}
  612. </Button>
  613. </div>
  614. </form>
  615. </div>
  616. </div>
  617. );
  618. }