import React from 'react';
import type { ReactElement } from 'react';
import Head from 'next/head';
import { useRouter } from 'next/router';
import LayoutGuest from '../layouts/Guest';
import { getPageTitle } from '../config';

export default function HomeRedirect() {
  const router = useRouter();

  React.useEffect(() => {
    const token =
      sessionStorage.getItem('token') || localStorage.getItem('token');

    router.replace(token ? '/projects/projects-list' : '/login');
  }, [router]);

  return (
    <Head>
      <title>{getPageTitle('Redirecting')}</title>
    </Head>
  );
}

HomeRedirect.getLayout = function getLayout(page: ReactElement) {
  return <LayoutGuest>{page}</LayoutGuest>;
};
