NotificationProviderCard.tsx 41 KB

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