SmartPlugCard.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import { useState } from 'react';
  2. import { useMutation, useQueryClient, useQuery } from '@tanstack/react-query';
  3. import { Plug, Power, PowerOff, Loader2, Trash2, Settings2, Thermometer, Clock, Wifi, WifiOff, Edit2, Bell, Calendar } from 'lucide-react';
  4. import { api } from '../api/client';
  5. import type { SmartPlug, SmartPlugUpdate } from '../api/client';
  6. import { Card, CardContent } from './Card';
  7. import { Button } from './Button';
  8. import { ConfirmModal } from './ConfirmModal';
  9. interface SmartPlugCardProps {
  10. plug: SmartPlug;
  11. onEdit: (plug: SmartPlug) => void;
  12. }
  13. export function SmartPlugCard({ plug, onEdit }: SmartPlugCardProps) {
  14. const queryClient = useQueryClient();
  15. const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
  16. const [showPowerOnConfirm, setShowPowerOnConfirm] = useState(false);
  17. const [showPowerOffConfirm, setShowPowerOffConfirm] = useState(false);
  18. const [isExpanded, setIsExpanded] = useState(false);
  19. // Fetch current status
  20. const { data: status, isLoading: statusLoading, refetch: refetchStatus } = useQuery({
  21. queryKey: ['smart-plug-status', plug.id],
  22. queryFn: () => api.getSmartPlugStatus(plug.id),
  23. refetchInterval: 30000, // Refresh every 30 seconds
  24. });
  25. // Fetch printers for linking
  26. const { data: printers } = useQuery({
  27. queryKey: ['printers'],
  28. queryFn: api.getPrinters,
  29. });
  30. const linkedPrinter = printers?.find(p => p.id === plug.printer_id);
  31. // Control mutation
  32. const controlMutation = useMutation({
  33. mutationFn: (action: 'on' | 'off' | 'toggle') => api.controlSmartPlug(plug.id, action),
  34. onSuccess: () => {
  35. refetchStatus();
  36. },
  37. });
  38. // Update mutation
  39. const updateMutation = useMutation({
  40. mutationFn: (data: SmartPlugUpdate) => api.updateSmartPlug(plug.id, data),
  41. onSuccess: () => {
  42. queryClient.invalidateQueries({ queryKey: ['smart-plugs'] });
  43. // Also invalidate printer-specific smart plug queries to keep PrintersPage in sync
  44. if (plug.printer_id) {
  45. queryClient.invalidateQueries({ queryKey: ['smartPlugByPrinter', plug.printer_id] });
  46. }
  47. },
  48. });
  49. // Delete mutation
  50. const deleteMutation = useMutation({
  51. mutationFn: () => api.deleteSmartPlug(plug.id),
  52. onSuccess: () => {
  53. queryClient.invalidateQueries({ queryKey: ['smart-plugs'] });
  54. },
  55. });
  56. const isOn = status?.state === 'ON';
  57. const isReachable = status?.reachable ?? false;
  58. const isPending = controlMutation.isPending;
  59. return (
  60. <>
  61. <Card className="relative">
  62. <CardContent className="p-4">
  63. {/* Header Row */}
  64. <div className="flex items-start justify-between mb-3">
  65. <div className="flex items-center gap-3">
  66. <div className={`p-2 rounded-lg ${isReachable ? (isOn ? 'bg-bambu-green/20' : 'bg-bambu-dark') : 'bg-red-500/20'}`}>
  67. <Plug className={`w-5 h-5 ${isReachable ? (isOn ? 'text-bambu-green' : 'text-bambu-gray') : 'text-red-400'}`} />
  68. </div>
  69. <div>
  70. <h3 className="font-medium text-white">{plug.name}</h3>
  71. <p className="text-sm text-bambu-gray">{plug.ip_address}</p>
  72. </div>
  73. </div>
  74. {/* Status indicator */}
  75. <div className="flex items-center gap-2">
  76. {statusLoading ? (
  77. <Loader2 className="w-4 h-4 text-bambu-gray animate-spin" />
  78. ) : isReachable ? (
  79. <div className="flex items-center gap-1 text-sm">
  80. <Wifi className="w-4 h-4 text-bambu-green" />
  81. <span className={isOn ? 'text-bambu-green' : 'text-bambu-gray'}>{status?.state || 'Unknown'}</span>
  82. </div>
  83. ) : (
  84. <div className="flex items-center gap-1 text-sm text-red-400">
  85. <WifiOff className="w-4 h-4" />
  86. <span>Offline</span>
  87. </div>
  88. )}
  89. </div>
  90. </div>
  91. {/* Linked Printer */}
  92. {linkedPrinter && (
  93. <div className="mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg">
  94. <span className="text-xs text-bambu-gray">Linked to: </span>
  95. <span className="text-sm text-white">{linkedPrinter.name}</span>
  96. </div>
  97. )}
  98. {/* Feature Badges */}
  99. {(plug.power_alert_enabled || plug.schedule_enabled) && (
  100. <div className="flex flex-wrap gap-1.5 mb-3">
  101. {plug.power_alert_enabled && (
  102. <span className="flex items-center gap-1 px-2 py-0.5 bg-yellow-500/20 text-yellow-400 text-xs rounded-full">
  103. <Bell className="w-3 h-3" />
  104. Alerts
  105. </span>
  106. )}
  107. {plug.schedule_enabled && (
  108. <span className="flex items-center gap-1 px-2 py-0.5 bg-blue-500/20 text-blue-400 text-xs rounded-full">
  109. <Calendar className="w-3 h-3" />
  110. {plug.schedule_on_time && plug.schedule_off_time
  111. ? `${plug.schedule_on_time} - ${plug.schedule_off_time}`
  112. : plug.schedule_on_time
  113. ? `On ${plug.schedule_on_time}`
  114. : `Off ${plug.schedule_off_time}`}
  115. </span>
  116. )}
  117. </div>
  118. )}
  119. {/* Quick Controls */}
  120. <div className="flex gap-2 mb-3">
  121. <Button
  122. size="sm"
  123. variant={isOn ? 'primary' : 'secondary'}
  124. disabled={!isReachable || isPending}
  125. onClick={() => setShowPowerOnConfirm(true)}
  126. className="flex-1"
  127. >
  128. {isPending ? <Loader2 className="w-4 h-4 animate-spin" /> : <Power className="w-4 h-4" />}
  129. On
  130. </Button>
  131. <Button
  132. size="sm"
  133. variant={!isOn ? 'primary' : 'secondary'}
  134. disabled={!isReachable || isPending}
  135. onClick={() => setShowPowerOffConfirm(true)}
  136. className="flex-1"
  137. >
  138. {isPending ? <Loader2 className="w-4 h-4 animate-spin" /> : <PowerOff className="w-4 h-4" />}
  139. Off
  140. </Button>
  141. </div>
  142. {/* Toggle Settings Panel */}
  143. <button
  144. onClick={() => setIsExpanded(!isExpanded)}
  145. className="w-full flex items-center justify-between py-2 text-sm text-bambu-gray hover:text-white transition-colors"
  146. >
  147. <span className="flex items-center gap-2">
  148. <Settings2 className="w-4 h-4" />
  149. Automation Settings
  150. </span>
  151. <span>{isExpanded ? '-' : '+'}</span>
  152. </button>
  153. {/* Expanded Settings */}
  154. {isExpanded && (
  155. <div className="pt-3 border-t border-bambu-dark-tertiary space-y-4">
  156. {/* Enabled Toggle */}
  157. <div className="flex items-center justify-between">
  158. <div>
  159. <p className="text-sm text-white">Enabled</p>
  160. <p className="text-xs text-bambu-gray">Enable automation for this plug</p>
  161. </div>
  162. <label className="relative inline-flex items-center cursor-pointer">
  163. <input
  164. type="checkbox"
  165. checked={plug.enabled}
  166. onChange={(e) => updateMutation.mutate({ enabled: e.target.checked })}
  167. className="sr-only peer"
  168. />
  169. <div className="w-9 h-5 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-bambu-green"></div>
  170. </label>
  171. </div>
  172. {/* Auto On */}
  173. <div className="flex items-center justify-between">
  174. <div>
  175. <p className="text-sm text-white">Auto On</p>
  176. <p className="text-xs text-bambu-gray">Turn on when print starts</p>
  177. </div>
  178. <label className="relative inline-flex items-center cursor-pointer">
  179. <input
  180. type="checkbox"
  181. checked={plug.auto_on}
  182. onChange={(e) => updateMutation.mutate({ auto_on: e.target.checked })}
  183. className="sr-only peer"
  184. />
  185. <div className="w-9 h-5 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-bambu-green"></div>
  186. </label>
  187. </div>
  188. {/* Auto Off */}
  189. <div className="flex items-center justify-between">
  190. <div>
  191. <p className="text-sm text-white">Auto Off</p>
  192. <p className="text-xs text-bambu-gray">Turn off when print completes (one-shot)</p>
  193. </div>
  194. <label className="relative inline-flex items-center cursor-pointer">
  195. <input
  196. type="checkbox"
  197. checked={plug.auto_off}
  198. onChange={(e) => updateMutation.mutate({ auto_off: e.target.checked })}
  199. className="sr-only peer"
  200. />
  201. <div className="w-9 h-5 bg-bambu-dark-tertiary peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-bambu-green"></div>
  202. </label>
  203. </div>
  204. {/* Delay Mode */}
  205. {plug.auto_off && (
  206. <div className="space-y-3 pl-4 border-l-2 border-bambu-dark-tertiary">
  207. <div>
  208. <p className="text-sm text-white mb-2">Turn Off Delay Mode</p>
  209. <div className="flex gap-2">
  210. <button
  211. onClick={() => updateMutation.mutate({ off_delay_mode: 'time' })}
  212. className={`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-sm transition-colors ${
  213. plug.off_delay_mode === 'time'
  214. ? 'bg-bambu-green text-white'
  215. : 'bg-bambu-dark text-bambu-gray hover:text-white'
  216. }`}
  217. >
  218. <Clock className="w-4 h-4" />
  219. Time
  220. </button>
  221. <button
  222. onClick={() => updateMutation.mutate({ off_delay_mode: 'temperature' })}
  223. className={`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-sm transition-colors ${
  224. plug.off_delay_mode === 'temperature'
  225. ? 'bg-bambu-green text-white'
  226. : 'bg-bambu-dark text-bambu-gray hover:text-white'
  227. }`}
  228. >
  229. <Thermometer className="w-4 h-4" />
  230. Temp
  231. </button>
  232. </div>
  233. </div>
  234. {plug.off_delay_mode === 'time' ? (
  235. <div>
  236. <label className="block text-xs text-bambu-gray mb-1">Delay (minutes)</label>
  237. <input
  238. type="number"
  239. min="1"
  240. max="60"
  241. value={plug.off_delay_minutes}
  242. onChange={(e) => updateMutation.mutate({ off_delay_minutes: parseInt(e.target.value) || 5 })}
  243. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none"
  244. />
  245. </div>
  246. ) : (
  247. <div>
  248. <label className="block text-xs text-bambu-gray mb-1">Temperature threshold (C)</label>
  249. <input
  250. type="number"
  251. min="30"
  252. max="100"
  253. value={plug.off_temp_threshold}
  254. onChange={(e) => updateMutation.mutate({ off_temp_threshold: parseInt(e.target.value) || 70 })}
  255. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none"
  256. />
  257. <p className="text-xs text-bambu-gray mt-1">Turns off when nozzle cools below this temperature</p>
  258. </div>
  259. )}
  260. </div>
  261. )}
  262. {/* Action Buttons */}
  263. <div className="flex gap-2 pt-2">
  264. <Button
  265. size="sm"
  266. variant="secondary"
  267. onClick={() => onEdit(plug)}
  268. className="flex-1"
  269. >
  270. <Edit2 className="w-4 h-4" />
  271. Edit
  272. </Button>
  273. <Button
  274. size="sm"
  275. variant="secondary"
  276. onClick={() => setShowDeleteConfirm(true)}
  277. className="text-red-400 hover:text-red-300"
  278. >
  279. <Trash2 className="w-4 h-4" />
  280. </Button>
  281. </div>
  282. </div>
  283. )}
  284. </CardContent>
  285. </Card>
  286. {/* Delete Confirmation */}
  287. {showDeleteConfirm && (
  288. <ConfirmModal
  289. title="Delete Smart Plug"
  290. message={`Are you sure you want to delete "${plug.name}"? This cannot be undone.`}
  291. confirmText="Delete"
  292. variant="danger"
  293. onConfirm={() => {
  294. deleteMutation.mutate();
  295. setShowDeleteConfirm(false);
  296. }}
  297. onCancel={() => setShowDeleteConfirm(false)}
  298. />
  299. )}
  300. {/* Power On Confirmation */}
  301. {showPowerOnConfirm && (
  302. <ConfirmModal
  303. title="Turn On Smart Plug"
  304. message={`Are you sure you want to turn on "${plug.name}"?`}
  305. confirmText="Turn On"
  306. variant="default"
  307. onConfirm={() => {
  308. controlMutation.mutate('on');
  309. setShowPowerOnConfirm(false);
  310. }}
  311. onCancel={() => setShowPowerOnConfirm(false)}
  312. />
  313. )}
  314. {/* Power Off Confirmation */}
  315. {showPowerOffConfirm && (
  316. <ConfirmModal
  317. title="Turn Off Smart Plug"
  318. message={`Are you sure you want to turn off "${plug.name}"? This will cut power to the connected device.`}
  319. confirmText="Turn Off"
  320. variant="danger"
  321. onConfirm={() => {
  322. controlMutation.mutate('off');
  323. setShowPowerOffConfirm(false);
  324. }}
  325. onCancel={() => setShowPowerOffConfirm(false)}
  326. />
  327. )}
  328. </>
  329. );
  330. }