"use client"; import { useState, useEffect, useCallback } from "react"; import { HAStates } from "@/lib/ha"; export function useHA(refreshInterval = 10000) { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const refresh = useCallback(async () => { try { const res = await fetch("/api/ha", { cache: "no-store" }); const json = await res.json(); setData(json); } catch (e) { console.error("HA fetch failed", e); } finally { setLoading(false); } }, []); useEffect(() => { refresh(); const id = setInterval(refresh, refreshInterval); return () => clearInterval(id); }, [refresh, refreshInterval]); return { data, loading, refresh }; } export function useWeather() { const [weather, setWeather] = useState(null); useEffect(() => { fetch("/api/weather") .then((r) => r.json()) .then(setWeather) .catch(() => {}); }, []); return weather; } export function useTasks() { const [tasks, setTasks] = useState([]); const [demo, setDemo] = useState(false); const refresh = useCallback(async () => { try { const res = await fetch("/api/tasks", { cache: "no-store" }); const json = await res.json(); setTasks(json.tasks || []); setDemo(!!json.demo); } catch (e) {} }, []); useEffect(() => { refresh(); }, [refresh]); return { tasks, setTasks, demo, refresh }; } export function useSavings() { const [savings, setSavings] = useState([]); const [demo, setDemo] = useState(false); useEffect(() => { fetch("/api/savings") .then((r) => r.json()) .then((d) => { setSavings(d.savings || []); setDemo(!!d.demo); }) .catch(() => {}); }, []); return { savings, demo }; }