Refactoring intent receivers for web links

This commit is contained in:
Eric van der Vlist 2023-04-17 15:02:46 +02:00
parent c3c93139ed
commit f9517b07b4
3 changed files with 35 additions and 8 deletions

View File

@ -95,14 +95,7 @@ const Map: Component = () => {
if (receiveIntent(intent, navigate)) { if (receiveIntent(intent, navigate)) {
return; return;
} }
if (intent.action === 'android.intent.action.VIEW') { if (
const url = new URL(intent.data);
console.log({ caller: 'Intent receiver', intent, url });
if (url.protocol === 'https:' && url.hostname === 'web.dyomedea.app') {
location.hash = url.hash;
location.pathname = url.pathname;
}
} else if (
intent.action === 'android.intent.action.SEND' && intent.action === 'android.intent.action.SEND' &&
!!intent.extras['android.intent.extra.TEXT'] !!intent.extras['android.intent.extra.TEXT']
) { ) {

View File

@ -1,11 +1,13 @@
import { geoLocationReceiver } from './geo-location'; import { geoLocationReceiver } from './geo-location';
import { gpxReceiver } from './gpx-receiver'; import { gpxReceiver } from './gpx-receiver';
import { invitationReceiver } from './invitation-receiver'; import { invitationReceiver } from './invitation-receiver';
import { webLinksReceiver } from './web-links';
const receivers = { const receivers = {
invitationReceiver, invitationReceiver,
gpxReceiver, gpxReceiver,
geoLocationReceiver, geoLocationReceiver,
webLinksReceiver,
}; };
export const receiveIntent = (intent: any, navigate: any) => { export const receiveIntent = (intent: any, navigate: any) => {

View File

@ -0,0 +1,32 @@
const ACCEPTED_ACTIONS = ['android.intent.action.VIEW'];
const ACCEPTED_PROTOCOLS = ['https:'];
const ACCEPTED_HOSTNAMES = ['web.dyomedea.app'];
export const webLinksReceiver = (
intent: any,
url: URL | undefined,
navigate: any
) => {
if (!ACCEPTED_ACTIONS.includes(intent.action)) {
return false;
}
if (!url) {
return false;
}
if (!ACCEPTED_PROTOCOLS.includes(url.protocol)) {
return false;
}
if (!ACCEPTED_HOSTNAMES.includes(url.hostname)) {
return false;
}
location.hash = url.hash;
location.pathname = url.pathname;
return true;
};