From c0347ec83067b8ef2dd53eeb3844bb79b139d887 Mon Sep 17 00:00:00 2001 From: Lorenzo Blasa Date: Thu, 24 Aug 2023 07:25:01 -0700 Subject: [PATCH] Deep-link hooked to our protocol handler Summary: This hooks the PWA deep-link to our existing deep-link url handler. Reviewed By: antonk52 Differential Revision: D48598886 fbshipit-source-id: 8da9b7bc89bebdafc2bd4c0dc0bd7608864e0254 --- desktop/flipper-ui-browser/src/index.tsx | 25 ++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/desktop/flipper-ui-browser/src/index.tsx b/desktop/flipper-ui-browser/src/index.tsx index 9dec878de..11e9837fc 100644 --- a/desktop/flipper-ui-browser/src/index.tsx +++ b/desktop/flipper-ui-browser/src/index.tsx @@ -16,6 +16,9 @@ if (loadingContainer) { loadingContainer.innerText = 'Loading...'; } +let cachedFile: {name: string; data: string} | undefined; +let cachedDeepLinkURL: string | undefined; + async function start() { // @ts-ignore electronRequire = function (path: string) { @@ -49,12 +52,10 @@ async function start() { const maybeParams = removePrefix(url.pathname, '/'); const params = new URLSearchParams(maybeParams); - const queryParamsObject: any = {}; - params.forEach((value, key) => { - queryParamsObject[key] = value; - }); + const deeplinkURL = new URL('flipper://open-plugin'); + deeplinkURL.search = params.toString(); - console.log(JSON.stringify(queryParamsObject)); + cachedDeepLinkURL = deeplinkURL.toString(); } const searchParams = new URLSearchParams({token: token ?? ''}); @@ -113,7 +114,6 @@ start().catch((e) => { async function initializePWA() { console.log('[PWA] Initialization'); - let cachedFile: {name: string; data: string} | undefined; let rehydrated = false; const openFileIfAny = () => { if (!cachedFile || !rehydrated) { @@ -127,6 +127,18 @@ async function initializePWA() { cachedFile = undefined; }; + const openURLIfAny = () => { + if (!cachedDeepLinkURL || !rehydrated) { + return; + } + window.dispatchEvent( + new CustomEvent('flipper-protocol-handler', { + detail: [cachedDeepLinkURL], + }), + ); + cachedDeepLinkURL = undefined; + }; + if ('serviceWorker' in navigator) { navigator.serviceWorker .register('/service-worker.js') @@ -177,6 +189,7 @@ async function initializePWA() { console.info('[PWA] Store is rehydrated'); rehydrated = true; openFileIfAny(); + openURLIfAny(); }); }