"use client"; import { useState, useCallback } from "react"; import { motion } from "framer-motion"; import { Wind } from "lucide-react"; import { toggleFan, setFanPreset } from "@/lib/api"; interface Props { entityId: string; state: string; presetMode?: string; onUpdate: () => void; } const MODES = [ { id: "Auto", label: "Авто", color: "#06b6d4" }, { id: "Night", label: "Ночь", color: "#6366f1" }, { id: "High", label: "Макс", color: "#f43f5e" }, ]; export default function AirPurifierCard({ entityId, state, presetMode, onUpdate, }: Props) { const isOn = state === "on"; const [currentMode, setCurrentMode] = useState(presetMode || "Auto"); const [pending, setPending] = useState(false); const handleToggle = useCallback(async () => { if (pending) return; setPending(true); await toggleFan(entityId, !isOn); onUpdate(); setPending(false); }, [entityId, isOn, pending, onUpdate]); const handleMode = useCallback( async (mode: string) => { setCurrentMode(mode); await setFanPreset(entityId, mode); onUpdate(); }, [entityId, onUpdate] ); const activeColor = MODES.find((m) => m.id === currentMode)?.color || "#06b6d4"; return (
Очиститель воздуха
{isOn ? currentMode : "Выключен"}
{isOn && ( {MODES.map((mode) => ( handleMode(mode.id)} className="flex-1 py-2 rounded-xl text-xs font-medium" style={ currentMode === mode.id ? { background: `${mode.color}25`, border: `1px solid ${mode.color}60`, color: mode.color, } : { background: "rgba(255,255,255,0.06)", border: "1px solid rgba(255,255,255,0.08)", color: "var(--text-secondary)", } } whileTap={{ scale: 0.9 }} > {mode.label} ))} )}
); }