SwitchbarPopover.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { useState } from 'react';
  2. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  3. import { Plug, Power, PowerOff, Loader2, Wifi, WifiOff, Zap, Play } 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. const isReachable = status?.reachable ?? false;
  28. const isPending = controlMutation.isPending;
  29. // Check if this is a HA script entity
  30. const isScript = plug.plug_type === 'homeassistant' && plug.ha_entity_id?.startsWith('script.');
  31. const handleConfirm = () => {
  32. if (confirmAction) {
  33. controlMutation.mutate(confirmAction);
  34. setConfirmAction(null);
  35. }
  36. };
  37. return (
  38. <>
  39. <div className="flex items-center justify-between py-2 px-3 hover:bg-bambu-dark-tertiary rounded-lg transition-colors">
  40. <div className="flex items-center gap-2">
  41. <div className={`p-1.5 rounded ${isReachable ? ((isOn || isScript) ? 'bg-bambu-green/20' : 'bg-bambu-dark') : 'bg-red-500/20'}`}>
  42. {isScript ? (
  43. <Play className={`w-4 h-4 ${isReachable ? 'text-bambu-green' : 'text-red-400'}`} />
  44. ) : (
  45. <Plug className={`w-4 h-4 ${isReachable ? (isOn ? 'text-bambu-green' : 'text-bambu-gray') : 'text-red-400'}`} />
  46. )}
  47. </div>
  48. <div>
  49. <div className="flex items-center gap-2">
  50. <p className="text-sm text-white font-medium">{plug.name}</p>
  51. {isScript && (
  52. <span className="px-1.5 py-0.5 bg-blue-500/20 text-blue-400 text-[10px] rounded-full">Script</span>
  53. )}
  54. </div>
  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. ) : isScript ? (
  59. <span className={isReachable ? 'text-status-ok' : 'text-status-error'}>
  60. {isReachable ? 'Ready' : 'Offline'}
  61. </span>
  62. ) : isReachable ? (
  63. <>
  64. <Wifi className="w-3 h-3 text-status-ok" />
  65. <span className={isOn ? 'text-status-ok' : 'text-bambu-gray'}>
  66. {status?.state || 'Unknown'}
  67. </span>
  68. {status?.energy?.power !== null && status?.energy?.power !== undefined && (
  69. <>
  70. <span className="text-bambu-gray mx-1">|</span>
  71. <Zap className="w-3 h-3 text-yellow-400" />
  72. <span className="text-yellow-400">{Math.round(status.energy.power)}W</span>
  73. </>
  74. )}
  75. </>
  76. ) : (
  77. <>
  78. <WifiOff className="w-3 h-3 text-status-error" />
  79. <span className="text-status-error">Offline</span>
  80. </>
  81. )}
  82. </div>
  83. </div>
  84. </div>
  85. <div className="flex gap-1">
  86. {isScript ? (
  87. /* Script: single Run button */
  88. <button
  89. onClick={() => setConfirmAction('on')}
  90. disabled={!isReachable || isPending}
  91. className="p-1.5 rounded transition-colors bg-bambu-green text-white disabled:opacity-50 disabled:cursor-not-allowed"
  92. title="Run Script"
  93. >
  94. {isPending ? <Loader2 className="w-4 h-4 animate-spin" /> : <Play className="w-4 h-4" />}
  95. </button>
  96. ) : (
  97. /* Regular: On/Off buttons */
  98. <>
  99. <button
  100. onClick={() => setConfirmAction('on')}
  101. disabled={!isReachable || isPending}
  102. className={`p-1.5 rounded transition-colors ${
  103. isOn
  104. ? 'bg-bambu-green text-white'
  105. : 'bg-bambu-dark text-bambu-gray hover:text-white'
  106. } disabled:opacity-50 disabled:cursor-not-allowed`}
  107. title="Turn On"
  108. >
  109. {isPending ? <Loader2 className="w-4 h-4 animate-spin" /> : <Power className="w-4 h-4" />}
  110. </button>
  111. <button
  112. onClick={() => setConfirmAction('off')}
  113. disabled={!isReachable || isPending}
  114. className={`p-1.5 rounded transition-colors ${
  115. !isOn && isReachable
  116. ? 'bg-bambu-dark-tertiary text-white'
  117. : 'bg-bambu-dark text-bambu-gray hover:text-white'
  118. } disabled:opacity-50 disabled:cursor-not-allowed`}
  119. title="Turn Off"
  120. >
  121. {isPending ? <Loader2 className="w-4 h-4 animate-spin" /> : <PowerOff className="w-4 h-4" />}
  122. </button>
  123. </>
  124. )}
  125. </div>
  126. </div>
  127. {confirmAction && (
  128. <ConfirmModal
  129. title={isScript && confirmAction === 'on'
  130. ? 'Run Script'
  131. : `Turn ${confirmAction === 'on' ? 'On' : 'Off'} Smart Plug`}
  132. message={isScript && confirmAction === 'on'
  133. ? `Are you sure you want to run the script "${plug.name}"?`
  134. : `Are you sure you want to turn ${confirmAction === 'on' ? 'on' : 'off'} "${plug.name}"?`}
  135. confirmText={isScript && confirmAction === 'on' ? 'Run' : (confirmAction === 'on' ? 'Turn On' : 'Turn Off')}
  136. variant={confirmAction === 'off' ? 'warning' : 'default'}
  137. onConfirm={handleConfirm}
  138. onCancel={() => setConfirmAction(null)}
  139. />
  140. )}
  141. </>
  142. );
  143. }
  144. export function SwitchbarPopover({ onClose }: SwitchbarPopoverProps) {
  145. // Fetch all smart plugs
  146. const { data: plugs, isLoading } = useQuery({
  147. queryKey: ['smart-plugs'],
  148. queryFn: api.getSmartPlugs,
  149. });
  150. // Filter to only show plugs with show_in_switchbar enabled
  151. const switchbarPlugs = plugs?.filter(p => p.show_in_switchbar) || [];
  152. return (
  153. <div
  154. 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"
  155. onMouseLeave={onClose}
  156. >
  157. {/* Header */}
  158. <div className="px-4 py-3 border-b border-bambu-dark-tertiary">
  159. <h3 className="text-sm font-semibold text-white flex items-center gap-2">
  160. <Plug className="w-4 h-4 text-bambu-green" />
  161. Smart Switches
  162. </h3>
  163. </div>
  164. {/* Content */}
  165. <div className="p-2 max-h-80 overflow-y-auto">
  166. {isLoading ? (
  167. <div className="flex items-center justify-center py-8">
  168. <Loader2 className="w-6 h-6 text-bambu-gray animate-spin" />
  169. </div>
  170. ) : switchbarPlugs.length === 0 ? (
  171. <div className="text-center py-6 px-4">
  172. <Plug className="w-8 h-8 text-bambu-gray mx-auto mb-2" />
  173. <p className="text-sm text-bambu-gray">No switches in switchbar</p>
  174. <p className="text-xs text-bambu-gray mt-1">
  175. Enable "Show in Switchbar" in Settings &gt; Smart Plugs
  176. </p>
  177. </div>
  178. ) : (
  179. <div className="space-y-1">
  180. {switchbarPlugs.map(plug => (
  181. <SwitchItem key={plug.id} plug={plug} />
  182. ))}
  183. </div>
  184. )}
  185. </div>
  186. </div>
  187. );
  188. }