Added navigate funtionalies

This commit is contained in:
Srinath 2026-03-09 18:12:07 +05:30
parent 6172be0f38
commit f47f161370
1 changed files with 43 additions and 60 deletions

View File

@ -32,9 +32,9 @@ const isCapacitor = () => !!window.Capacitor?.isNativePlatform?.();
// Define your home/exit pages here
const HOME_PAGES = [
'/landing-page/home',
'/home/landing-page/home',
'/app-page/home', // android minimize
'/apps/retail/app-page/home',
'/' // android minimize
];
const LOGIN_PAGES = ['signin', 'login', 'auth'];
@ -89,104 +89,87 @@ const AppRoutes = () => {
const handlePopState = () => {
const currentPath = location.pathname;
// On home page just stay, don't redirect
if (HOME_PAGES.some((p) => currentPath.includes(p))) {
window.history.go(1); // stay here
const SessionId = getSession('SessionId');
// 🟢 If session exists allow navigation
if (SessionId) {
console.log("Browser back allowed");
return;
}
const SessionId = getSession('SessionId');
if (!SessionId) {
alert(
'Session invalid and Unauthorized action detected. Redirecting to login...'
);
// 🔴 Only redirect if user is on protected page
if (!LOGIN_PAGES.some(p => currentPath.includes(p))) {
console.log("Session missing → redirecting login");
clearSession();
window.location.replace(`${commonSubDir}`);
} else {
window.history.go(1);
}
};
// Add event listener on mount
window.addEventListener('popstate', handlePopState);
return () => window.removeEventListener('popstate', handlePopState);
}, [location.pathname]);
// Android Capacitor back button
useEffect(() => {
if (!isMobile && !isIOS && !isCapacitor()) return;
if (!isCapacitor()) return;
let backButtonListener;
const setupBackButton = async () => {
try {
backButtonListener = await CapacitorApp.addListener(
'backButton',
async () => {
const handler = async () => {
try {
const currentPath = location.pathname + (location.search || '');
console.log('🔙 Back pressed:', currentPath);
// 🏠 Home page minimize
if (HOME_PAGES?.some((p) => currentPath?.startsWith(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) : [];
// Remove current path from stack if it's at the end
while (stack.length && stack[stack.length - 1] === currentPath) {
stack.pop();
}
if (stack.length) {
// If there are pages in history, go back to previous page
if (stack.length > 0) {
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]));
sessionStorage.setItem('navStack', JSON.stringify(stack));
isBackNav.current = true;
navigate(home);
navigate(previous);
console.log('Navigate to previous:', previous);
return;
}
sessionStorage.setItem('navStack', JSON.stringify(stack));
isBackNav.current = true;
navigate(previous);
} else {
const home = '/landing-page/home';
// Stack is empty - check where we are
// 🏠 Home page minimize
if (HOME_PAGES?.some((p) => currentPath?.includes(p))) {
console.log("Home page & empty stack → minimizing app");
await CapacitorApp.minimizeApp();
return;
}
// 🚪 Login page minimize
if (LOGIN_PAGES?.some((p) => currentPath?.includes(p))) {
console.log("Login page & empty stack → minimizing app");
await CapacitorApp.minimizeApp();
return;
}
// Anywhere else with empty stack go to home
const home = '/app-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');
}
}
);
} catch (err) {
console.error('Listener error:', err);
console.log('Empty stack, navigate to home');
} catch (e) {
console.error('Back handler error:', e);
await CapacitorApp.minimizeApp();
}
};
setupBackButton();
const listener = CapacitorApp.addListener('backButton', handler);
return () => {
try {
backButtonListener?.remove();
} catch (err) {}
listener.remove();
};
}, [navigate, location.pathname, location.search]);
// mohan