Android_Retail/src/App.jsx

276 lines
8.6 KiB
React
Raw Normal View History

import { useEffect, lazy, Suspense, useRef, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { App as CapacitorApp } from '@capacitor/app';
2026-01-27 18:27:29 +05:30
import { Routes, Route } from 'react-router-dom';
2026-02-10 16:35:19 +05:30
import { useSelector } from 'react-redux';
2026-01-27 18:27:29 +05:30
import ProtectedRoutes from './ProtectedRoutes';
import SelfBooking from './Pages/SelfBooking/SelfBooking.jsx';
2026-02-10 16:35:19 +05:30
import { isMobile, isIOS } from 'react-device-detect';
2026-01-27 18:27:29 +05:30
import { routesConfig } from './routesConfig';
2026-02-10 16:35:19 +05:30
import { GlobalItemCard } from './Features/BookingScreen/BookingData/BookingData.js';
import { clearSession, getSession } from './Services/Others.js';
2026-02-23 12:18:31 +05:30
2026-02-25 16:24:29 +05:30
const KisokSelBooking = lazy(
() => import('./Pages/SelfBooking/KisokSelBooking')
);
const IndividualBooking = lazy(
() => import('./Pages/SelfBooking/IndividualBooking')
);
2026-02-23 12:18:31 +05:30
2026-01-27 18:27:29 +05:30
const commonSubDir = import.meta.env.ENV_COMMON_BASE_URL;
const subDirectory = import.meta.env.ENV_BASE_URL;
2026-02-10 16:35:19 +05:30
import '../ownLib/my-ui-lib.css';
import CardSkeleton from './Components/Skeleton/CardSkeleton.jsx';
import CustomerMenuPage from './Pages/Reports/MenuQRCode/CustomerMenuPage.jsx';
import WeightScaleApp from './Pages/BookingScreen/WeightscaleApp.jsx';
import useSubscriptionManager from './useSubscriptionManager.js';
import useSessionManager from './useSessionManager.js';
import ExtendSubscriptionModal from './Pages/ExtentedModal/ExtendSubscriptionModal.jsx';
import devtools from 'devtools-detect';
2026-02-25 16:24:29 +05:30
const isCapacitor = () => !!window.Capacitor?.isNativePlatform?.();
// ✅ Define your home/exit pages here
const HOME_PAGES = [
'/landing-page/home',
'/home/landing-page/home',
2026-02-25 16:24:29 +05:30
'app-page/home', // android minimize
];
const LOGIN_PAGES = ['signin', 'login', 'auth'];
2026-02-11 12:45:21 +05:30
2026-01-27 18:27:29 +05:30
const AppRoutes = () => {
const ItemCard = useSelector(GlobalItemCard);
2026-02-11 15:27:50 +05:30
const { sessionData, logout } = useSessionManager(ItemCard);
2026-02-25 16:24:29 +05:30
const {
remainingDays,
handleCancel,
freeExtend,
extendModel,
handlePay,
setExtendModel,
} = useSubscriptionManager(sessionData, logout);
const location = useLocation();
const navigate = useNavigate();
const [devToolsOpen, setDevToolsOpen] = useState(false);
// ✅ Flag to prevent stack push when back button navigates
const isBackNav = useRef(false);
2026-02-10 16:35:19 +05:30
// ✅ Build navigation stack
2026-01-27 18:27:29 +05:30
useEffect(() => {
// Skip pushing to stack when back button caused this navigation
if (isBackNav.current) {
isBackNav.current = false;
return;
}
try {
const path = location.pathname + (location.search || '');
// Don't track login pages
2026-02-25 16:24:29 +05:30
if (LOGIN_PAGES.some((p) => path.includes(p))) return;
const raw = sessionStorage.getItem('navStack');
let stack = raw ? JSON.parse(raw) : [];
if (stack.length === 0 || stack[stack.length - 1] !== path) {
stack.push(path);
if (stack.length > 50) stack = stack.slice(-50);
sessionStorage.setItem('navStack', JSON.stringify(stack));
console.log('📍 Stack:', stack);
}
} catch (e) {}
}, [location.pathname, location.search]);
// ✅ Web — block browser back button
useEffect(() => {
if (isCapacitor()) return; // Android handled separately
2026-02-10 16:35:19 +05:30
const handlePopState = () => {
const currentPath = location.pathname;
// On home page — just stay, don't redirect
2026-02-25 16:24:29 +05:30
if (HOME_PAGES.some((p) => currentPath.includes(p))) {
window.history.go(1); // stay here
return;
}
2026-01-27 18:27:29 +05:30
const SessionId = getSession('SessionId');
if (!SessionId) {
alert(
'Session invalid and Unauthorized action detected. Redirecting to login...'
);
clearSession();
window.location.replace(`${commonSubDir}`);
} else {
window.history.go(1);
2026-01-27 18:27:29 +05:30
}
};
// Add event listener on mount
2026-01-27 18:27:29 +05:30
window.addEventListener('popstate', handlePopState);
return () => window.removeEventListener('popstate', handlePopState);
}, [location.pathname]);
2026-01-27 18:27:29 +05:30
// ✅ Android — Capacitor back button
useEffect(() => {
if (!isMobile && !isIOS && !isCapacitor()) return;
2026-02-25 16:24:29 +05:30
let backButtonListener;
const setupBackButton = async () => {
try {
2026-02-25 16:24:29 +05:30
backButtonListener = await CapacitorApp.addListener(
'backButton',
async () => {
try {
const currentPath = location.pathname + (location.search || '');
console.log('🔙 Back pressed:', currentPath);
// 🏠 Home page → minimize
if (HOME_PAGES.some((p) => currentPath.includes(p))) {
if (isCapacitor()) {
await CapacitorApp.minimizeApp();
}
return;
}
// 🚪 Login page → exit
if (LOGIN_PAGES.some((p) => currentPath.includes(p))) {
if (isCapacitor()) {
await CapacitorApp.exitApp();
}
return;
}
const raw = sessionStorage.getItem('navStack');
let stack = raw ? JSON.parse(raw) : [];
while (stack.length && stack[stack.length - 1] === currentPath) {
stack.pop();
}
if (stack.length) {
const previous = stack[stack.length - 1];
if (LOGIN_PAGES.some((p) => previous.includes(p))) {
const home = '/landing-page/home';
sessionStorage.setItem('navStack', JSON.stringify([home]));
isBackNav.current = true;
navigate(home);
return;
}
sessionStorage.setItem('navStack', JSON.stringify(stack));
isBackNav.current = true;
navigate(previous);
} else {
const home = '/landing-page/home';
sessionStorage.setItem('navStack', JSON.stringify([home]));
isBackNav.current = true;
navigate(home);
}
} catch (err) {
console.error('Back handler error:', err);
navigate('/landing-page/home');
}
}
2026-02-25 16:24:29 +05:30
);
} catch (err) {
console.error('Listener error:', err);
}
2026-01-27 18:27:29 +05:30
};
2026-02-25 16:24:29 +05:30
setupBackButton();
2026-01-27 18:27:29 +05:30
return () => {
try {
2026-02-25 16:24:29 +05:30
backButtonListener?.remove();
} catch (err) {}
};
}, [navigate, location.pathname, location.search]);
// mohan
useEffect(() => {
if (isMobile || isIOS) {
console.log("📱 Mobile/iOS device → skipping DevTools detection");
return;
}
const checkDevTools = setInterval(() => {
if (devtools.isOpen && !devToolsOpen) {
setDevToolsOpen(true);
document.body.innerHTML =
"<h1 style='color: red; text-align: center;'>Close Inspect to continue using the application.</h1>";
}
else if (!devtools.isOpen && devToolsOpen) {
setDevToolsOpen(false);
setTimeout(() => {
window.location.reload();
}, 100);
clearInterval(checkDevTools);
}
if (!devtools.isOpen) {
let before = performance.now();
let after = performance.now();
let executionDelay = after - before;
if (executionDelay > 100) {
setDevToolsOpen(true);
document.body.innerHTML =
"<h1 style='color: red; text-align: center;'>Close Inspect to continue using the application.</h1>";
} else {
setDevToolsOpen(false);
}
}
}, 1000);
return () => clearInterval(checkDevTools);
}, [devToolsOpen]);
2026-02-11 12:09:36 +05:30
if (extendModel) {
return (
<ExtendSubscriptionModal
onClose={handleCancel}
2026-02-23 12:18:31 +05:30
onPayNow={handlePay}
onContinueWithCredits={freeExtend}
2026-01-27 18:27:29 +05:30
/>
2026-02-11 12:09:36 +05:30
);
}
return (
2026-02-24 17:19:56 +05:30
<Suspense fallback={<CardSkeleton />}>
2026-02-11 12:09:36 +05:30
<Routes>
<Route
path={`${subDirectory}selfBooking/:SelfBookingId`}
element={<SelfBooking />}
/>
<Route
path={`${subDirectory}MenuQRCode`}
element={<CustomerMenuPage />}
/>
<Route
path={`${subDirectory}weightscaleapp`}
element={<WeightScaleApp />}
/>
2026-02-10 16:35:19 +05:30
<Route
path={`${subDirectory}kioskSelfBooking`}
element={<KisokSelBooking />}
/>
2026-01-27 18:27:29 +05:30
2026-02-11 12:09:36 +05:30
<Route
path={`${subDirectory}kiosk-individual-self-booking`}
element={<IndividualBooking />}
/>
<Route
path="/*"
element={<ProtectedRoutes routesConfig={routesConfig} />}
/>
</Routes>
2026-02-10 16:35:19 +05:30
</Suspense>
2026-01-27 18:27:29 +05:30
);
};
export default AppRoutes;