Spaces:
Sleeping
Sleeping
File size: 1,471 Bytes
bea55e2 | 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 46 47 48 49 50 51 52 | 'use client';
import { useEffect } from 'react';
import { useRouter, usePathname } from 'next/navigation';
/**
* NativeBackGesture — handles Android back navigation.
*
* iOS: Native swipe-back is enabled via allowsBackForwardNavigationGestures
* in the WKWebView (set during the GitHub Actions iOS build).
*
* Android: Listens for @capacitor/app 'backButton' event.
*/
export function NativeBackGesture({ children }: { children: React.ReactNode }) {
const router = useRouter();
const pathname = usePathname();
useEffect(() => {
if (typeof window === 'undefined') return;
let backListener: { remove: () => void } | null = null;
const setup = async () => {
try {
// Dynamic imports — won't crash on web where these don't exist
const { Capacitor } = await import('@capacitor/core');
if (!Capacitor.isNativePlatform()) return;
const { App: CapacitorApp } = await import('@capacitor/app');
backListener = await CapacitorApp.addListener('backButton', () => {
const exitRoutes = ['/', '/home', '/login'];
if (exitRoutes.includes(pathname)) {
CapacitorApp.exitApp();
} else {
router.back();
}
});
} catch {
// Not in Capacitor environment — do nothing
}
};
setup();
return () => {
if (backListener) backListener.remove();
};
}, [router, pathname]);
return <>{children}</>;
}
|