SmartPlugCard.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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, LayoutGrid } 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. {/* Show in Switchbar Toggle */}
  157. <div className="flex items-center justify-between">
  158. <div className="flex items-center gap-2">
  159. <LayoutGrid className="w-4 h-4 text-bambu-green" />
  160. <div>
  161. <p className="text-sm text-white">Show in Switchbar</p>
  162. <p className="text-xs text-bambu-gray">Quick access from sidebar</p>
  163. </div>
  164. </div>
  165. <label className="relative inline-flex items-center cursor-pointer">
  166. <input
  167. type="checkbox"
  168. checked={plug.show_in_switchbar}
  169. onChange={(e) => updateMutation.mutate({ show_in_switchbar: e.target.checked })}
  170. className="sr-only peer"
  171. />
  172. <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>
  173. </label>
  174. </div>
  175. {/* Enabled Toggle */}
  176. <div className="flex items-center justify-between">
  177. <div>
  178. <p className="text-sm text-white">Enabled</p>
  179. <p className="text-xs text-bambu-gray">Enable automation for this plug</p>
  180. </div>
  181. <label className="relative inline-flex items-center cursor-pointer">
  182. <input
  183. type="checkbox"
  184. checked={plug.enabled}
  185. onChange={(e) => updateMutation.mutate({ enabled: e.target.checked })}
  186. className="sr-only peer"
  187. />
  188. <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>
  189. </label>
  190. </div>
  191. {/* Auto On */}
  192. <div className="flex items-center justify-between">
  193. <div>
  194. <p className="text-sm text-white">Auto On</p>
  195. <p className="text-xs text-bambu-gray">Turn on when print starts</p>
  196. </div>
  197. <label className="relative inline-flex items-center cursor-pointer">
  198. <input
  199. type="checkbox"
  200. checked={plug.auto_on}
  201. onChange={(e) => updateMutation.mutate({ auto_on: e.target.checked })}
  202. className="sr-only peer"
  203. />
  204. <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>
  205. </label>
  206. </div>
  207. {/* Auto Off */}
  208. <div className="flex items-center justify-between">
  209. <div>
  210. <p className="text-sm text-white">Auto Off</p>
  211. <p className="text-xs text-bambu-gray">Turn off when print completes (one-shot)</p>
  212. </div>
  213. <label className="relative inline-flex items-center cursor-pointer">
  214. <input
  215. type="checkbox"
  216. checked={plug.auto_off}
  217. onChange={(e) => updateMutation.mutate({ auto_off: e.target.checked })}
  218. className="sr-only peer"
  219. />
  220. <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>
  221. </label>
  222. </div>
  223. {/* Delay Mode */}
  224. {plug.auto_off && (
  225. <div className="space-y-3 pl-4 border-l-2 border-bambu-dark-tertiary">
  226. <div>
  227. <p className="text-sm text-white mb-2">Turn Off Delay Mode</p>
  228. <div className="flex gap-2">
  229. <button
  230. onClick={() => updateMutation.mutate({ off_delay_mode: 'time' })}
  231. className={`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-sm transition-colors ${
  232. plug.off_delay_mode === 'time'
  233. ? 'bg-bambu-green text-white'
  234. : 'bg-bambu-dark text-bambu-gray hover:text-white'
  235. }`}
  236. >
  237. <Clock className="w-4 h-4" />
  238. Time
  239. </button>
  240. <button
  241. onClick={() => updateMutation.mutate({ off_delay_mode: 'temperature' })}
  242. className={`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-sm transition-colors ${
  243. plug.off_delay_mode === 'temperature'
  244. ? 'bg-bambu-green text-white'
  245. : 'bg-bambu-dark text-bambu-gray hover:text-white'
  246. }`}
  247. >
  248. <Thermometer className="w-4 h-4" />
  249. Temp
  250. </button>
  251. </div>
  252. </div>
  253. {plug.off_delay_mode === 'time' ? (
  254. <div>
  255. <label className="block text-xs text-bambu-gray mb-1">Delay (minutes)</label>
  256. <input
  257. type="number"
  258. min="1"
  259. max="60"
  260. value={plug.off_delay_minutes}
  261. onChange={(e) => updateMutation.mutate({ off_delay_minutes: parseInt(e.target.value) || 5 })}
  262. 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"
  263. />
  264. </div>
  265. ) : (
  266. <div>
  267. <label className="block text-xs text-bambu-gray mb-1">Temperature threshold (C)</label>
  268. <input
  269. type="number"
  270. min="30"
  271. max="100"
  272. value={plug.off_temp_threshold}
  273. onChange={(e) => updateMutation.mutate({ off_temp_threshold: parseInt(e.target.value) || 70 })}
  274. 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"
  275. />
  276. <p className="text-xs text-bambu-gray mt-1">Turns off when nozzle cools below this temperature</p>
  277. </div>
  278. )}
  279. </div>
  280. )}
  281. {/* Action Buttons */}
  282. <div className="flex gap-2 pt-2">
  283. <Button
  284. size="sm"
  285. variant="secondary"
  286. onClick={() => onEdit(plug)}
  287. className="flex-1"
  288. >
  289. <Edit2 className="w-4 h-4" />
  290. Edit
  291. </Button>
  292. <Button
  293. size="sm"
  294. variant="secondary"
  295. onClick={() => setShowDeleteConfirm(true)}
  296. className="text-red-400 hover:text-red-300"
  297. >
  298. <Trash2 className="w-4 h-4" />
  299. </Button>
  300. </div>
  301. </div>
  302. )}
  303. </CardContent>
  304. </Card>
  305. {/* Delete Confirmation */}
  306. {showDeleteConfirm && (
  307. <ConfirmModal
  308. title="Delete Smart Plug"
  309. message={`Are you sure you want to delete "${plug.name}"? This cannot be undone.`}
  310. confirmText="Delete"
  311. variant="danger"
  312. onConfirm={() => {
  313. deleteMutation.mutate();
  314. setShowDeleteConfirm(false);
  315. }}
  316. onCancel={() => setShowDeleteConfirm(false)}
  317. />
  318. )}
  319. {/* Power On Confirmation */}
  320. {showPowerOnConfirm && (
  321. <ConfirmModal
  322. title="Turn On Smart Plug"
  323. message={`Are you sure you want to turn on "${plug.name}"?`}
  324. confirmText="Turn On"
  325. variant="default"
  326. onConfirm={() => {
  327. controlMutation.mutate('on');
  328. setShowPowerOnConfirm(false);
  329. }}
  330. onCancel={() => setShowPowerOnConfirm(false)}
  331. />
  332. )}
  333. {/* Power Off Confirmation */}
  334. {showPowerOffConfirm && (
  335. <ConfirmModal
  336. title="Turn Off Smart Plug"
  337. message={`Are you sure you want to turn off "${plug.name}"? This will cut power to the connected device.`}
  338. confirmText="Turn Off"
  339. variant="danger"
  340. onConfirm={() => {
  341. controlMutation.mutate('off');
  342. setShowPowerOffConfirm(false);
  343. }}
  344. onCancel={() => setShowPowerOffConfirm(false)}
  345. />
  346. )}
  347. </>
  348. );
  349. }