refactor(navigation): make mobile deep link navigation completely generic and dynamic (#12)

This commit is contained in:
ShivaniKhaire 2026-10-05 14:37:11 +05:30
parent 3f536986de
commit 537e92cced
3 changed files with 70 additions and 26 deletions

View File

@ -17,6 +17,7 @@ import { AuthModal } from './components/auth/AuthModal';
import { ProfileDrawer } from './components/auth/ProfileDrawer';
import { PaymentSuccessModal } from './components/checkout/PaymentSuccessModal';
import { PasswordUpdatedModal } from './components/auth/PasswordUpdatedModal';
import { openMobileApp, DEFAULT_RETURN_ROUTE } from './utils/navigation';
const getInitialTab = (): 'storefront' | 'subscriptions' => {
if (typeof window !== 'undefined') {
@ -96,6 +97,8 @@ export default function App() {
// Active payment state
const [verificationResult, setVerificationResult] = useState<VerificationResult | null>(null);
const [razorpayKeyId, setRazorpayKeyId] = useState<string>('');
const [returnRoute, setReturnRoute] = useState<string>(DEFAULT_RETURN_ROUTE);
const [appScheme, setAppScheme] = useState<string | undefined>(undefined);
// Handle browser back button navigation
useEffect(() => {
@ -105,10 +108,7 @@ export default function App() {
if (verificationResult && verificationResult.status === 'success') {
const isMobile = /Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
if (isMobile) {
window.location.href = 'acupedia://modules';
setTimeout(() => {
window.location.href = 'myapp://modules';
}, 500);
openMobileApp({ route: returnRoute, schemes: appScheme ? [appScheme] : undefined });
} else {
switchTab('subscriptions');
setVerificationResult(null);
@ -119,7 +119,7 @@ export default function App() {
};
window.addEventListener('popstate', handlePopState);
return () => window.removeEventListener('popstate', handlePopState);
}, [verificationResult]);
}, [verificationResult, returnRoute, appScheme]);
// Profile Update & Drawer State
const [showProfileDrawer, setShowProfileDrawer] = useState<boolean>(false);
@ -171,6 +171,15 @@ export default function App() {
const params = new URLSearchParams(window.location.search);
const urlToken = params.get('token');
const urlPlanCode = params.get('planCode');
const urlReturnRoute = params.get('returnRoute') || params.get('redirectPath') || params.get('redirect');
const urlScheme = params.get('scheme');
if (urlReturnRoute) {
setReturnRoute(urlReturnRoute);
}
if (urlScheme) {
setAppScheme(urlScheme);
}
let activeToken = userToken;
@ -682,10 +691,7 @@ export default function App() {
if (/Android|iPhone|iPad|iPod/i.test(navigator.userAgent)) {
setTimeout(() => {
window.location.href = 'acupedia://modules';
setTimeout(() => {
window.location.href = 'myapp://modules';
}, 500);
openMobileApp({ route: returnRoute, schemes: appScheme ? [appScheme] : undefined });
}, 1500);
}
@ -888,6 +894,8 @@ export default function App() {
loggedInUser={loggedInUser}
onDownloadInvoice={handleDownloadCheckoutInvoice}
downloadingInvoice={downloadingModalInvoice}
returnRoute={returnRoute}
appScheme={appScheme}
/>
)}

View File

@ -1,6 +1,7 @@
import React from 'react';
import { XCircle, CheckCircle, Check, Download, RefreshCw, Smartphone } from 'lucide-react';
import type { VerificationResult, UserProfile } from '../../types';
import { openMobileApp, getAppDeepLinkUrls } from '../../utils/navigation';
interface PaymentSuccessModalProps {
verificationResult: VerificationResult;
@ -8,6 +9,8 @@ interface PaymentSuccessModalProps {
loggedInUser: UserProfile | null;
onDownloadInvoice: (paymentId: string, invoiceUrl?: string) => void;
downloadingInvoice: boolean;
returnRoute?: string;
appScheme?: string;
}
export const PaymentSuccessModal: React.FC<PaymentSuccessModalProps> = ({
@ -16,6 +19,8 @@ export const PaymentSuccessModal: React.FC<PaymentSuccessModalProps> = ({
loggedInUser,
onDownloadInvoice,
downloadingInvoice,
returnRoute,
appScheme,
}) => {
return (
<div className="fixed inset-0 z-[100] flex items-center justify-center p-4 bg-slate-950/75 backdrop-blur-md transition-all duration-300">
@ -176,25 +181,10 @@ export const PaymentSuccessModal: React.FC<PaymentSuccessModalProps> = ({
)}
<a
href="acupedia://modules"
href={getAppDeepLinkUrls({ route: returnRoute, schemes: appScheme ? [appScheme] : undefined })[0]}
onClick={(e) => {
e.preventDefault();
const userAgent = navigator.userAgent || '';
const isAndroid = /Android/i.test(userAgent);
const isIOS = /iPhone|iPad|iPod/i.test(userAgent);
if (isAndroid) {
window.location.href = 'acupedia://modules';
setTimeout(() => {
window.location.href = 'myapp://modules';
}, 500);
} else if (isIOS) {
window.location.href = 'acupedia://modules';
setTimeout(() => {
window.location.href = 'myapp://modules';
}, 500);
} else {
window.location.href = 'acupedia://modules';
}
openMobileApp({ route: returnRoute, schemes: appScheme ? [appScheme] : undefined });
}}
className="w-full h-9 bg-emerald-600 hover:bg-emerald-500 text-white font-bold text-xs rounded-lg transition-all duration-300 flex items-center justify-center gap-2 shadow-md shadow-emerald-600/10 no-underline decoration-0 cursor-pointer"
>

46
src/utils/navigation.ts Normal file
View File

@ -0,0 +1,46 @@
/**
* Generic Navigation & Deep-Linking Utility for Acupedia Storefront
*/
export const DEFAULT_APP_SCHEMES = ['acupedia', 'myapp'];
export const DEFAULT_RETURN_ROUTE = 'modules';
export interface MobileAppLaunchOptions {
route?: string;
schemes?: string[];
delayBetweenFallbacks?: number;
}
/**
* Cleanly format route for custom URI scheme (e.g. "/modules" -> "modules")
*/
export function formatDeepLinkRoute(route?: string): string {
if (!route) return DEFAULT_RETURN_ROUTE;
return route.replace(/^\/+/, '').trim() || DEFAULT_RETURN_ROUTE;
}
/**
* Generate full deep-link URLs for supported schemes
*/
export function getAppDeepLinkUrls(options: MobileAppLaunchOptions = {}): string[] {
const route = formatDeepLinkRoute(options.route);
const schemes = options.schemes && options.schemes.length > 0 ? options.schemes : DEFAULT_APP_SCHEMES;
return schemes.map((scheme) => `${scheme}://${route}`);
}
/**
* Generic function to launch the mobile app with progressive fallback across supported schemes
*/
export function openMobileApp(options: MobileAppLaunchOptions = {}): void {
const urls = getAppDeepLinkUrls(options);
if (urls.length === 0) return;
const [primaryUrl, ...fallbackUrls] = urls;
window.location.href = primaryUrl;
fallbackUrls.forEach((url, idx) => {
setTimeout(() => {
window.location.href = url;
}, (idx + 1) * (options.delayBetweenFallbacks || 500));
});
}