| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- import { useState } from 'react';
- import { useMutation, useQueryClient, useQuery } from '@tanstack/react-query';
- import { Plug, Power, PowerOff, Loader2, Trash2, Settings2, Thermometer, Clock, Wifi, WifiOff, Edit2, Bell, Calendar, LayoutGrid } from 'lucide-react';
- import { api } from '../api/client';
- import type { SmartPlug, SmartPlugUpdate } from '../api/client';
- import { Card, CardContent } from './Card';
- import { Button } from './Button';
- import { ConfirmModal } from './ConfirmModal';
- interface SmartPlugCardProps {
- plug: SmartPlug;
- onEdit: (plug: SmartPlug) => void;
- }
- export function SmartPlugCard({ plug, onEdit }: SmartPlugCardProps) {
- const queryClient = useQueryClient();
- const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
- const [showPowerOnConfirm, setShowPowerOnConfirm] = useState(false);
- const [showPowerOffConfirm, setShowPowerOffConfirm] = useState(false);
- const [isExpanded, setIsExpanded] = useState(false);
- // Fetch current status
- const { data: status, isLoading: statusLoading, refetch: refetchStatus } = useQuery({
- queryKey: ['smart-plug-status', plug.id],
- queryFn: () => api.getSmartPlugStatus(plug.id),
- refetchInterval: 30000, // Refresh every 30 seconds
- });
- // Fetch printers for linking
- const { data: printers } = useQuery({
- queryKey: ['printers'],
- queryFn: api.getPrinters,
- });
- const linkedPrinter = printers?.find(p => p.id === plug.printer_id);
- // Control mutation
- const controlMutation = useMutation({
- mutationFn: (action: 'on' | 'off' | 'toggle') => api.controlSmartPlug(plug.id, action),
- onSuccess: () => {
- refetchStatus();
- },
- });
- // Update mutation
- const updateMutation = useMutation({
- mutationFn: (data: SmartPlugUpdate) => api.updateSmartPlug(plug.id, data),
- onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: ['smart-plugs'] });
- // Also invalidate printer-specific smart plug queries to keep PrintersPage in sync
- if (plug.printer_id) {
- queryClient.invalidateQueries({ queryKey: ['smartPlugByPrinter', plug.printer_id] });
- }
- },
- });
- // Delete mutation
- const deleteMutation = useMutation({
- mutationFn: () => api.deleteSmartPlug(plug.id),
- onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: ['smart-plugs'] });
- },
- });
- const isOn = status?.state === 'ON';
- const isReachable = status?.reachable ?? false;
- const isPending = controlMutation.isPending;
- return (
- <>
- <Card className="relative">
- <CardContent className="p-4">
- {/* Header Row */}
- <div className="flex items-start justify-between mb-3">
- <div className="flex items-center gap-3">
- <div className={`p-2 rounded-lg ${isReachable ? (isOn ? 'bg-bambu-green/20' : 'bg-bambu-dark') : 'bg-red-500/20'}`}>
- <Plug className={`w-5 h-5 ${isReachable ? (isOn ? 'text-bambu-green' : 'text-bambu-gray') : 'text-red-400'}`} />
- </div>
- <div>
- <h3 className="font-medium text-white">{plug.name}</h3>
- <p className="text-sm text-bambu-gray">{plug.ip_address}</p>
- </div>
- </div>
- {/* Status indicator */}
- <div className="flex items-center gap-2">
- {statusLoading ? (
- <Loader2 className="w-4 h-4 text-bambu-gray animate-spin" />
- ) : isReachable ? (
- <div className="flex items-center gap-1 text-sm">
- <Wifi className="w-4 h-4 text-bambu-green" />
- <span className={isOn ? 'text-bambu-green' : 'text-bambu-gray'}>{status?.state || 'Unknown'}</span>
- </div>
- ) : (
- <div className="flex items-center gap-1 text-sm text-red-400">
- <WifiOff className="w-4 h-4" />
- <span>Offline</span>
- </div>
- )}
- </div>
- </div>
- {/* Linked Printer */}
- {linkedPrinter && (
- <div className="mb-3 px-2 py-1.5 bg-bambu-dark rounded-lg">
- <span className="text-xs text-bambu-gray">Linked to: </span>
- <span className="text-sm text-white">{linkedPrinter.name}</span>
- </div>
- )}
- {/* Feature Badges */}
- {(plug.power_alert_enabled || plug.schedule_enabled) && (
- <div className="flex flex-wrap gap-1.5 mb-3">
- {plug.power_alert_enabled && (
- <span className="flex items-center gap-1 px-2 py-0.5 bg-yellow-500/20 text-yellow-400 text-xs rounded-full">
- <Bell className="w-3 h-3" />
- Alerts
- </span>
- )}
- {plug.schedule_enabled && (
- <span className="flex items-center gap-1 px-2 py-0.5 bg-blue-500/20 text-blue-400 text-xs rounded-full">
- <Calendar className="w-3 h-3" />
- {plug.schedule_on_time && plug.schedule_off_time
- ? `${plug.schedule_on_time} - ${plug.schedule_off_time}`
- : plug.schedule_on_time
- ? `On ${plug.schedule_on_time}`
- : `Off ${plug.schedule_off_time}`}
- </span>
- )}
- </div>
- )}
- {/* Quick Controls */}
- <div className="flex gap-2 mb-3">
- <Button
- size="sm"
- variant={isOn ? 'primary' : 'secondary'}
- disabled={!isReachable || isPending}
- onClick={() => setShowPowerOnConfirm(true)}
- className="flex-1"
- >
- {isPending ? <Loader2 className="w-4 h-4 animate-spin" /> : <Power className="w-4 h-4" />}
- On
- </Button>
- <Button
- size="sm"
- variant={!isOn ? 'primary' : 'secondary'}
- disabled={!isReachable || isPending}
- onClick={() => setShowPowerOffConfirm(true)}
- className="flex-1"
- >
- {isPending ? <Loader2 className="w-4 h-4 animate-spin" /> : <PowerOff className="w-4 h-4" />}
- Off
- </Button>
- </div>
- {/* Toggle Settings Panel */}
- <button
- onClick={() => setIsExpanded(!isExpanded)}
- className="w-full flex items-center justify-between py-2 text-sm text-bambu-gray hover:text-white transition-colors"
- >
- <span className="flex items-center gap-2">
- <Settings2 className="w-4 h-4" />
- Automation Settings
- </span>
- <span>{isExpanded ? '-' : '+'}</span>
- </button>
- {/* Expanded Settings */}
- {isExpanded && (
- <div className="pt-3 border-t border-bambu-dark-tertiary space-y-4">
- {/* Show in Switchbar Toggle */}
- <div className="flex items-center justify-between">
- <div className="flex items-center gap-2">
- <LayoutGrid className="w-4 h-4 text-bambu-green" />
- <div>
- <p className="text-sm text-white">Show in Switchbar</p>
- <p className="text-xs text-bambu-gray">Quick access from sidebar</p>
- </div>
- </div>
- <label className="relative inline-flex items-center cursor-pointer">
- <input
- type="checkbox"
- checked={plug.show_in_switchbar}
- onChange={(e) => updateMutation.mutate({ show_in_switchbar: e.target.checked })}
- className="sr-only peer"
- />
- <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>
- </label>
- </div>
- {/* Enabled Toggle */}
- <div className="flex items-center justify-between">
- <div>
- <p className="text-sm text-white">Enabled</p>
- <p className="text-xs text-bambu-gray">Enable automation for this plug</p>
- </div>
- <label className="relative inline-flex items-center cursor-pointer">
- <input
- type="checkbox"
- checked={plug.enabled}
- onChange={(e) => updateMutation.mutate({ enabled: e.target.checked })}
- className="sr-only peer"
- />
- <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>
- </label>
- </div>
- {/* Auto On */}
- <div className="flex items-center justify-between">
- <div>
- <p className="text-sm text-white">Auto On</p>
- <p className="text-xs text-bambu-gray">Turn on when print starts</p>
- </div>
- <label className="relative inline-flex items-center cursor-pointer">
- <input
- type="checkbox"
- checked={plug.auto_on}
- onChange={(e) => updateMutation.mutate({ auto_on: e.target.checked })}
- className="sr-only peer"
- />
- <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>
- </label>
- </div>
- {/* Auto Off */}
- <div className="flex items-center justify-between">
- <div>
- <p className="text-sm text-white">Auto Off</p>
- <p className="text-xs text-bambu-gray">Turn off when print completes (one-shot)</p>
- </div>
- <label className="relative inline-flex items-center cursor-pointer">
- <input
- type="checkbox"
- checked={plug.auto_off}
- onChange={(e) => updateMutation.mutate({ auto_off: e.target.checked })}
- className="sr-only peer"
- />
- <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>
- </label>
- </div>
- {/* Delay Mode */}
- {plug.auto_off && (
- <div className="space-y-3 pl-4 border-l-2 border-bambu-dark-tertiary">
- <div>
- <p className="text-sm text-white mb-2">Turn Off Delay Mode</p>
- <div className="flex gap-2">
- <button
- onClick={() => updateMutation.mutate({ off_delay_mode: 'time' })}
- className={`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-sm transition-colors ${
- plug.off_delay_mode === 'time'
- ? 'bg-bambu-green text-white'
- : 'bg-bambu-dark text-bambu-gray hover:text-white'
- }`}
- >
- <Clock className="w-4 h-4" />
- Time
- </button>
- <button
- onClick={() => updateMutation.mutate({ off_delay_mode: 'temperature' })}
- className={`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg text-sm transition-colors ${
- plug.off_delay_mode === 'temperature'
- ? 'bg-bambu-green text-white'
- : 'bg-bambu-dark text-bambu-gray hover:text-white'
- }`}
- >
- <Thermometer className="w-4 h-4" />
- Temp
- </button>
- </div>
- </div>
- {plug.off_delay_mode === 'time' ? (
- <div>
- <label className="block text-xs text-bambu-gray mb-1">Delay (minutes)</label>
- <input
- type="number"
- min="1"
- max="60"
- value={plug.off_delay_minutes}
- onChange={(e) => updateMutation.mutate({ off_delay_minutes: parseInt(e.target.value) || 5 })}
- 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"
- />
- </div>
- ) : (
- <div>
- <label className="block text-xs text-bambu-gray mb-1">Temperature threshold (C)</label>
- <input
- type="number"
- min="30"
- max="100"
- value={plug.off_temp_threshold}
- onChange={(e) => updateMutation.mutate({ off_temp_threshold: parseInt(e.target.value) || 70 })}
- 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"
- />
- <p className="text-xs text-bambu-gray mt-1">Turns off when nozzle cools below this temperature</p>
- </div>
- )}
- </div>
- )}
- {/* Action Buttons */}
- <div className="flex gap-2 pt-2">
- <Button
- size="sm"
- variant="secondary"
- onClick={() => onEdit(plug)}
- className="flex-1"
- >
- <Edit2 className="w-4 h-4" />
- Edit
- </Button>
- <Button
- size="sm"
- variant="secondary"
- onClick={() => setShowDeleteConfirm(true)}
- className="text-red-400 hover:text-red-300"
- >
- <Trash2 className="w-4 h-4" />
- </Button>
- </div>
- </div>
- )}
- </CardContent>
- </Card>
- {/* Delete Confirmation */}
- {showDeleteConfirm && (
- <ConfirmModal
- title="Delete Smart Plug"
- message={`Are you sure you want to delete "${plug.name}"? This cannot be undone.`}
- confirmText="Delete"
- variant="danger"
- onConfirm={() => {
- deleteMutation.mutate();
- setShowDeleteConfirm(false);
- }}
- onCancel={() => setShowDeleteConfirm(false)}
- />
- )}
- {/* Power On Confirmation */}
- {showPowerOnConfirm && (
- <ConfirmModal
- title="Turn On Smart Plug"
- message={`Are you sure you want to turn on "${plug.name}"?`}
- confirmText="Turn On"
- variant="default"
- onConfirm={() => {
- controlMutation.mutate('on');
- setShowPowerOnConfirm(false);
- }}
- onCancel={() => setShowPowerOnConfirm(false)}
- />
- )}
- {/* Power Off Confirmation */}
- {showPowerOffConfirm && (
- <ConfirmModal
- title="Turn Off Smart Plug"
- message={`Are you sure you want to turn off "${plug.name}"? This will cut power to the connected device.`}
- confirmText="Turn Off"
- variant="danger"
- onConfirm={() => {
- controlMutation.mutate('off');
- setShowPowerOffConfirm(false);
- }}
- onCancel={() => setShowPowerOffConfirm(false)}
- />
- )}
- </>
- );
- }
|