"use client";

import { useEffect, useState } from "react";
import { getSystemInfo, type SystemInfo } from "@/lib/api";

export function useSystemInfo() {
  const [systemInfo, setSystemInfo] = useState<SystemInfo | null>(null);
  const [systemInfoLoading, setSystemInfoLoading] = useState(true);

  useEffect(() => {
    let cancelled = false;
    setSystemInfoLoading(true);
    getSystemInfo()
      .then((info) => {
        if (!cancelled) setSystemInfo(info);
      })
      .catch(() => {
        if (!cancelled) setSystemInfo(null);
      })
      .finally(() => {
        if (!cancelled) setSystemInfoLoading(false);
      });
    return () => {
      cancelled = true;
    };
  }, []);

  return { systemInfo, systemInfoLoading };
}
