import { Navigate } from "react-router";
import Error from "~/components/error";
import HomePage from "~/components/home";
import Loader from "~/components/loader";
import { AUTH_ERROR, useUser } from "~/lib/api";

export default function Home() {
  const { user, isLoading, error } = useUser();

  if (isLoading) {
    return <Loader />;
  }
  if (error?.message === AUTH_ERROR) {
    return <Navigate to="/login" replace />;
  }
  if (error) {
    return <Error message={error.message} />;
  }

  return <HomePage user={user} />;
}
