Spaces:
Running
Running
File size: 1,365 Bytes
e4526cf eb7dd3e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import React, { useEffect } from "react";
import { useLocation, useNavigate } from "react-router-dom";
const InitialRedirectHandler: React.FC = () => {
const navigate = useNavigate();
const location = useLocation();
useEffect(() => {
const handleInitialRedirect = async () => {
if (location.pathname === "/") {
try {
const response = await fetch("/_api/", {
method: "GET",
redirect: "follow",
});
if (response.ok) {
console.log("Backend redirect response URL", response.url);
if (response.url && response.url != window.location.href) {
const newPath = new URL(response.url).pathname;
if (location.pathname !== newPath) {
navigate(newPath, { replace: true });
}
} else {
const data = await response.json();
if (data && data.url) {
navigate(data.url, { replace: true });
}
}
} else {
console.error("Failed to get initial chat URL: ", response.status);
}
} catch (error) {
console.error("Error getting chat URL: ", error);
}
}
};
handleInitialRedirect();
}, [location.pathname, navigate]);
return null;
};
export default InitialRedirectHandler;
|