From d5a8118c2aa9f8b4de49657977844356603f3632 Mon Sep 17 00:00:00 2001 From: evlist Date: Mon, 17 Apr 2023 14:45:04 +0200 Subject: [PATCH] Refactoring intent receivers for invitations --- src/components/invitation/Invitation.tsx | 24 ++-------------- src/components/invitation/index.ts | 2 +- src/components/map/Map.tsx | 16 ++--------- src/lib/intent-receivers/index.ts | 2 ++ .../intent-receivers/invitation-receiver.ts | 28 +++++++++++++++++++ 5 files changed, 36 insertions(+), 36 deletions(-) create mode 100644 src/lib/intent-receivers/invitation-receiver.ts diff --git a/src/components/invitation/Invitation.tsx b/src/components/invitation/Invitation.tsx index 35a1e58..55e4fc5 100644 --- a/src/components/invitation/Invitation.tsx +++ b/src/components/invitation/Invitation.tsx @@ -36,27 +36,8 @@ const fetchInvitation = () => { }); }; -export const searchInvitationInIntent = (intent: any) => { - if (intent.action !== 'android.intent.action.VIEW') { - return false; - } - let url; +export const openInvitation = (payload: string) => { try { - url = new URL(intent.data); - } catch (error) { - console.error({ caller: 'searchInvitationInIntent', intent, error }); - return false; - } - if (url.protocol !== 'geo:') { - return false; - } - const q = url.search; - const matches = q.match(/invitation=(.+)/); - if (!matches) { - return false; - } - try { - const payload = matches[1]; const decoded = atob(payload); const url = new URL(decoded); setInvitation({ url }); @@ -64,8 +45,7 @@ export const searchInvitationInIntent = (intent: any) => { } catch { console.warn({ caller: 'Invitation / searchInvitationInIntent', - intent, - matches, + payload, message: 'Query is not a base64 encoded URL', }); } diff --git a/src/components/invitation/index.ts b/src/components/invitation/index.ts index 2be74d0..b7dbbb2 100644 --- a/src/components/invitation/index.ts +++ b/src/components/invitation/index.ts @@ -1 +1 @@ -export { default, searchInvitation, searchInvitationInIntent } from './Invitation'; +export { default, searchInvitation } from './Invitation'; diff --git a/src/components/map/Map.tsx b/src/components/map/Map.tsx index b6e28ca..fda5c90 100644 --- a/src/components/map/Map.tsx +++ b/src/components/map/Map.tsx @@ -1,5 +1,5 @@ import { Component, createEffect, createSignal, onMount } from 'solid-js'; -import { useParams, useNavigate, useLocation } from '@solidjs/router'; +import { useParams, useNavigate } from '@solidjs/router'; import OlMap from 'ol/Map'; import View from 'ol/View'; import TileLayer from 'ol/layer/Tile'; @@ -16,7 +16,7 @@ import DragRotate from 'ol/interaction/DragRotate'; import 'ol/ol.css'; import './Map.css'; -import { Collection, Overlay } from 'ol'; +import { Collection } from 'ol'; import { Point } from 'ol/geom'; import { Style, Icon } from 'ol/style'; import GetLocation, { getCurrentLocation } from '../get-location'; @@ -26,7 +26,6 @@ import GpxImport, { importUrls } from '../import'; import AllGpxes from '../all-gpxes'; import MapTileProvider, { mapTileProviders } from '../map-tile-provider'; import Interaction from 'ol/interaction/Interaction'; -import DoubleClickZoom from 'ol/interaction/DoubleClickZoom'; import DragPan from 'ol/interaction/DragPan'; import PinchRotate from 'ol/interaction/PinchRotate'; import PinchZoom from 'ol/interaction/PinchZoom'; @@ -41,16 +40,10 @@ import dispatch from '../../workers/dispatcher-main'; import { debounce } from 'lodash'; import { AndroidFullScreen } from '@awesome-cordova-plugins/android-full-screen'; -import Account from '../account'; import { Overlays } from '../overlays/Overlays'; import Finder, { findLocation } from '../finder'; -import { ZoomIn } from '@suid/icons-material'; import Note from '../note'; -import Invitation, { - searchInvitation, - searchInvitationInIntent, -} from '../invitation'; -import { returnOrUpdate } from 'ol/extent'; +import Invitation, { searchInvitation } from '../invitation'; import Updater from '../updater'; import SystemDrawer from '../system-drawer'; import { receiveIntent } from '../../lib/intent-receivers'; @@ -99,9 +92,6 @@ const Map: Component = () => { ); window.plugins.intentShim.onIntent(function (intent: any) { console.log({ caller: 'Intent receiver', intent }); - if (searchInvitationInIntent(intent)) { - return; - } if (receiveIntent(intent)) { return; } diff --git a/src/lib/intent-receivers/index.ts b/src/lib/intent-receivers/index.ts index e0f9e20..1b3732e 100644 --- a/src/lib/intent-receivers/index.ts +++ b/src/lib/intent-receivers/index.ts @@ -1,6 +1,8 @@ import { gpxReceiver } from './gpx-receiver'; +import { invitationReceiver } from './invitation-receiver'; const receivers = { + invitationReceiver, gpxReceiver, }; diff --git a/src/lib/intent-receivers/invitation-receiver.ts b/src/lib/intent-receivers/invitation-receiver.ts new file mode 100644 index 0000000..c39e19a --- /dev/null +++ b/src/lib/intent-receivers/invitation-receiver.ts @@ -0,0 +1,28 @@ +import { openInvitation } from '../../components/invitation/Invitation'; + +const ACCEPTED_ACTIONS = ['android.intent.action.VIEW']; + +const ACCEPTED_PROTOCOLS = ['geo:']; + +export const invitationReceiver = (intent: any, url: URL | undefined) => { + if (!ACCEPTED_ACTIONS.includes(intent.action)) { + return false; + } + + if (!url) { + return false; + } + + if (!ACCEPTED_PROTOCOLS.includes(url.protocol)) { + return false; + } + + const q = url.search; + const matches = q.match(/invitation=(.+)/); + if (!matches) { + return false; + } + const payload = matches[1]; + openInvitation(payload); + return true; +};