From f9517b07b4675ce552f5ba75747a161dd57a5abc Mon Sep 17 00:00:00 2001 From: evlist Date: Mon, 17 Apr 2023 15:02:46 +0200 Subject: [PATCH] Refactoring intent receivers for web links --- src/components/map/Map.tsx | 9 +------- src/lib/intent-receivers/index.ts | 2 ++ src/lib/intent-receivers/web-links.ts | 32 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 src/lib/intent-receivers/web-links.ts diff --git a/src/components/map/Map.tsx b/src/components/map/Map.tsx index e033588..3cee1d9 100644 --- a/src/components/map/Map.tsx +++ b/src/components/map/Map.tsx @@ -95,14 +95,7 @@ const Map: Component = () => { if (receiveIntent(intent, navigate)) { return; } - if (intent.action === 'android.intent.action.VIEW') { - 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 ( + if ( intent.action === 'android.intent.action.SEND' && !!intent.extras['android.intent.extra.TEXT'] ) { diff --git a/src/lib/intent-receivers/index.ts b/src/lib/intent-receivers/index.ts index b1cb2c3..81d4db5 100644 --- a/src/lib/intent-receivers/index.ts +++ b/src/lib/intent-receivers/index.ts @@ -1,11 +1,13 @@ import { geoLocationReceiver } from './geo-location'; import { gpxReceiver } from './gpx-receiver'; import { invitationReceiver } from './invitation-receiver'; +import { webLinksReceiver } from './web-links'; const receivers = { invitationReceiver, gpxReceiver, geoLocationReceiver, + webLinksReceiver, }; export const receiveIntent = (intent: any, navigate: any) => { diff --git a/src/lib/intent-receivers/web-links.ts b/src/lib/intent-receivers/web-links.ts new file mode 100644 index 0000000..4030fcf --- /dev/null +++ b/src/lib/intent-receivers/web-links.ts @@ -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; +};