From 4f80f96a8dea03a833fa59337113716f0d5c1417 Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Sun, 28 Jul 2024 21:26:21 +0800 Subject: [PATCH] parseTestPayload --- src/context.ts | 4 ++-- src/provider.tsx | 45 +++++++++++++++++++++++++++++++++++++-------- src/type.ts | 2 +- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/context.ts b/src/context.ts index 6ed834d8..1714d182 100644 --- a/src/context.ts +++ b/src/context.ts @@ -14,7 +14,7 @@ export const defaultContext = { downloadUpdate: asyncNoop, downloadAndInstallApk: asyncNoop, getCurrentVersionInfo: () => Promise.resolve({}), - parseTestPayload: () => Promise.resolve(false), + parseTestQrCode: () => false, currentHash: '', packageVersion: '', }; @@ -32,7 +32,7 @@ export const PushyContext = createContext<{ description?: string; metaInfo?: string; }>; - parseTestPayload: (code: string) => Promise; + parseTestQrCode: (code: string) => boolean; currentHash: string; packageVersion: string; client?: Pushy; diff --git a/src/provider.tsx b/src/provider.tsx index 2a4d03f0..b6359d29 100644 --- a/src/provider.tsx +++ b/src/provider.tsx @@ -263,13 +263,7 @@ export const PushyProvider = ({ }, [checkUpdate, options, dismissError, markSuccess]); const parseTestPayload = useCallback( - async (code: string) => { - let payload: PushyTestPayload; - try { - payload = JSON.parse(code); - } catch { - return false; - } + (payload: PushyTestPayload) => { if (payload && payload.type && payload.type.startsWith('__rnPushy')) { const logger = options.logger || (() => {}); options.logger = ({ type, data }) => { @@ -294,6 +288,41 @@ export const PushyProvider = ({ [checkUpdate, options], ); + const parseTestQrCode = useCallback( + (code: string) => { + let payload: PushyTestPayload; + try { + payload = JSON.parse(code); + return parseTestPayload(payload); + } catch { + return false; + } + }, + [parseTestPayload], + ); + + useEffect(() => { + const parseLinking = (url: string | null) => { + if (!url) { + return; + } + const params = new URLSearchParams(url); + const payload = { + type: params.get('type'), + data: params.get('data'), + }; + parseTestPayload(payload); + }; + + Linking.getInitialURL().then(parseLinking); + const linkingListener = Linking.addEventListener('url', ({ url }) => + parseLinking(url), + ); + return () => { + linkingListener.remove(); + }; + }, [parseTestPayload]); + return ( {children} diff --git a/src/type.ts b/src/type.ts index e6ed64d6..0fe01180 100644 --- a/src/type.ts +++ b/src/type.ts @@ -83,6 +83,6 @@ export interface PushyOptions { } export interface PushyTestPayload { - type: '__rnPushyVersionHash'; + type: '__rnPushyVersionHash' | string | null; data: any; }