NotificationProviderCard.tsx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. import { useState } from 'react';
  2. import { useMutation, useQueryClient, useQuery } from '@tanstack/react-query';
  3. import { Bell, Trash2, Settings2, Edit2, Send, Loader2, CheckCircle, XCircle, Moon, Clock, ChevronDown, ChevronUp, Calendar } from 'lucide-react';
  4. import { api } from '../api/client';
  5. import type { NotificationProvider, NotificationProviderUpdate } from '../api/client';
  6. import { Card, CardContent } from './Card';
  7. import { Button } from './Button';
  8. import { ConfirmModal } from './ConfirmModal';
  9. import { Toggle } from './Toggle';
  10. interface NotificationProviderCardProps {
  11. provider: NotificationProvider;
  12. onEdit: (provider: NotificationProvider) => void;
  13. }
  14. const PROVIDER_LABELS: Record<string, string> = {
  15. callmebot: 'CallMeBot/WhatsApp',
  16. ntfy: 'ntfy',
  17. pushover: 'Pushover',
  18. telegram: 'Telegram',
  19. email: 'Email',
  20. discord: 'Discord',
  21. webhook: 'Webhook',
  22. };
  23. export function NotificationProviderCard({ provider, onEdit }: NotificationProviderCardProps) {
  24. const queryClient = useQueryClient();
  25. const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
  26. const [isExpanded, setIsExpanded] = useState(false);
  27. const [testResult, setTestResult] = useState<{ success: boolean; message: string } | null>(null);
  28. // Fetch printers for linking
  29. const { data: printers } = useQuery({
  30. queryKey: ['printers'],
  31. queryFn: api.getPrinters,
  32. });
  33. const linkedPrinter = printers?.find(p => p.id === provider.printer_id);
  34. // Update mutation
  35. const updateMutation = useMutation({
  36. mutationFn: (data: NotificationProviderUpdate) => api.updateNotificationProvider(provider.id, data),
  37. onSuccess: () => {
  38. queryClient.invalidateQueries({ queryKey: ['notification-providers'] });
  39. },
  40. });
  41. // Delete mutation
  42. const deleteMutation = useMutation({
  43. mutationFn: () => api.deleteNotificationProvider(provider.id),
  44. onSuccess: () => {
  45. queryClient.invalidateQueries({ queryKey: ['notification-providers'] });
  46. },
  47. });
  48. // Test mutation
  49. const testMutation = useMutation({
  50. mutationFn: () => api.testNotificationProvider(provider.id),
  51. onSuccess: (result) => {
  52. setTestResult(result);
  53. queryClient.invalidateQueries({ queryKey: ['notification-providers'] });
  54. },
  55. onError: (err: Error) => {
  56. setTestResult({ success: false, message: err.message });
  57. },
  58. });
  59. // Format time for display
  60. const formatTime = (time: string | null) => {
  61. if (!time) return '';
  62. return time;
  63. };
  64. return (
  65. <>
  66. <Card className="relative">
  67. <CardContent className="p-4">
  68. {/* Header Row */}
  69. <div className="flex items-start justify-between mb-3">
  70. <div className="flex items-center gap-3">
  71. <div className={`p-2 rounded-lg ${provider.enabled ? 'bg-bambu-green/20' : 'bg-bambu-dark'}`}>
  72. <Bell className={`w-5 h-5 ${provider.enabled ? 'text-bambu-green' : 'text-bambu-gray'}`} />
  73. </div>
  74. <div>
  75. <h3 className="font-medium text-white">{provider.name}</h3>
  76. <p className="text-sm text-bambu-gray">{PROVIDER_LABELS[provider.provider_type] || provider.provider_type}</p>
  77. </div>
  78. </div>
  79. {/* Quick enable/disable toggle + Status indicator */}
  80. <div className="flex items-center gap-3">
  81. {provider.last_success && (
  82. <span className="text-xs text-bambu-green hidden sm:inline">Last: {new Date(provider.last_success).toLocaleDateString()}</span>
  83. )}
  84. {/* Only show error if it's more recent than last success */}
  85. {provider.last_error && provider.last_error_at && (
  86. !provider.last_success || new Date(provider.last_error_at) > new Date(provider.last_success)
  87. ) && (
  88. <span className="text-xs text-red-400" title={provider.last_error}>Error</span>
  89. )}
  90. <Toggle
  91. checked={provider.enabled}
  92. onChange={(checked) => updateMutation.mutate({ enabled: checked })}
  93. />
  94. </div>
  95. </div>
  96. {/* Linked Printer */}
  97. {linkedPrinter && (
  98. <div className="mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg">
  99. <span className="text-xs text-bambu-gray">Printer: </span>
  100. <span className="text-sm text-white">{linkedPrinter.name}</span>
  101. </div>
  102. )}
  103. {!linkedPrinter && !provider.printer_id && (
  104. <div className="mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg">
  105. <span className="text-xs text-bambu-gray">All printers</span>
  106. </div>
  107. )}
  108. {/* Event summary - show all event tags */}
  109. <div className="mb-3 flex flex-wrap gap-1">
  110. {provider.on_print_start && (
  111. <span className="px-2 py-0.5 bg-blue-500/20 text-blue-400 text-xs rounded">Start</span>
  112. )}
  113. {provider.on_print_complete && (
  114. <span className="px-2 py-0.5 bg-bambu-green/20 text-bambu-green text-xs rounded">Complete</span>
  115. )}
  116. {provider.on_print_failed && (
  117. <span className="px-2 py-0.5 bg-red-500/20 text-red-400 text-xs rounded">Failed</span>
  118. )}
  119. {provider.on_print_stopped && (
  120. <span className="px-2 py-0.5 bg-orange-500/20 text-orange-400 text-xs rounded">Stopped</span>
  121. )}
  122. {provider.on_print_progress && (
  123. <span className="px-2 py-0.5 bg-yellow-500/20 text-yellow-400 text-xs rounded">Progress</span>
  124. )}
  125. {provider.on_printer_offline && (
  126. <span className="px-2 py-0.5 bg-gray-500/20 text-gray-400 text-xs rounded">Offline</span>
  127. )}
  128. {provider.on_printer_error && (
  129. <span className="px-2 py-0.5 bg-rose-500/20 text-rose-400 text-xs rounded">Error</span>
  130. )}
  131. {provider.on_filament_low && (
  132. <span className="px-2 py-0.5 bg-cyan-500/20 text-cyan-400 text-xs rounded">Low Filament</span>
  133. )}
  134. {provider.on_maintenance_due && (
  135. <span className="px-2 py-0.5 bg-purple-500/20 text-purple-400 text-xs rounded">Maintenance</span>
  136. )}
  137. {provider.on_ams_humidity_high && (
  138. <span className="px-2 py-0.5 bg-blue-600/20 text-blue-300 text-xs rounded">AMS Humidity</span>
  139. )}
  140. {provider.on_ams_temperature_high && (
  141. <span className="px-2 py-0.5 bg-orange-600/20 text-orange-300 text-xs rounded">AMS Temp</span>
  142. )}
  143. {provider.on_ams_ht_humidity_high && (
  144. <span className="px-2 py-0.5 bg-cyan-600/20 text-cyan-300 text-xs rounded">AMS-HT Humidity</span>
  145. )}
  146. {provider.on_ams_ht_temperature_high && (
  147. <span className="px-2 py-0.5 bg-amber-600/20 text-amber-300 text-xs rounded">AMS-HT Temp</span>
  148. )}
  149. {provider.quiet_hours_enabled && (
  150. <span className="px-2 py-0.5 bg-indigo-500/20 text-indigo-400 text-xs rounded flex items-center gap-1">
  151. <Moon className="w-3 h-3" />
  152. Quiet
  153. </span>
  154. )}
  155. {provider.daily_digest_enabled && (
  156. <span className="px-2 py-0.5 bg-emerald-500/20 text-emerald-400 text-xs rounded flex items-center gap-1">
  157. <Calendar className="w-3 h-3" />
  158. Digest {provider.daily_digest_time}
  159. </span>
  160. )}
  161. </div>
  162. {/* Test Button */}
  163. <div className="mb-3">
  164. <Button
  165. size="sm"
  166. variant="secondary"
  167. disabled={testMutation.isPending}
  168. onClick={() => {
  169. setTestResult(null);
  170. testMutation.mutate();
  171. }}
  172. className="w-full"
  173. >
  174. {testMutation.isPending ? (
  175. <Loader2 className="w-4 h-4 animate-spin" />
  176. ) : (
  177. <Send className="w-4 h-4" />
  178. )}
  179. Send Test Notification
  180. </Button>
  181. </div>
  182. {/* Test Result */}
  183. {testResult && (
  184. <div className={`mb-3 p-2 rounded-lg flex items-center gap-2 text-sm ${
  185. testResult.success
  186. ? 'bg-bambu-green/20 text-bambu-green'
  187. : 'bg-red-500/20 text-red-400'
  188. }`}>
  189. {testResult.success ? (
  190. <CheckCircle className="w-4 h-4" />
  191. ) : (
  192. <XCircle className="w-4 h-4" />
  193. )}
  194. <span>{testResult.message}</span>
  195. </div>
  196. )}
  197. {/* Toggle Settings Panel */}
  198. <button
  199. onClick={() => setIsExpanded(!isExpanded)}
  200. className="w-full flex items-center justify-between py-2 text-sm text-bambu-gray hover:text-white transition-colors border-t border-bambu-dark-tertiary"
  201. >
  202. <span className="flex items-center gap-2">
  203. <Settings2 className="w-4 h-4" />
  204. Event Settings
  205. </span>
  206. {isExpanded ? (
  207. <ChevronUp className="w-4 h-4" />
  208. ) : (
  209. <ChevronDown className="w-4 h-4" />
  210. )}
  211. </button>
  212. {/* Expanded Settings */}
  213. {isExpanded && (
  214. <div className="pt-3 border-t border-bambu-dark-tertiary space-y-4">
  215. {/* Enabled Toggle */}
  216. <div className="flex items-center justify-between">
  217. <div>
  218. <p className="text-sm text-white">Enabled</p>
  219. <p className="text-xs text-bambu-gray">Send notifications from this provider</p>
  220. </div>
  221. <Toggle
  222. checked={provider.enabled}
  223. onChange={(checked) => updateMutation.mutate({ enabled: checked })}
  224. />
  225. </div>
  226. {/* Print Lifecycle Events */}
  227. <div className="space-y-2">
  228. <p className="text-xs text-bambu-gray uppercase tracking-wide">Print Events</p>
  229. <div className="flex items-center justify-between">
  230. <p className="text-sm text-white">Print Started</p>
  231. <Toggle
  232. checked={provider.on_print_start}
  233. onChange={(checked) => updateMutation.mutate({ on_print_start: checked })}
  234. />
  235. </div>
  236. <div className="flex items-center justify-between">
  237. <p className="text-sm text-white">Print Completed</p>
  238. <Toggle
  239. checked={provider.on_print_complete}
  240. onChange={(checked) => updateMutation.mutate({ on_print_complete: checked })}
  241. />
  242. </div>
  243. <div className="flex items-center justify-between">
  244. <p className="text-sm text-white">Print Failed</p>
  245. <Toggle
  246. checked={provider.on_print_failed}
  247. onChange={(checked) => updateMutation.mutate({ on_print_failed: checked })}
  248. />
  249. </div>
  250. <div className="flex items-center justify-between">
  251. <p className="text-sm text-white">Print Stopped</p>
  252. <Toggle
  253. checked={provider.on_print_stopped}
  254. onChange={(checked) => updateMutation.mutate({ on_print_stopped: checked })}
  255. />
  256. </div>
  257. <div className="flex items-center justify-between">
  258. <div>
  259. <p className="text-sm text-white">Progress Milestones</p>
  260. <p className="text-xs text-bambu-gray">Notify at 25%, 50%, 75%</p>
  261. </div>
  262. <Toggle
  263. checked={provider.on_print_progress}
  264. onChange={(checked) => updateMutation.mutate({ on_print_progress: checked })}
  265. />
  266. </div>
  267. </div>
  268. {/* Printer Status Events */}
  269. <div className="space-y-2">
  270. <p className="text-xs text-bambu-gray uppercase tracking-wide">Printer Status</p>
  271. <div className="flex items-center justify-between">
  272. <p className="text-sm text-white">Printer Offline</p>
  273. <Toggle
  274. checked={provider.on_printer_offline}
  275. onChange={(checked) => updateMutation.mutate({ on_printer_offline: checked })}
  276. />
  277. </div>
  278. <div className="flex items-center justify-between">
  279. <p className="text-sm text-white">Printer Error</p>
  280. <Toggle
  281. checked={provider.on_printer_error}
  282. onChange={(checked) => updateMutation.mutate({ on_printer_error: checked })}
  283. />
  284. </div>
  285. <div className="flex items-center justify-between">
  286. <p className="text-sm text-white">Low Filament</p>
  287. <Toggle
  288. checked={provider.on_filament_low}
  289. onChange={(checked) => updateMutation.mutate({ on_filament_low: checked })}
  290. />
  291. </div>
  292. <div className="flex items-center justify-between">
  293. <div>
  294. <p className="text-sm text-white">Maintenance Due</p>
  295. <p className="text-xs text-bambu-gray">Notify when maintenance is needed</p>
  296. </div>
  297. <Toggle
  298. checked={provider.on_maintenance_due ?? false}
  299. onChange={(checked) => updateMutation.mutate({ on_maintenance_due: checked })}
  300. />
  301. </div>
  302. </div>
  303. {/* AMS Environmental Alarms (regular AMS) */}
  304. <div className="space-y-2">
  305. <p className="text-xs text-bambu-gray uppercase tracking-wide">AMS Alarms</p>
  306. <div className="flex items-center justify-between">
  307. <div>
  308. <p className="text-sm text-white">AMS Humidity High</p>
  309. <p className="text-xs text-bambu-gray">Regular AMS humidity exceeds threshold</p>
  310. </div>
  311. <Toggle
  312. checked={provider.on_ams_humidity_high ?? false}
  313. onChange={(checked) => updateMutation.mutate({ on_ams_humidity_high: checked })}
  314. />
  315. </div>
  316. <div className="flex items-center justify-between">
  317. <div>
  318. <p className="text-sm text-white">AMS Temperature High</p>
  319. <p className="text-xs text-bambu-gray">Regular AMS temperature exceeds threshold</p>
  320. </div>
  321. <Toggle
  322. checked={provider.on_ams_temperature_high ?? false}
  323. onChange={(checked) => updateMutation.mutate({ on_ams_temperature_high: checked })}
  324. />
  325. </div>
  326. </div>
  327. {/* AMS-HT Environmental Alarms */}
  328. <div className="space-y-2">
  329. <p className="text-xs text-bambu-gray uppercase tracking-wide">AMS-HT Alarms</p>
  330. <div className="flex items-center justify-between">
  331. <div>
  332. <p className="text-sm text-white">AMS-HT Humidity High</p>
  333. <p className="text-xs text-bambu-gray">AMS-HT humidity exceeds threshold</p>
  334. </div>
  335. <Toggle
  336. checked={provider.on_ams_ht_humidity_high ?? false}
  337. onChange={(checked) => updateMutation.mutate({ on_ams_ht_humidity_high: checked })}
  338. />
  339. </div>
  340. <div className="flex items-center justify-between">
  341. <div>
  342. <p className="text-sm text-white">AMS-HT Temperature High</p>
  343. <p className="text-xs text-bambu-gray">AMS-HT temperature exceeds threshold</p>
  344. </div>
  345. <Toggle
  346. checked={provider.on_ams_ht_temperature_high ?? false}
  347. onChange={(checked) => updateMutation.mutate({ on_ams_ht_temperature_high: checked })}
  348. />
  349. </div>
  350. </div>
  351. {/* Quiet Hours */}
  352. <div className="space-y-2">
  353. <div className="flex items-center justify-between">
  354. <div className="flex items-center gap-2">
  355. <Moon className="w-4 h-4 text-purple-400" />
  356. <p className="text-sm text-white">Quiet Hours</p>
  357. </div>
  358. <Toggle
  359. checked={provider.quiet_hours_enabled}
  360. onChange={(checked) => updateMutation.mutate({ quiet_hours_enabled: checked })}
  361. />
  362. </div>
  363. {provider.quiet_hours_enabled && (
  364. <div className="pl-4 border-l-2 border-bambu-dark-tertiary space-y-2">
  365. <p className="text-xs text-bambu-gray">No notifications during these hours</p>
  366. <div className="flex items-center gap-2">
  367. <Clock className="w-4 h-4 text-bambu-gray" />
  368. <span className="text-sm text-white">
  369. {formatTime(provider.quiet_hours_start) || '22:00'} - {formatTime(provider.quiet_hours_end) || '07:00'}
  370. </span>
  371. </div>
  372. <p className="text-xs text-bambu-gray">Edit provider to change quiet hours</p>
  373. </div>
  374. )}
  375. </div>
  376. {/* Daily Digest */}
  377. <div className="space-y-2">
  378. <div className="flex items-center justify-between">
  379. <div className="flex items-center gap-2">
  380. <Calendar className="w-4 h-4 text-emerald-400" />
  381. <p className="text-sm text-white">Daily Digest</p>
  382. </div>
  383. <Toggle
  384. checked={provider.daily_digest_enabled}
  385. onChange={(checked) => updateMutation.mutate({ daily_digest_enabled: checked })}
  386. />
  387. </div>
  388. {provider.daily_digest_enabled && (
  389. <div className="pl-4 border-l-2 border-bambu-dark-tertiary space-y-2">
  390. <p className="text-xs text-bambu-gray">Batch notifications into a single daily summary</p>
  391. <div className="flex items-center gap-2">
  392. <Clock className="w-4 h-4 text-bambu-gray" />
  393. <span className="text-sm text-white">
  394. Send at {formatTime(provider.daily_digest_time) || '08:00'}
  395. </span>
  396. </div>
  397. <p className="text-xs text-bambu-gray">Edit provider to change digest time</p>
  398. </div>
  399. )}
  400. </div>
  401. {/* Action Buttons */}
  402. <div className="flex gap-2 pt-2">
  403. <Button
  404. size="sm"
  405. variant="secondary"
  406. onClick={() => onEdit(provider)}
  407. className="flex-1"
  408. >
  409. <Edit2 className="w-4 h-4" />
  410. Edit
  411. </Button>
  412. <Button
  413. size="sm"
  414. variant="secondary"
  415. onClick={() => setShowDeleteConfirm(true)}
  416. className="text-red-400 hover:text-red-300"
  417. >
  418. <Trash2 className="w-4 h-4" />
  419. </Button>
  420. </div>
  421. </div>
  422. )}
  423. </CardContent>
  424. </Card>
  425. {/* Delete Confirmation */}
  426. {showDeleteConfirm && (
  427. <ConfirmModal
  428. title="Delete Notification Provider"
  429. message={`Are you sure you want to delete "${provider.name}"? This cannot be undone.`}
  430. confirmText="Delete"
  431. variant="danger"
  432. onConfirm={() => {
  433. deleteMutation.mutate();
  434. setShowDeleteConfirm(false);
  435. }}
  436. onCancel={() => setShowDeleteConfirm(false)}
  437. />
  438. )}
  439. </>
  440. );
  441. }