NotificationProviderCard.tsx 41 KB

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