SmartPlugCard.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 } 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. {/* Quick Controls */}
  99. <div className="flex gap-2 mb-3">
  100. <Button
  101. size="sm"
  102. variant={isOn ? 'primary' : 'secondary'}
  103. disabled={!isReachable || isPending}
  104. onClick={() => setShowPowerOnConfirm(true)}
  105. className="flex-1"
  106. >
  107. {isPending ? <Loader2 className="w-4 h-4 animate-spin" /> : <Power className="w-4 h-4" />}
  108. On
  109. </Button>
  110. <Button
  111. size="sm"
  112. variant={!isOn ? 'primary' : 'secondary'}
  113. disabled={!isReachable || isPending}
  114. onClick={() => setShowPowerOffConfirm(true)}
  115. className="flex-1"
  116. >
  117. {isPending ? <Loader2 className="w-4 h-4 animate-spin" /> : <PowerOff className="w-4 h-4" />}
  118. Off
  119. </Button>
  120. </div>
  121. {/* Toggle Settings Panel */}
  122. <button
  123. onClick={() => setIsExpanded(!isExpanded)}
  124. className="w-full flex items-center justify-between py-2 text-sm text-bambu-gray hover:text-white transition-colors"
  125. >
  126. <span className="flex items-center gap-2">
  127. <Settings2 className="w-4 h-4" />
  128. Automation Settings
  129. </span>
  130. <span>{isExpanded ? '-' : '+'}</span>
  131. </button>
  132. {/* Expanded Settings */}
  133. {isExpanded && (
  134. <div className="pt-3 border-t border-bambu-dark-tertiary space-y-4">
  135. {/* Enabled Toggle */}
  136. <div className="flex items-center justify-between">
  137. <div>
  138. <p className="text-sm text-white">Enabled</p>
  139. <p className="text-xs text-bambu-gray">Enable automation for this plug</p>
  140. </div>
  141. <label className="relative inline-flex items-center cursor-pointer">
  142. <input
  143. type="checkbox"
  144. checked={plug.enabled}
  145. onChange={(e) => updateMutation.mutate({ enabled: e.target.checked })}
  146. className="sr-only peer"
  147. />
  148. <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>
  149. </label>
  150. </div>
  151. {/* Auto On */}
  152. <div className="flex items-center justify-between">
  153. <div>
  154. <p className="text-sm text-white">Auto On</p>
  155. <p className="text-xs text-bambu-gray">Turn on when print starts</p>
  156. </div>
  157. <label className="relative inline-flex items-center cursor-pointer">
  158. <input
  159. type="checkbox"
  160. checked={plug.auto_on}
  161. onChange={(e) => updateMutation.mutate({ auto_on: e.target.checked })}
  162. className="sr-only peer"
  163. />
  164. <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>
  165. </label>
  166. </div>
  167. {/* Auto Off */}
  168. <div className="flex items-center justify-between">
  169. <div>
  170. <p className="text-sm text-white">Auto Off</p>
  171. <p className="text-xs text-bambu-gray">Turn off when print completes (one-shot)</p>
  172. </div>
  173. <label className="relative inline-flex items-center cursor-pointer">
  174. <input
  175. type="checkbox"
  176. checked={plug.auto_off}
  177. onChange={(e) => updateMutation.mutate({ auto_off: e.target.checked })}
  178. className="sr-only peer"
  179. />
  180. <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>
  181. </label>
  182. </div>
  183. {/* Delay Mode */}
  184. {plug.auto_off && (
  185. <div className="space-y-3 pl-4 border-l-2 border-bambu-dark-tertiary">
  186. <div>
  187. <p className="text-sm text-white mb-2">Turn Off Delay Mode</p>
  188. <div className="flex gap-2">
  189. <button
  190. onClick={() => updateMutation.mutate({ off_delay_mode: 'time' })}
  191. className={`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-sm transition-colors ${
  192. plug.off_delay_mode === 'time'
  193. ? 'bg-bambu-green text-white'
  194. : 'bg-bambu-dark text-bambu-gray hover:text-white'
  195. }`}
  196. >
  197. <Clock className="w-4 h-4" />
  198. Time
  199. </button>
  200. <button
  201. onClick={() => updateMutation.mutate({ off_delay_mode: 'temperature' })}
  202. className={`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-sm transition-colors ${
  203. plug.off_delay_mode === 'temperature'
  204. ? 'bg-bambu-green text-white'
  205. : 'bg-bambu-dark text-bambu-gray hover:text-white'
  206. }`}
  207. >
  208. <Thermometer className="w-4 h-4" />
  209. Temp
  210. </button>
  211. </div>
  212. </div>
  213. {plug.off_delay_mode === 'time' ? (
  214. <div>
  215. <label className="block text-xs text-bambu-gray mb-1">Delay (minutes)</label>
  216. <input
  217. type="number"
  218. min="1"
  219. max="60"
  220. value={plug.off_delay_minutes}
  221. onChange={(e) => updateMutation.mutate({ off_delay_minutes: parseInt(e.target.value) || 5 })}
  222. 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"
  223. />
  224. </div>
  225. ) : (
  226. <div>
  227. <label className="block text-xs text-bambu-gray mb-1">Temperature threshold (C)</label>
  228. <input
  229. type="number"
  230. min="30"
  231. max="100"
  232. value={plug.off_temp_threshold}
  233. onChange={(e) => updateMutation.mutate({ off_temp_threshold: parseInt(e.target.value) || 70 })}
  234. 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"
  235. />
  236. <p className="text-xs text-bambu-gray mt-1">Turns off when nozzle cools below this temperature</p>
  237. </div>
  238. )}
  239. </div>
  240. )}
  241. {/* Action Buttons */}
  242. <div className="flex gap-2 pt-2">
  243. <Button
  244. size="sm"
  245. variant="secondary"
  246. onClick={() => onEdit(plug)}
  247. className="flex-1"
  248. >
  249. <Edit2 className="w-4 h-4" />
  250. Edit
  251. </Button>
  252. <Button
  253. size="sm"
  254. variant="secondary"
  255. onClick={() => setShowDeleteConfirm(true)}
  256. className="text-red-400 hover:text-red-300"
  257. >
  258. <Trash2 className="w-4 h-4" />
  259. </Button>
  260. </div>
  261. </div>
  262. )}
  263. </CardContent>
  264. </Card>
  265. {/* Delete Confirmation */}
  266. {showDeleteConfirm && (
  267. <ConfirmModal
  268. title="Delete Smart Plug"
  269. message={`Are you sure you want to delete "${plug.name}"? This cannot be undone.`}
  270. confirmText="Delete"
  271. variant="danger"
  272. onConfirm={() => {
  273. deleteMutation.mutate();
  274. setShowDeleteConfirm(false);
  275. }}
  276. onCancel={() => setShowDeleteConfirm(false)}
  277. />
  278. )}
  279. {/* Power On Confirmation */}
  280. {showPowerOnConfirm && (
  281. <ConfirmModal
  282. title="Turn On Smart Plug"
  283. message={`Are you sure you want to turn on "${plug.name}"?`}
  284. confirmText="Turn On"
  285. variant="default"
  286. onConfirm={() => {
  287. controlMutation.mutate('on');
  288. setShowPowerOnConfirm(false);
  289. }}
  290. onCancel={() => setShowPowerOnConfirm(false)}
  291. />
  292. )}
  293. {/* Power Off Confirmation */}
  294. {showPowerOffConfirm && (
  295. <ConfirmModal
  296. title="Turn Off Smart Plug"
  297. message={`Are you sure you want to turn off "${plug.name}"? This will cut power to the connected device.`}
  298. confirmText="Turn Off"
  299. variant="danger"
  300. onConfirm={() => {
  301. controlMutation.mutate('off');
  302. setShowPowerOffConfirm(false);
  303. }}
  304. onCancel={() => setShowPowerOffConfirm(false)}
  305. />
  306. )}
  307. </>
  308. );
  309. }