"use client" import { useEffect, useState } from "react" import { dashboardApi } from "@/lib/api/dashboard" import { DashboardStats } from "@/types/dashboard" import { useAuth } from "@/components/auth/AuthProvider" /** * Shared cross-domain counts (pending POs, low-stock, pending leave, overdue invoices, ...) * from GET /dashboard/stats — the same aggregate the Dashboard home page renders, reused * here as small KPI tiles on individual list pages. Null while loading or on error, so * callers should render nothing (or a skeleton) until it resolves. */ export function useDashboardStats(): DashboardStats | null { const { activeBranchId, branchingEnabled } = useAuth() const [stats, setStats] = useState(null) useEffect(() => { let cancelled = false const branchId = branchingEnabled ? (activeBranchId ?? undefined) : undefined dashboardApi .stats({ branchId }) .then((s) => { if (!cancelled) setStats(s) }) .catch(() => { if (!cancelled) setStats(null) }) return () => { cancelled = true } }, [activeBranchId, branchingEnabled]) return stats }