47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
/**
|
|
* 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));
|
|
});
|
|
}
|