Browse Source

feat: enhance redirect logic in Intro component with ref and listener for improved flow

master
sina_sajjadi 2 months ago
parent
commit
e266a1dfdd
  1. 23
      src/app/intro/page.tsx

23
src/app/intro/page.tsx

@ -1,7 +1,7 @@
"use client";
import Image from "next/image";
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { useRouter } from "next/navigation";
import Button from "@/components/ui/button";
import NavigationButton from "@/components/ui/navigation-button";
@ -47,11 +47,16 @@ export default function Intro() {
const [isReportSheetOpen, setIsReportSheetOpen] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const [isCheckingRedirect, setIsCheckingRedirect] = useState(true);
const isRedirectingRef = useRef(false);
useEffect(() => {
let isCancelled = false;
const redirectIfNeeded = async () => {
if (isRedirectingRef.current) {
return;
}
const shouldRedirect =
authBridge.isAuthenticated() &&
window.sessionStorage.getItem(REDIRECT_SESSION_KEY) === "true";
@ -63,19 +68,35 @@ export default function Intro() {
return;
}
isRedirectingRef.current = true;
if (!isCancelled) {
setIsCheckingRedirect(true);
}
const profileResponse = profile ?? (await refetch()).data;
const nextPath = localizePath(getSubmitPath(profileResponse), locale);
window.sessionStorage.removeItem(REDIRECT_SESSION_KEY);
if (!isCancelled) {
router.replace(nextPath);
return;
}
isRedirectingRef.current = false;
};
void redirectIfNeeded();
const unsubscribe = window.addFlutterResponseListener?.((event) => {
if (event.action === "login" && event.success) {
void redirectIfNeeded();
}
});
return () => {
isCancelled = true;
unsubscribe?.();
};
}, [locale, profile, refetch, router]);

Loading…
Cancel
Save