fix: DeviceCard sync initialState from HA on first load
All checks were successful
Deploy to Coolify / deploy (push) Successful in 3s

This commit is contained in:
Cosmo
2026-04-22 11:23:13 +00:00
parent 311ae1dc4b
commit a6cff75c68

View File

@@ -1,6 +1,6 @@
'use client' 'use client'
import { useState } from 'react' import { useState, useEffect } from 'react'
interface DeviceCardProps { interface DeviceCardProps {
id: string id: string
@@ -14,21 +14,23 @@ interface DeviceCardProps {
} }
export default function DeviceCard({ export default function DeviceCard({
id, id, name, icon, entityId, domain, initialState, isMock = false, extraInfo,
name,
icon,
entityId,
domain,
initialState,
isMock = false,
extraInfo,
}: DeviceCardProps) { }: DeviceCardProps) {
const [isOn, setIsOn] = useState(initialState) const [isOn, setIsOn] = useState(initialState)
const [synced, setSynced] = useState(false)
const [loading, setLoading] = useState(false) const [loading, setLoading] = useState(false)
// Sync from HA only once — when real data first arrives (not mock default)
useEffect(() => {
if (!synced && !isMock) {
setIsOn(initialState)
setSynced(true)
}
}, [initialState, isMock, synced])
const toggle = async () => { const toggle = async () => {
const next = !isOn const next = !isOn
setIsOn(next) // optimistic update setIsOn(next) // optimistic
if (!isMock && entityId && domain) { if (!isMock && entityId && domain) {
setLoading(true) setLoading(true)
@@ -43,7 +45,6 @@ export default function DeviceCard({
}), }),
}) })
} catch { } catch {
// revert on error
setIsOn(!next) setIsOn(!next)
} finally { } finally {
setLoading(false) setLoading(false)
@@ -51,87 +52,66 @@ export default function DeviceCard({
} }
} }
const accent = '#00d4ff'
return ( return (
<div <div style={{
style={{ background: isOn ? 'rgba(0,212,255,0.07)' : 'rgba(255,255,255,0.04)',
background: isOn ? 'rgba(0,212,255,0.06)' : 'var(--card-bg)', border: isOn ? '1px solid rgba(0,212,255,0.25)' : '1px solid rgba(255,255,255,0.08)',
border: isOn ? '1px solid rgba(0,212,255,0.2)' : '1px solid var(--card-border)',
borderRadius: 18, borderRadius: 18,
padding: '16px', padding: '18px 16px 16px',
minHeight: 140, minHeight: 140,
display: 'flex', display: 'flex',
flexDirection: 'column', flexDirection: 'column',
justifyContent: 'space-between', justifyContent: 'space-between',
transition: 'all 0.25s ease', transition: 'background 0.25s ease, border-color 0.25s ease',
}} boxShadow: isOn ? '0 0 24px rgba(0,212,255,0.06)' : 'none',
> }}>
{/* Top row: icon + toggle */} {/* Top: icon + toggle */}
<div style={{ display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between' }}> <div style={{ display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between' }}>
<div <div style={{
style={{ width: 42, height: 42, borderRadius: 12,
width: 44,
height: 44,
borderRadius: 12,
background: isOn ? 'rgba(0,212,255,0.15)' : 'rgba(255,255,255,0.06)', background: isOn ? 'rgba(0,212,255,0.15)' : 'rgba(255,255,255,0.06)',
display: 'flex', display: 'flex', alignItems: 'center', justifyContent: 'center',
alignItems: 'center', fontSize: 20,
justifyContent: 'center', transition: 'background 0.25s ease',
fontSize: 22, boxShadow: isOn ? '0 0 16px rgba(0,212,255,0.25)' : 'none',
transition: 'all 0.25s ease', }}>
}}
>
{icon} {icon}
</div> </div>
{/* Toggle button */}
<button <button
onClick={toggle} onClick={toggle}
disabled={loading} disabled={loading}
style={{ style={{
width: 50, width: 52, height: 30, borderRadius: 15,
height: 28, background: isOn ? `linear-gradient(90deg, #00b4d8, #00d4ff)` : 'rgba(255,255,255,0.1)',
borderRadius: 14, position: 'relative', border: 'none', cursor: 'pointer',
background: isOn flexShrink: 0, touchAction: 'manipulation',
? 'linear-gradient(90deg, #00b4d8, #00d4ff)'
: 'rgba(255,255,255,0.1)',
position: 'relative',
transition: 'background 0.3s ease',
flexShrink: 0,
touchAction: 'manipulation',
WebkitTapHighlightColor: 'transparent', WebkitTapHighlightColor: 'transparent',
transition: 'background 0.25s ease',
opacity: loading ? 0.6 : 1, opacity: loading ? 0.6 : 1,
boxShadow: isOn ? '0 0 10px rgba(0,212,255,0.4)' : 'none',
}} }}
> >
<span <span style={{
style={{ position: 'absolute', top: 4,
position: 'absolute', left: isOn ? 26 : 4,
top: 3, width: 22, height: 22, borderRadius: '50%',
left: isOn ? 25 : 3,
width: 22,
height: 22,
borderRadius: '50%',
background: '#fff', background: '#fff',
boxShadow: '0 1px 4px rgba(0,0,0,0.3)', boxShadow: '0 1px 4px rgba(0,0,0,0.3)',
transition: 'left 0.25s ease', transition: 'left 0.22s ease',
}} display: 'block',
/> }} />
</button> </button>
</div> </div>
{/* Bottom: name + status */} {/* Bottom: name + status */}
<div> <div>
<div <div style={{ fontSize: 14, fontWeight: 600, color: 'var(--text-primary)', lineHeight: 1.3 }}>
style={{
fontSize: 14,
fontWeight: 600,
color: 'var(--text-primary)',
marginBottom: 3,
lineHeight: 1.2,
}}
>
{name} {name}
</div> </div>
<div style={{ fontSize: 12, color: isOn ? 'var(--accent)' : 'var(--text-secondary)' }}> <div style={{ fontSize: 12, color: isOn ? accent : 'var(--text-secondary)', marginTop: 3 }}>
{isOn ? 'Включён' : 'Выключен'} {isOn ? 'Включён' : 'Выключен'}
{extraInfo && isOn ? ` · ${extraInfo}` : ''} {extraInfo && isOn ? ` · ${extraInfo}` : ''}
</div> </div>