import React, { useState, useEffect } from 'react';
import { useNavigate, useHref } from "react-router-dom";
import { queryCurrentUser } from '@/services/user';

export default function useReady({ user, setUser }) {
  const navigate = useNavigate();
  const isLogined = user && user.id;

  useEffect(() => {
    if (!isLogined) {
      queryCurrentUser().then((res) => {
        setUser(res);
      }).catch(err => {
        const { data } = err;
        if (data.code === 1001) {
          navigate('/login?back=true');
        }
      });
    }
  }, [isLogined]);
}
