SwitchbarPopover.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { useState } from 'react';
  2. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  3. import { Plug, Power, PowerOff, Loader2, Wifi, WifiOff, Zap, Radio, Eye } from 'lucide-react';
  4. import { api } from '../api/client';
  5. import type { SmartPlug } from '../api/client';
  6. import { ConfirmModal } from './ConfirmModal';
  7. interface SwitchbarPopoverProps {
  8. onClose: () => void;
  9. }
  10. function SwitchItem({ plug }: { plug: SmartPlug }) {
  11. const queryClient = useQueryClient();
  12. const [confirmAction, setConfirmAction] = useState<'on' | 'off' | null>(null);
  13. // Fetch current status
  14. const { data: status, isLoading: statusLoading } = useQuery({
  15. queryKey: ['smart-plug-status', plug.id],
  16. queryFn: () => api.getSmartPlugStatus(plug.id),
  17. refetchInterval: 10000, // Refresh every 10 seconds when popover is open
  18. });
  19. // Control mutation
  20. const controlMutation = useMutation({
  21. mutationFn: (action: 'on' | 'off') => api.controlSmartPlug(plug.id, action),
  22. onSuccess: () => {
  23. queryClient.invalidateQueries({ queryKey: ['smart-plug-status', plug.id] });
  24. },
  25. });
  26. const isOn = status?.state === 'ON';
  27. // For MQTT plugs, consider reachable if we have power data
  28. const hasMqttData = plug.plug_type === 'mqtt' && (status?.energy?.power !== null && status?.energy?.power !== undefined);
  29. const isReachable = (status?.reachable ?? false) || hasMqttData;
  30. const isPending = controlMutation.isPending;
  31. const isMqtt = plug.plug_type === 'mqtt';
  32. const handleConfirm = () => {
  33. if (confirmAction) {
  34. controlMutation.mutate(confirmAction);
  35. setConfirmAction(null);
  36. }
  37. };
  38. return (
  39. <>
  40. <div className="flex items-center justify-between py-2 px-3 hover:bg-bambu-dark-tertiary rounded-lg transition-colors">
  41. <div className="flex items-center gap-2">
  42. <div className={`p-1.5 rounded ${
  43. isMqtt
  44. ? (isReachable ? 'bg-teal-500/20' : 'bg-red-500/20')
  45. : (isReachable ? (isOn ? 'bg-bambu-green/20' : 'bg-bambu-dark') : 'bg-red-500/20')
  46. }`}>
  47. {isMqtt ? (
  48. <Radio className={`w-4 h-4 ${isReachable ? 'text-teal-400' : 'text-red-400'}`} />
  49. ) : (
  50. <Plug className={`w-4 h-4 ${isReachable ? (isOn ? 'text-bambu-green' : 'text-bambu-gray') : 'text-red-400'}`} />
  51. )}
  52. </div>
  53. <div>
  54. <p className="text-sm text-white font-medium">{plug.name}</p>
  55. <div className="flex items-center gap-1 text-xs">
  56. {statusLoading ? (
  57. <Loader2 className="w-3 h-3 text-bambu-gray animate-spin" />
  58. ) : isMqtt ? (
  59. /* MQTT plugs show power and monitor-only indicator */
  60. isReachable ? (
  61. <>
  62. <Zap className="w-3 h-3 text-teal-400" />
  63. <span className="text-teal-400">{Math.round(status?.energy?.power ?? 0)}W</span>
  64. <span className="text-bambu-gray mx-1">|</span>
  65. <Eye className="w-3 h-3 text-bambu-gray" />
  66. <span className="text-bambu-gray">Monitor</span>
  67. </>
  68. ) : (
  69. <>
  70. <WifiOff className="w-3 h-3 text-status-error" />
  71. <span className="text-status-error">Waiting</span>
  72. </>
  73. )
  74. ) : isReachable ? (
  75. <>
  76. <Wifi className="w-3 h-3 text-status-ok" />
  77. <span className={isOn ? 'text-status-ok' : 'text-bambu-gray'}>{status?.state || 'Unknown'}</span>
  78. {status?.energy?.power !== null && status?.energy?.power !== undefined && (
  79. <>
  80. <span className="text-bambu-gray mx-1">|</span>
  81. <Zap className="w-3 h-3 text-yellow-400" />
  82. <span className="text-yellow-400">{Math.round(status.energy.power)}W</span>
  83. </>
  84. )}
  85. </>
  86. ) : (
  87. <>
  88. <WifiOff className="w-3 h-3 text-status-error" />
  89. <span className="text-status-error">Offline</span>
  90. </>
  91. )}
  92. </div>
  93. </div>
  94. </div>
  95. {/* Hide on/off buttons for MQTT plugs (monitor-only) */}
  96. {!isMqtt && (
  97. <div className="flex gap-1">
  98. <button
  99. onClick={() => setConfirmAction('on')}
  100. disabled={!isReachable || isPending}
  101. className={`p-1.5 rounded transition-colors ${
  102. isOn
  103. ? 'bg-bambu-green text-white'
  104. : 'bg-bambu-dark text-bambu-gray hover:text-white'
  105. } disabled:opacity-50 disabled:cursor-not-allowed`}
  106. title="Turn On"
  107. >
  108. {isPending ? <Loader2 className="w-4 h-4 animate-spin" /> : <Power className="w-4 h-4" />}
  109. </button>
  110. <button
  111. onClick={() => setConfirmAction('off')}
  112. disabled={!isReachable || isPending}
  113. className={`p-1.5 rounded transition-colors ${
  114. !isOn && isReachable
  115. ? 'bg-bambu-dark-tertiary text-white'
  116. : 'bg-bambu-dark text-bambu-gray hover:text-white'
  117. } disabled:opacity-50 disabled:cursor-not-allowed`}
  118. title="Turn Off"
  119. >
  120. {isPending ? <Loader2 className="w-4 h-4 animate-spin" /> : <PowerOff className="w-4 h-4" />}
  121. </button>
  122. </div>
  123. )}
  124. </div>
  125. {confirmAction && (
  126. <ConfirmModal
  127. title={`Turn ${confirmAction === 'on' ? 'On' : 'Off'} Smart Plug`}
  128. message={`Are you sure you want to turn ${confirmAction === 'on' ? 'on' : 'off'} "${plug.name}"?`}
  129. confirmText={confirmAction === 'on' ? 'Turn On' : 'Turn Off'}
  130. variant={confirmAction === 'off' ? 'warning' : 'default'}
  131. onConfirm={handleConfirm}
  132. onCancel={() => setConfirmAction(null)}
  133. />
  134. )}
  135. </>
  136. );
  137. }
  138. export function SwitchbarPopover({ onClose }: SwitchbarPopoverProps) {
  139. // Fetch all smart plugs
  140. const { data: plugs, isLoading } = useQuery({
  141. queryKey: ['smart-plugs'],
  142. queryFn: api.getSmartPlugs,
  143. });
  144. // Filter to only show plugs with show_in_switchbar enabled
  145. const switchbarPlugs = plugs?.filter(p => p.show_in_switchbar) || [];
  146. return (
  147. <div
  148. className="absolute bottom-full left-0 mb-2 w-72 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-xl z-50"
  149. onMouseLeave={onClose}
  150. >
  151. {/* Header */}
  152. <div className="px-4 py-3 border-b border-bambu-dark-tertiary">
  153. <h3 className="text-sm font-semibold text-white flex items-center gap-2">
  154. <Plug className="w-4 h-4 text-bambu-green" />
  155. Smart Switches
  156. </h3>
  157. </div>
  158. {/* Content */}
  159. <div className="p-2 max-h-80 overflow-y-auto">
  160. {isLoading ? (
  161. <div className="flex items-center justify-center py-8">
  162. <Loader2 className="w-6 h-6 text-bambu-gray animate-spin" />
  163. </div>
  164. ) : switchbarPlugs.length === 0 ? (
  165. <div className="text-center py-6 px-4">
  166. <Plug className="w-8 h-8 text-bambu-gray mx-auto mb-2" />
  167. <p className="text-sm text-bambu-gray">No switches in switchbar</p>
  168. <p className="text-xs text-bambu-gray mt-1">
  169. Enable "Show in Switchbar" in Settings &gt; Smart Plugs
  170. </p>
  171. </div>
  172. ) : (
  173. <div className="space-y-1">
  174. {switchbarPlugs.map(plug => (
  175. <SwitchItem key={plug.id} plug={plug} />
  176. ))}
  177. </div>
  178. )}
  179. </div>
  180. </div>
  181. );
  182. }