import { useSession } from "@xtablo/shared/contexts/SessionContext";
import { useSignUpToStream } from "@xtablo/shared/hooks/auth";
import { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { api } from "../lib/api";

export const OAuthSigninPage = () => {
  const navigate = useNavigate();
  const { session } = useSession();
  const { signUpToStream } = useSignUpToStream(api);
  const redirectUrl = localStorage.getItem("redirectUrl");
  useEffect(() => {
    const interval = setInterval(() => {
      if (session) {
        signUpToStream(session.access_token);
        if (redirectUrl) {
          localStorage.removeItem("redirectUrl");
          navigate(decodeURIComponent(redirectUrl));
        } else {
          navigate("/");
        }
      }
    }, 100);
    return () => clearInterval(interval);
  }, [navigate, session, signUpToStream]);
  return <></>;
};
