NotificationProviderCard.tsx 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. import { useState } from 'react';
  2. import { useMutation, useQueryClient, useQuery } from '@tanstack/react-query';
  3. import { useTranslation } from 'react-i18next';
  4. import { Bell, Trash2, Settings2, Edit2, Send, Loader2, CheckCircle, XCircle, Moon, Clock, ChevronDown, ChevronUp, Calendar, Camera, CameraOff } from 'lucide-react';
  5. import { api } from '../api/client';
  6. import { formatDateOnly, parseUTCDate } from '../utils/date';
  7. import type { NotificationProvider, NotificationProviderUpdate } from '../api/client';
  8. import { Card, CardContent } from './Card';
  9. import { Button } from './Button';
  10. import { ConfirmModal } from './ConfirmModal';
  11. import { Toggle } from './Toggle';
  12. interface NotificationProviderCardProps {
  13. provider: NotificationProvider;
  14. onEdit: (provider: NotificationProvider) => void;
  15. }
  16. export function NotificationProviderCard({ provider, onEdit }: NotificationProviderCardProps) {
  17. const { t } = useTranslation();
  18. const queryClient = useQueryClient();
  19. const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
  20. const [isExpanded, setIsExpanded] = useState(false);
  21. const [testResult, setTestResult] = useState<{ success: boolean; message: string } | null>(null);
  22. // Fetch printers for linking
  23. const { data: printers } = useQuery({
  24. queryKey: ['printers'],
  25. queryFn: api.getPrinters,
  26. });
  27. const linkedPrinter = printers?.find(p => p.id === provider.printer_id);
  28. // Update mutation
  29. const updateMutation = useMutation({
  30. mutationFn: (data: NotificationProviderUpdate) => api.updateNotificationProvider(provider.id, data),
  31. onSuccess: () => {
  32. queryClient.invalidateQueries({ queryKey: ['notification-providers'] });
  33. },
  34. });
  35. // Delete mutation
  36. const deleteMutation = useMutation({
  37. mutationFn: () => api.deleteNotificationProvider(provider.id),
  38. onSuccess: () => {
  39. queryClient.invalidateQueries({ queryKey: ['notification-providers'] });
  40. },
  41. });
  42. // Test mutation
  43. const testMutation = useMutation({
  44. mutationFn: () => api.testNotificationProvider(provider.id),
  45. onSuccess: (result) => {
  46. setTestResult(result);
  47. queryClient.invalidateQueries({ queryKey: ['notification-providers'] });
  48. },
  49. onError: (err: Error) => {
  50. setTestResult({ success: false, message: err.message });
  51. },
  52. });
  53. // Format time for display
  54. const formatTime = (time: string | null) => {
  55. if (!time) return '';
  56. return time;
  57. };
  58. return (
  59. <>
  60. <Card className="relative">
  61. <CardContent className="p-3">
  62. {/* Header Row */}
  63. <div className={`flex items-start justify-between ${provider.enabled ? 'mb-3' : 'mb-0'}`}>
  64. <div className="flex items-center gap-3">
  65. <div className={`p-2 rounded-lg ${provider.enabled ? 'bg-bambu-green/20' : 'bg-bambu-dark'}`}>
  66. <Bell className={`w-5 h-5 ${provider.enabled ? 'text-bambu-green' : 'text-bambu-gray'}`} />
  67. </div>
  68. <div>
  69. <h3 className="font-medium text-white">{provider.name}</h3>
  70. <p className="text-sm text-bambu-gray">{t(`notifications.providerTypes.${provider.provider_type}`, provider.provider_type)}</p>
  71. </div>
  72. </div>
  73. {/* Quick enable/disable toggle + Status indicator */}
  74. <div className="flex items-center gap-3">
  75. {provider.last_success && (
  76. <span className="text-xs text-status-ok hidden sm:inline">{t('notifications.lastSuccess', { date: formatDateOnly(provider.last_success) })}</span>
  77. )}
  78. {/* Only show error if it's more recent than last success */}
  79. {provider.last_error && provider.last_error_at && (
  80. !provider.last_success || (parseUTCDate(provider.last_error_at)?.getTime() || 0) > (parseUTCDate(provider.last_success)?.getTime() || 0)
  81. ) && (
  82. <span className="text-xs text-status-error" title={provider.last_error}>{t('notifications.error')}</span>
  83. )}
  84. <Toggle
  85. checked={provider.enabled}
  86. onChange={(checked) => updateMutation.mutate({ enabled: checked })}
  87. />
  88. </div>
  89. </div>
  90. {provider.enabled && (<>
  91. {/* Linked Printer */}
  92. {linkedPrinter && (
  93. <div className="mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg">
  94. <span className="text-xs text-bambu-gray">{t('notifications.printer')} </span>
  95. <span className="text-sm text-white">{linkedPrinter.name}</span>
  96. </div>
  97. )}
  98. {!linkedPrinter && !provider.printer_id && (
  99. <div className="mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg">
  100. <span className="text-xs text-bambu-gray">{t('notifications.allPrinters')}</span>
  101. </div>
  102. )}
  103. {/* Event summary - show all event tags */}
  104. <div className="mb-3 flex flex-wrap gap-1">
  105. {provider.on_print_start && (
  106. <span className="px-2 py-0.5 bg-blue-100 dark:bg-blue-500/20 text-blue-700 dark:text-blue-400 text-xs rounded">{t('notifications.start')}</span>
  107. )}
  108. {provider.on_plate_not_empty && (
  109. <span className="px-2 py-0.5 bg-rose-100 dark:bg-rose-600/20 text-rose-700 dark:text-rose-300 text-xs rounded">{t('notifications.plateCheck')}</span>
  110. )}
  111. {provider.on_print_complete && (
  112. <span className="px-2 py-0.5 bg-bambu-green/20 text-bambu-green text-xs rounded">{t('notifications.complete')}</span>
  113. )}
  114. {provider.on_plate_clear_required && (
  115. <span className="px-2 py-0.5 bg-amber-100 dark:bg-amber-500/20 text-amber-700 dark:text-amber-400 text-xs rounded">{t('notifications.plateClear')}</span>
  116. )}
  117. {provider.on_print_confirm_request && (
  118. <span className="px-2 py-0.5 bg-teal-100 dark:bg-teal-500/20 text-teal-700 dark:text-teal-400 text-xs rounded">{t('notifications.printConfirmRequest')}</span>
  119. )}
  120. {provider.provider_type === 'telegram' && provider.on_print_confirm_request && provider.telegram_verdict_mode === 'reactions' && (
  121. <span className="px-2 py-0.5 bg-sky-100 dark:bg-sky-500/20 text-sky-700 dark:text-sky-400 text-xs rounded">{t('notifications.telegramVerdictBadgeReactions')}</span>
  122. )}
  123. {provider.provider_type === 'telegram' && provider.on_print_confirm_request && provider.telegram_verdict_mode === 'both' && (
  124. <span className="px-2 py-0.5 bg-sky-100 dark:bg-sky-500/20 text-sky-700 dark:text-sky-400 text-xs rounded">{t('notifications.telegramVerdictBadgeBoth')}</span>
  125. )}
  126. {provider.on_print_failed && (
  127. <span className="px-2 py-0.5 bg-red-100 dark:bg-red-500/20 text-red-700 dark:text-red-400 text-xs rounded">{t('notifications.failed')}</span>
  128. )}
  129. {provider.on_print_stopped && (
  130. <span className="px-2 py-0.5 bg-orange-100 dark:bg-orange-500/20 text-orange-700 dark:text-orange-400 text-xs rounded">{t('notifications.stopped')}</span>
  131. )}
  132. {provider.on_billing_charge_failed && (
  133. <span className="px-2 py-0.5 bg-red-100 dark:bg-red-600/20 text-red-700 dark:text-red-300 text-xs rounded">{t('notifications.billingChargeFailedLabel')}</span>
  134. )}
  135. {provider.on_print_progress && (
  136. <span className="px-2 py-0.5 bg-yellow-100 dark:bg-yellow-500/20 text-yellow-700 dark:text-yellow-400 text-xs rounded">{t('notifications.progress')}</span>
  137. )}
  138. {provider.on_printer_offline && (
  139. <span className="px-2 py-0.5 bg-gray-500/20 text-gray-400 text-xs rounded">{t('notifications.offline')}</span>
  140. )}
  141. {provider.on_printer_error && (
  142. <span className="px-2 py-0.5 bg-rose-100 dark:bg-rose-500/20 text-rose-700 dark:text-rose-400 text-xs rounded">{t('notifications.error')}</span>
  143. )}
  144. {provider.on_ai_failure_detection && (
  145. <span className="px-2 py-0.5 bg-fuchsia-100 dark:bg-fuchsia-500/20 text-fuchsia-700 dark:text-fuchsia-300 text-xs rounded">{t('notifications.aiFailureDetection')}</span>
  146. )}
  147. {provider.on_ha_sensor_alert && (
  148. <span className="px-2 py-0.5 bg-indigo-100 dark:bg-indigo-500/20 text-indigo-700 dark:text-indigo-300 text-xs rounded">{t('notifications.haSensorAlert')}</span>
  149. )}
  150. {provider.on_location_ha_sensor_alert && (
  151. <span className="px-2 py-0.5 bg-indigo-100 dark:bg-indigo-500/20 text-indigo-700 dark:text-indigo-300 text-xs rounded">{t('notifications.locationHaSensorAlert')}</span>
  152. )}
  153. {provider.on_filament_low && (
  154. <span className="px-2 py-0.5 bg-cyan-100 dark:bg-cyan-500/20 text-cyan-700 dark:text-cyan-400 text-xs rounded">{t('notifications.lowFilament')}</span>
  155. )}
  156. {provider.on_maintenance_due && (
  157. <span className="px-2 py-0.5 bg-purple-100 dark:bg-purple-500/20 text-purple-700 dark:text-purple-400 text-xs rounded">{t('notifications.maintenance')}</span>
  158. )}
  159. {provider.on_ams_humidity_high && (
  160. <span className="px-2 py-0.5 bg-blue-100 dark:bg-blue-600/20 text-blue-700 dark:text-blue-300 text-xs rounded">{t('notifications.amsHumidity')}</span>
  161. )}
  162. {provider.on_ams_temperature_high && (
  163. <span className="px-2 py-0.5 bg-orange-100 dark:bg-orange-600/20 text-orange-700 dark:text-orange-300 text-xs rounded">{t('notifications.amsTemp')}</span>
  164. )}
  165. {provider.on_ams_drying_suspended && (
  166. <span className="px-2 py-0.5 bg-rose-100 dark:bg-rose-600/20 text-rose-700 dark:text-rose-300 text-xs rounded">{t('notifications.amsDryingSuspended')}</span>
  167. )}
  168. {provider.on_ams_ht_humidity_high && (
  169. <span className="px-2 py-0.5 bg-cyan-100 dark:bg-cyan-600/20 text-cyan-700 dark:text-cyan-300 text-xs rounded">{t('notifications.amsHtHumidity')}</span>
  170. )}
  171. {provider.on_ams_ht_temperature_high && (
  172. <span className="px-2 py-0.5 bg-amber-100 dark:bg-amber-600/20 text-amber-700 dark:text-amber-300 text-xs rounded">{t('notifications.amsHtTemp')}</span>
  173. )}
  174. {provider.on_bed_cooled && (
  175. <span className="px-2 py-0.5 bg-teal-100 dark:bg-teal-500/20 text-teal-700 dark:text-teal-400 text-xs rounded">{t('notifications.bedCooled')}</span>
  176. )}
  177. {provider.on_first_layer_complete && (
  178. <span className="px-2 py-0.5 bg-emerald-100 dark:bg-emerald-600/20 text-emerald-700 dark:text-emerald-300 text-xs rounded">{t('notifications.firstLayer')}</span>
  179. )}
  180. {provider.on_app_message && (
  181. <span className="px-2 py-0.5 bg-sky-100 dark:bg-sky-500/20 text-sky-700 dark:text-sky-400 text-xs rounded">{t('notifications.appMessagesBadge')}</span>
  182. )}
  183. {provider.on_print_missing_spool_assignment && (
  184. <span className="px-2 py-0.5 bg-amber-100 dark:bg-amber-500/20 text-amber-700 dark:text-amber-300 text-xs rounded">{t('notifications.missingSpoolAssignmentLabel')}</span>
  185. )}
  186. {provider.on_stock_reorder_alert && (
  187. <span className="px-2 py-0.5 bg-lime-100 dark:bg-lime-500/20 text-lime-700 dark:text-lime-400 text-xs rounded">{t('notifications.stockReorderAlert')}</span>
  188. )}
  189. {provider.on_stock_break_alert && (
  190. <span className="px-2 py-0.5 bg-red-100 dark:bg-red-600/20 text-red-700 dark:text-red-300 text-xs rounded">{t('notifications.stockBreakAlert')}</span>
  191. )}
  192. {provider.quiet_hours_enabled && (
  193. <span className="px-2 py-0.5 bg-indigo-100 dark:bg-indigo-500/20 text-indigo-700 dark:text-indigo-400 text-xs rounded flex items-center gap-1">
  194. <Moon className="w-3 h-3" />
  195. {t('notifications.quiet')}
  196. </span>
  197. )}
  198. {provider.daily_digest_enabled && (
  199. <span className="px-2 py-0.5 bg-emerald-100 dark:bg-emerald-500/20 text-emerald-700 dark:text-emerald-400 text-xs rounded flex items-center gap-1">
  200. <Calendar className="w-3 h-3" />
  201. {t('notifications.digest', { time: provider.daily_digest_time })}
  202. </span>
  203. )}
  204. {provider.attach_photo === false && (
  205. <span className="px-2 py-0.5 bg-bambu-dark-tertiary text-bambu-gray text-xs rounded flex items-center gap-1">
  206. <CameraOff className="w-3 h-3" />
  207. {t('notifications.noPhoto')}
  208. </span>
  209. )}
  210. </div>
  211. {/* Test Button */}
  212. <div className="mb-3">
  213. <Button
  214. size="sm"
  215. variant="secondary"
  216. disabled={testMutation.isPending}
  217. onClick={() => {
  218. setTestResult(null);
  219. testMutation.mutate();
  220. }}
  221. className="w-full"
  222. >
  223. {testMutation.isPending ? (
  224. <Loader2 className="w-4 h-4 animate-spin" />
  225. ) : (
  226. <Send className="w-4 h-4" />
  227. )}
  228. {t('notifications.sendTestNotification')}
  229. </Button>
  230. </div>
  231. {/* Test Result */}
  232. {testResult && (
  233. <div className={`mb-3 p-2 rounded-lg flex items-center gap-2 text-sm ${
  234. testResult.success
  235. ? 'bg-bambu-green/20 text-bambu-green'
  236. : 'bg-red-100 dark:bg-red-500/20 text-red-700 dark:text-red-400'
  237. }`}>
  238. {testResult.success ? (
  239. <CheckCircle className="w-4 h-4" />
  240. ) : (
  241. <XCircle className="w-4 h-4" />
  242. )}
  243. <span>{testResult.message}</span>
  244. </div>
  245. )}
  246. </>)}
  247. {/* Toggle Settings Panel */}
  248. <button
  249. onClick={() => setIsExpanded(!isExpanded)}
  250. className={`w-full flex items-center justify-between py-2 text-sm text-bambu-gray hover:text-white transition-colors ${provider.enabled ? 'border-t border-bambu-dark-tertiary' : 'mt-2 border-t border-bambu-dark-tertiary'}`}
  251. >
  252. <span className="flex items-center gap-2">
  253. <Settings2 className="w-4 h-4" />
  254. {t('notifications.eventSettings')}
  255. </span>
  256. {isExpanded ? (
  257. <ChevronUp className="w-4 h-4" />
  258. ) : (
  259. <ChevronDown className="w-4 h-4" />
  260. )}
  261. </button>
  262. {/* Expanded Settings */}
  263. {isExpanded && (
  264. <div className="pt-3 border-t border-bambu-dark-tertiary space-y-4">
  265. {/* Enabled Toggle */}
  266. <div className="flex items-center justify-between">
  267. <div>
  268. <p className="text-sm text-white">{t('notifications.enabled')}</p>
  269. <p className="text-xs text-bambu-gray">{t('notifications.sendFromProvider')}</p>
  270. </div>
  271. <Toggle
  272. checked={provider.enabled}
  273. onChange={(checked) => updateMutation.mutate({ enabled: checked })}
  274. />
  275. </div>
  276. {/* Print Lifecycle Events */}
  277. <div className="space-y-2">
  278. <p className="text-xs text-bambu-gray uppercase tracking-wide">{t('notifications.printEvents')}</p>
  279. <div className="flex items-center justify-between">
  280. <p className="text-sm text-white">{t('notifications.printStarted')}</p>
  281. <Toggle
  282. checked={provider.on_print_start}
  283. onChange={(checked) => updateMutation.mutate({ on_print_start: checked })}
  284. />
  285. </div>
  286. <div className="flex items-center justify-between">
  287. <div>
  288. <p className="text-sm text-white">{t('notifications.plateNotEmpty')}</p>
  289. <p className="text-xs text-bambu-gray">{t('notifications.plateNotEmptyDescription')}</p>
  290. </div>
  291. <Toggle
  292. checked={provider.on_plate_not_empty ?? true}
  293. onChange={(checked) => updateMutation.mutate({ on_plate_not_empty: checked })}
  294. />
  295. </div>
  296. <div className="flex items-center justify-between">
  297. <p className="text-sm text-white">{t('notifications.printCompleted')}</p>
  298. <Toggle
  299. checked={provider.on_print_complete}
  300. onChange={(checked) => updateMutation.mutate({ on_print_complete: checked })}
  301. />
  302. </div>
  303. <div className="flex items-center justify-between">
  304. <div>
  305. <p className="text-sm text-white">{t('notifications.plateClearRequired')}</p>
  306. <p className="text-xs text-bambu-gray">{t('notifications.plateClearRequiredDescription')}</p>
  307. </div>
  308. <Toggle
  309. checked={provider.on_plate_clear_required ?? false}
  310. onChange={(checked) => updateMutation.mutate({ on_plate_clear_required: checked })}
  311. />
  312. </div>
  313. <div className="flex items-center justify-between">
  314. <div>
  315. <p className="text-sm text-white">{t('notifications.printConfirmRequest')}</p>
  316. <p className="text-xs text-bambu-gray">{t('notifications.printConfirmRequestDescription')}</p>
  317. </div>
  318. <Toggle
  319. checked={provider.on_print_confirm_request ?? true}
  320. onChange={(checked) => updateMutation.mutate({ on_print_confirm_request: checked })}
  321. />
  322. </div>
  323. <div className="flex items-center justify-between">
  324. <div>
  325. <p className="text-sm text-white">{t('notifications.bedCooledLabel')}</p>
  326. <p className="text-xs text-bambu-gray">{t('notifications.bedCooledDescription')}</p>
  327. </div>
  328. <Toggle
  329. checked={provider.on_bed_cooled ?? false}
  330. onChange={(checked) => updateMutation.mutate({ on_bed_cooled: checked })}
  331. />
  332. </div>
  333. <div className="flex items-center justify-between">
  334. <div>
  335. <p className="text-sm text-white">{t('notifications.firstLayerCompleteLabel')}</p>
  336. <p className="text-xs text-bambu-gray">{t('notifications.firstLayerCompleteDescription')}</p>
  337. </div>
  338. <Toggle
  339. checked={provider.on_first_layer_complete ?? false}
  340. onChange={(checked) => updateMutation.mutate({ on_first_layer_complete: checked })}
  341. />
  342. </div>
  343. <div className="flex items-center justify-between">
  344. <div>
  345. <p className="text-sm text-white">{t('notifications.missingSpoolAssignmentLabel')}</p>
  346. <p className="text-xs text-bambu-gray">{t('notifications.missingSpoolAssignmentDescription')}</p>
  347. </div>
  348. <Toggle
  349. checked={provider.on_print_missing_spool_assignment ?? false}
  350. onChange={(checked) => updateMutation.mutate({ on_print_missing_spool_assignment: checked })}
  351. />
  352. </div>
  353. <div className="flex items-center justify-between">
  354. <p className="text-sm text-white">{t('notifications.printFailed')}</p>
  355. <Toggle
  356. checked={provider.on_print_failed}
  357. onChange={(checked) => updateMutation.mutate({ on_print_failed: checked })}
  358. />
  359. </div>
  360. <div className="flex items-center justify-between">
  361. <p className="text-sm text-white">{t('notifications.printStopped')}</p>
  362. <Toggle
  363. checked={provider.on_print_stopped}
  364. onChange={(checked) => updateMutation.mutate({ on_print_stopped: checked })}
  365. />
  366. </div>
  367. <div className="flex items-center justify-between">
  368. <div>
  369. <p className="text-sm text-white">{t('notifications.billingChargeFailedLabel')}</p>
  370. <p className="text-xs text-bambu-gray">{t('notifications.billingChargeFailedDescription')}</p>
  371. </div>
  372. <Toggle
  373. checked={provider.on_billing_charge_failed ?? true}
  374. onChange={(checked) => updateMutation.mutate({ on_billing_charge_failed: checked })}
  375. />
  376. </div>
  377. <div className="flex items-center justify-between">
  378. <div>
  379. <p className="text-sm text-white">{t('notifications.progressMilestones')}</p>
  380. <p className="text-xs text-bambu-gray">{t('notifications.progressMilestonesDescription')}</p>
  381. </div>
  382. <Toggle
  383. checked={provider.on_print_progress}
  384. onChange={(checked) => updateMutation.mutate({ on_print_progress: checked })}
  385. />
  386. </div>
  387. </div>
  388. {/* Printer Status Events */}
  389. <div className="space-y-2">
  390. <p className="text-xs text-bambu-gray uppercase tracking-wide">{t('notifications.printerStatus')}</p>
  391. <div className="flex items-center justify-between">
  392. <p className="text-sm text-white">{t('notifications.printerOffline')}</p>
  393. <Toggle
  394. checked={provider.on_printer_offline}
  395. onChange={(checked) => updateMutation.mutate({ on_printer_offline: checked })}
  396. />
  397. </div>
  398. <div className="flex items-center justify-between">
  399. <p className="text-sm text-white">{t('notifications.printerError')}</p>
  400. <Toggle
  401. checked={provider.on_printer_error}
  402. onChange={(checked) => updateMutation.mutate({ on_printer_error: checked })}
  403. />
  404. </div>
  405. <div className="flex items-center justify-between">
  406. <div>
  407. <p className="text-sm text-white">{t('notifications.aiFailureDetection')}</p>
  408. <p className="text-xs text-bambu-gray">{t('notifications.aiFailureDetectionDescription')}</p>
  409. </div>
  410. <Toggle
  411. checked={provider.on_ai_failure_detection ?? false}
  412. onChange={(checked) => updateMutation.mutate({ on_ai_failure_detection: checked })}
  413. />
  414. </div>
  415. <div className="flex items-center justify-between">
  416. <div>
  417. <p className="text-sm text-white">{t('notifications.haSensorAlert')}</p>
  418. <p className="text-xs text-bambu-gray">{t('notifications.haSensorAlertDescription')}</p>
  419. </div>
  420. <Toggle
  421. checked={provider.on_ha_sensor_alert ?? false}
  422. onChange={(checked) => updateMutation.mutate({ on_ha_sensor_alert: checked })}
  423. />
  424. </div>
  425. <div className="flex items-center justify-between">
  426. <div>
  427. <p className="text-sm text-white">{t('notifications.locationHaSensorAlert')}</p>
  428. <p className="text-xs text-bambu-gray">{t('notifications.locationHaSensorAlertDescription')}</p>
  429. </div>
  430. <Toggle
  431. checked={provider.on_location_ha_sensor_alert ?? false}
  432. onChange={(checked) => updateMutation.mutate({ on_location_ha_sensor_alert: checked })}
  433. />
  434. </div>
  435. <div className="flex items-center justify-between">
  436. <p className="text-sm text-white">{t('notifications.lowFilamentLabel')}</p>
  437. <Toggle
  438. checked={provider.on_filament_low}
  439. onChange={(checked) => updateMutation.mutate({ on_filament_low: checked })}
  440. />
  441. </div>
  442. <div className="flex items-center justify-between">
  443. <div>
  444. <p className="text-sm text-white">{t('notifications.maintenanceDue')}</p>
  445. <p className="text-xs text-bambu-gray">{t('notifications.maintenanceDueDescription')}</p>
  446. </div>
  447. <Toggle
  448. checked={provider.on_maintenance_due ?? false}
  449. onChange={(checked) => updateMutation.mutate({ on_maintenance_due: checked })}
  450. />
  451. </div>
  452. </div>
  453. {/* AMS Environmental Alarms (regular AMS) */}
  454. <div className="space-y-2">
  455. <p className="text-xs text-bambu-gray uppercase tracking-wide">{t('notifications.amsAlarms')}</p>
  456. <div className="flex items-center justify-between">
  457. <div>
  458. <p className="text-sm text-white">{t('notifications.amsHumidityHigh')}</p>
  459. <p className="text-xs text-bambu-gray">{t('notifications.amsHumidityHighDescription')}</p>
  460. </div>
  461. <Toggle
  462. checked={provider.on_ams_humidity_high ?? false}
  463. onChange={(checked) => updateMutation.mutate({ on_ams_humidity_high: checked })}
  464. />
  465. </div>
  466. <div className="flex items-center justify-between">
  467. <div>
  468. <p className="text-sm text-white">{t('notifications.amsTemperatureHigh')}</p>
  469. <p className="text-xs text-bambu-gray">{t('notifications.amsTemperatureHighDescription')}</p>
  470. </div>
  471. <Toggle
  472. checked={provider.on_ams_temperature_high ?? false}
  473. onChange={(checked) => updateMutation.mutate({ on_ams_temperature_high: checked })}
  474. />
  475. </div>
  476. <div className="flex items-center justify-between">
  477. <div>
  478. <p className="text-sm text-white">{t('notifications.amsDryingSuspendedTitle')}</p>
  479. <p className="text-xs text-bambu-gray">{t('notifications.amsDryingSuspendedDescription')}</p>
  480. </div>
  481. <Toggle
  482. checked={provider.on_ams_drying_suspended ?? false}
  483. onChange={(checked) => updateMutation.mutate({ on_ams_drying_suspended: checked })}
  484. />
  485. </div>
  486. </div>
  487. {/* AMS-HT Environmental Alarms */}
  488. <div className="space-y-2">
  489. <p className="text-xs text-bambu-gray uppercase tracking-wide">{t('notifications.amsHtAlarms')}</p>
  490. <div className="flex items-center justify-between">
  491. <div>
  492. <p className="text-sm text-white">{t('notifications.amsHtHumidityHigh')}</p>
  493. <p className="text-xs text-bambu-gray">{t('notifications.amsHtHumidityHighDescription')}</p>
  494. </div>
  495. <Toggle
  496. checked={provider.on_ams_ht_humidity_high ?? false}
  497. onChange={(checked) => updateMutation.mutate({ on_ams_ht_humidity_high: checked })}
  498. />
  499. </div>
  500. <div className="flex items-center justify-between">
  501. <div>
  502. <p className="text-sm text-white">{t('notifications.amsHtTemperatureHigh')}</p>
  503. <p className="text-xs text-bambu-gray">{t('notifications.amsHtTemperatureHighDescription')}</p>
  504. </div>
  505. <Toggle
  506. checked={provider.on_ams_ht_temperature_high ?? false}
  507. onChange={(checked) => updateMutation.mutate({ on_ams_ht_temperature_high: checked })}
  508. />
  509. </div>
  510. </div>
  511. {/* Inventory Stock Alerts */}
  512. <div className="space-y-2">
  513. <p className="text-xs text-bambu-gray uppercase tracking-wide">{t('notifications.inventoryAlerts')}</p>
  514. <div className="flex items-center justify-between">
  515. <div>
  516. <p className="text-sm text-white">{t('notifications.stockReorderAlert')}</p>
  517. <p className="text-xs text-bambu-gray">{t('notifications.stockReorderAlertDescription')}</p>
  518. </div>
  519. <Toggle
  520. checked={provider.on_stock_reorder_alert ?? false}
  521. onChange={(checked) => updateMutation.mutate({ on_stock_reorder_alert: checked })}
  522. />
  523. </div>
  524. <div className="flex items-center justify-between">
  525. <div>
  526. <p className="text-sm text-white">{t('notifications.stockBreakAlert')}</p>
  527. <p className="text-xs text-bambu-gray">{t('notifications.stockBreakAlertDescription')}</p>
  528. </div>
  529. <Toggle
  530. checked={provider.on_stock_break_alert ?? false}
  531. onChange={(checked) => updateMutation.mutate({ on_stock_break_alert: checked })}
  532. />
  533. </div>
  534. </div>
  535. {/* Print Queue Events */}
  536. <div className="space-y-2">
  537. <p className="text-xs text-bambu-gray uppercase tracking-wide">{t('notifications.printQueue')}</p>
  538. <div className="flex items-center justify-between">
  539. <div>
  540. <p className="text-sm text-white">{t('notifications.jobAdded')}</p>
  541. <p className="text-xs text-bambu-gray">{t('notifications.jobAddedDescription')}</p>
  542. </div>
  543. <Toggle
  544. checked={provider.on_queue_job_added ?? false}
  545. onChange={(checked) => updateMutation.mutate({ on_queue_job_added: checked })}
  546. />
  547. </div>
  548. <div className="flex items-center justify-between">
  549. <div>
  550. <p className="text-sm text-white">{t('notifications.jobAssigned')}</p>
  551. <p className="text-xs text-bambu-gray">{t('notifications.jobAssignedDescription')}</p>
  552. </div>
  553. <Toggle
  554. checked={provider.on_queue_job_assigned ?? false}
  555. onChange={(checked) => updateMutation.mutate({ on_queue_job_assigned: checked })}
  556. />
  557. </div>
  558. <div className="flex items-center justify-between">
  559. <div>
  560. <p className="text-sm text-white">{t('notifications.jobStarted')}</p>
  561. <p className="text-xs text-bambu-gray">{t('notifications.jobStartedDescription')}</p>
  562. </div>
  563. <Toggle
  564. checked={provider.on_queue_job_started ?? false}
  565. onChange={(checked) => updateMutation.mutate({ on_queue_job_started: checked })}
  566. />
  567. </div>
  568. <div className="flex items-center justify-between">
  569. <div>
  570. <p className="text-sm text-white">{t('notifications.jobWaiting')}</p>
  571. <p className="text-xs text-bambu-gray">{t('notifications.jobWaitingDescription')}</p>
  572. </div>
  573. <Toggle
  574. checked={provider.on_queue_job_waiting ?? true}
  575. onChange={(checked) => updateMutation.mutate({ on_queue_job_waiting: checked })}
  576. />
  577. </div>
  578. <div className="flex items-center justify-between">
  579. <div>
  580. <p className="text-sm text-white">{t('notifications.jobSkipped')}</p>
  581. <p className="text-xs text-bambu-gray">{t('notifications.jobSkippedDescription')}</p>
  582. </div>
  583. <Toggle
  584. checked={provider.on_queue_job_skipped ?? true}
  585. onChange={(checked) => updateMutation.mutate({ on_queue_job_skipped: checked })}
  586. />
  587. </div>
  588. <div className="flex items-center justify-between">
  589. <div>
  590. <p className="text-sm text-white">{t('notifications.jobFailed')}</p>
  591. <p className="text-xs text-bambu-gray">{t('notifications.jobFailedDescription')}</p>
  592. </div>
  593. <Toggle
  594. checked={provider.on_queue_job_failed ?? true}
  595. onChange={(checked) => updateMutation.mutate({ on_queue_job_failed: checked })}
  596. />
  597. </div>
  598. <div className="flex items-center justify-between">
  599. <div>
  600. <p className="text-sm text-white">{t('notifications.queueComplete')}</p>
  601. <p className="text-xs text-bambu-gray">{t('notifications.queueCompleteDescription')}</p>
  602. </div>
  603. <Toggle
  604. checked={provider.on_queue_completed ?? false}
  605. onChange={(checked) => updateMutation.mutate({ on_queue_completed: checked })}
  606. />
  607. </div>
  608. <div className="flex items-center justify-between">
  609. <div>
  610. <p className="text-sm text-white">{t('notifications.appMessages')}</p>
  611. <p className="text-xs text-bambu-gray">{t('notifications.appMessagesDescription')}</p>
  612. </div>
  613. <Toggle
  614. checked={provider.on_app_message ?? false}
  615. onChange={(checked) => updateMutation.mutate({ on_app_message: checked })}
  616. />
  617. </div>
  618. </div>
  619. {/* Quiet Hours */}
  620. <div className="space-y-2">
  621. <div className="flex items-center justify-between">
  622. <div className="flex items-center gap-2">
  623. <Moon className="w-4 h-4 text-purple-600 dark:text-purple-400" />
  624. <p className="text-sm text-white">{t('notifications.quietHours')}</p>
  625. </div>
  626. <Toggle
  627. checked={provider.quiet_hours_enabled}
  628. onChange={(checked) => updateMutation.mutate({ quiet_hours_enabled: checked })}
  629. />
  630. </div>
  631. {provider.quiet_hours_enabled && (
  632. <div className="pl-4 border-l-2 border-bambu-dark-tertiary space-y-2">
  633. <p className="text-xs text-bambu-gray">{t('notifications.noNotificationsDuring')}</p>
  634. <div className="flex items-center gap-2">
  635. <Clock className="w-4 h-4 text-bambu-gray" />
  636. <span className="text-sm text-white">
  637. {formatTime(provider.quiet_hours_start) || '22:00'} - {formatTime(provider.quiet_hours_end) || '07:00'}
  638. </span>
  639. </div>
  640. <p className="text-xs text-bambu-gray">{t('notifications.editProviderToChangeQuietHours')}</p>
  641. </div>
  642. )}
  643. </div>
  644. {/* Daily Digest */}
  645. <div className="space-y-2">
  646. <div className="flex items-center justify-between">
  647. <div className="flex items-center gap-2">
  648. <Calendar className="w-4 h-4 text-emerald-600 dark:text-emerald-400" />
  649. <p className="text-sm text-white">{t('notifications.dailyDigest')}</p>
  650. </div>
  651. <Toggle
  652. checked={provider.daily_digest_enabled}
  653. onChange={(checked) => updateMutation.mutate({ daily_digest_enabled: checked })}
  654. />
  655. </div>
  656. {provider.daily_digest_enabled && (
  657. <div className="pl-4 border-l-2 border-bambu-dark-tertiary space-y-2">
  658. <p className="text-xs text-bambu-gray">{t('notifications.batchNotifications')}</p>
  659. <div className="flex items-center gap-2">
  660. <Clock className="w-4 h-4 text-bambu-gray" />
  661. <span className="text-sm text-white">
  662. {t('notifications.sendAt', { time: formatTime(provider.daily_digest_time) || '08:00' })}
  663. </span>
  664. </div>
  665. <p className="text-xs text-bambu-gray">{t('notifications.editProviderToChangeDigestTime')}</p>
  666. </div>
  667. )}
  668. </div>
  669. {/* Attach Photo */}
  670. <div className="flex items-center justify-between">
  671. <div className="flex items-center gap-2">
  672. <Camera className="w-4 h-4 text-bambu-gray" />
  673. <div>
  674. <p className="text-sm text-white">{t('notifications.attachPhotoLabel')}</p>
  675. <p className="text-xs text-bambu-gray">{t('notifications.attachPhotoDescription')}</p>
  676. </div>
  677. </div>
  678. <Toggle
  679. checked={provider.attach_photo ?? true}
  680. onChange={(checked) => updateMutation.mutate({ attach_photo: checked })}
  681. />
  682. </div>
  683. {/* Action Buttons */}
  684. <div className="flex gap-2 pt-2">
  685. <Button
  686. size="sm"
  687. variant="secondary"
  688. onClick={() => onEdit(provider)}
  689. className="flex-1"
  690. >
  691. <Edit2 className="w-4 h-4" />
  692. {t('notifications.edit')}
  693. </Button>
  694. <Button
  695. size="sm"
  696. variant="secondary"
  697. onClick={() => setShowDeleteConfirm(true)}
  698. className="text-red-700 dark:text-red-400 hover:text-red-800 dark:hover:text-red-300"
  699. >
  700. <Trash2 className="w-4 h-4" />
  701. </Button>
  702. </div>
  703. </div>
  704. )}
  705. </CardContent>
  706. </Card>
  707. {/* Delete Confirmation */}
  708. {showDeleteConfirm && (
  709. <ConfirmModal
  710. title={t('notifications.deleteProvider')}
  711. message={t('notifications.deleteConfirm', { name: provider.name })}
  712. confirmText={t('notifications.delete')}
  713. variant="danger"
  714. onConfirm={() => {
  715. deleteMutation.mutate();
  716. setShowDeleteConfirm(false);
  717. }}
  718. onCancel={() => setShowDeleteConfirm(false)}
  719. />
  720. )}
  721. </>
  722. );
  723. }