import { useEffect } from "react";
import { Navigate } from "react-router-dom";
import { getProjects } from "@/lib/api";
import { useState } from "react";

export default function HomePage() {
  const [projects, setProjects] = useState<any[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    getProjects()
      .then(setProjects)
      .catch(() => {})
      .finally(() => setLoading(false));
  }, []);

  if (loading) {
    return (
      <div className="flex h-screen items-center justify-center text-[var(--muted)]">
        Загрузка...
      </div>
    );
  }

  if (projects.length === 0) {
    return <Navigate to="/projects/new" replace />;
  }

  // Redirect to first project
  return <Navigate to={`/projects/${projects[0].slug}`} replace />;
}