import { FC } from 'react';
import { useLocation } from 'react-router-dom';
import { Landing } from 'screens/landing/Landing.web';
import { useReactiveVar } from '@apollo/client';
import { isLoggedInVar } from 'state-management/session';
import { setPreRequestedRoute } from 'utils/auth';
import { useSession } from 'hooks';

type AuthProps = {
  children: any;
};

export const Auth: FC<AuthProps> = ({ children }) => {
  const isLoggedIn = useReactiveVar(isLoggedInVar);
  const { pathname } = useLocation();
  const { checkSession } = useSession();
  checkSession();
  setPreRequestedRoute(pathname);

  if (!isLoggedIn) {
    return <Landing />;
  }

  return <>{children}</>;
};
