// Hides the react root element. // If fade is true, it will fade out the root element before removing it completely. // If fade is false, it will remove the root element immediately without any animations. function removeReactRoot(fade = true) { const root = document.getElementById("root"); if (!root) { console.warn("No Root element found."); return; } if (fade) { root.style.transition = "opacity 0.3s ease"; root.style.opacity = 0; setTimeout(() => root.style.display = "none", 300); } else { root.style.display = "none"; } } // Show Flutter immediately with standard fluter_bootstrap.js. // This is how flutter boots up for a pure flutter web app. // We use this to skip the React landing page and boot Flutter directly. // This is useful for apps that don't need a React landing page. // e.g. logged in, or routing to a page under Flutter control like /login, or /pricing, etc. function bootFlutterNow() { console.log("Booting Flutter immediately..."); // 1. First thing we do is remove the React root element so it doesn't render and overlay Flutter's canvas. removeReactRoot(false); // 2. Inject flutter_bootstrap.js script to load Flutter. // Note that we don't need to show flutter container here as flutter_bootstrap.js will show it automatically, // rendering flutter-container div useless. const script = document.createElement("script"); // Use new URL with document.baseURI to respect tag for subdirectory deployments script.src = new URL("flutter_bootstrap.js", document.baseURI).href; script.defer = true; document.body.appendChild(script); } /// Starts loading Flutter in the background, when the landing page is shown. /// Adds a click handler to the "Get Started" button to show Flutter when clicked. function backgroundLoadFlutter() { console.log("Starting background load of Flutter..."); // Skip if already initialized if (window.__flutterLoaderInit) return; window.__flutterLoaderInit = true; // 1. Preload Flutter on page load. window.addEventListener("load", () => { console.log("Loading Flutter engine..."); _flutter.loader.loadEntrypoint({ // Use new URL with document.baseURI to respect tag for subdirectory deployments entrypointUrl: new URL("main.dart.js", document.baseURI).href, onEntrypointLoaded: async (initializer) => { console.log("Flutter entrypoint loaded, initializing engine..."); const appRunner = await initializer.initializeEngine({ hostElement: document.getElementById("flutter_host"), }); // Expose to window object for React bridge window.__flutterAppRunner = appRunner; window.__flutterAppLoaded = true; console.log("Flutter engine initialized successfully"); console.log("✅ Set window.__flutterAppRunner:", !!window.__flutterAppRunner); console.log("✅ Set window.__flutterAppLoaded:", window.__flutterAppLoaded); } }); }); // Normal click handler for Get Started async function onClickHandler() { if (window.__flutterAppLoaded) { // Show the Flutter container const f = document.getElementById("flutter-container"); f.style.display = "block"; // Remove react root element to prevent React from rendering over Flutter. removeReactRoot(); // Start Flutter app if not already running if (!window.__flutterAppRunning) { await window.__flutterAppRunner.runApp(); window.__flutterAppRunning = true; } } else { console.warn("Flutter app not loaded yet, waiting..."); setTimeout(onClickHandler, 100); } } // Note: Button click handlers are managed by React via switchToFlutterApp() // No need to attach button listeners here }; function isUserLoggedIn() { // ✅ Check if user is already logged in via localStorage. // If user data is found, skip the landing page and load Flutter directly. // Load landing page otherwise. const userData = localStorage.getItem("user"); if (!userData) return false; try { let parsedUser = JSON.parse(userData); if (typeof parsedUser === "string") { parsedUser = JSON.parse(parsedUser); // double parse if needed } return parsedUser && parsedUser.uid; } catch (err) { console.warn("Error parsing localStorage user", err); return false; } } // List of routes that React should handle const reactRoutes = ["/about", "/contact", "/terms", "/privacy", "/view"]; // ✅ If current route is not root or any of the above React routes, let flutter render immediately. if (window.location.pathname == "/") { if (isUserLoggedIn()) { console.log("User is logged in, booting Flutter immediately..."); bootFlutterNow(); } else { console.log("User is not logged in, showing landing page..."); backgroundLoadFlutter(); } // The route is root or one of the React routes, so we show the landing page. } else if (!reactRoutes.includes(window.location.pathname)) { // Non-root path detected, but not a React route. So it is likely a Flutter route. console.log(`Non-root path detected (${window.location.pathname}) → skipping landing page`); bootFlutterNow(); } else { console.log("Showing landing page for React routes..."); backgroundLoadFlutter(); }