From 537e92cced5a2186de7e0cae442b034826694f51 Mon Sep 17 00:00:00 2001 From: ShivaniKhaire Date: Mon, 5 Oct 2026 14:37:11 +0530 Subject: [PATCH] refactor(navigation): make mobile deep link navigation completely generic and dynamic (#12) --- src/App.tsx | 26 +++++++---- .../checkout/PaymentSuccessModal.tsx | 24 +++------- src/utils/navigation.ts | 46 +++++++++++++++++++ 3 files changed, 70 insertions(+), 26 deletions(-) create mode 100644 src/utils/navigation.ts diff --git a/src/App.tsx b/src/App.tsx index a3c2ef6..af1ee19 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -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(null); const [razorpayKeyId, setRazorpayKeyId] = useState(''); + const [returnRoute, setReturnRoute] = useState(DEFAULT_RETURN_ROUTE); + const [appScheme, setAppScheme] = useState(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(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} /> )} diff --git a/src/components/checkout/PaymentSuccessModal.tsx b/src/components/checkout/PaymentSuccessModal.tsx index 5684b44..c371457 100644 --- a/src/components/checkout/PaymentSuccessModal.tsx +++ b/src/components/checkout/PaymentSuccessModal.tsx @@ -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 = ({ @@ -16,6 +19,8 @@ export const PaymentSuccessModal: React.FC = ({ loggedInUser, onDownloadInvoice, downloadingInvoice, + returnRoute, + appScheme, }) => { return (
@@ -176,25 +181,10 @@ export const PaymentSuccessModal: React.FC = ({ )} { 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" > diff --git a/src/utils/navigation.ts b/src/utils/navigation.ts new file mode 100644 index 0000000..09cb581 --- /dev/null +++ b/src/utils/navigation.ts @@ -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)); + }); +}