From 42705b3dd71aee1e46b39aab17f937d27f8869a4 Mon Sep 17 00:00:00 2001 From: Ldoppea Date: Mon, 13 Jan 2025 18:13:15 +0100 Subject: [PATCH] feat: Make the DataProxyProvider work in Flagship app In previous implementation we disabled the DataProxy in Flagship app because this feature was not implemented yet in that environment We are working on implementing it in the Flagship app, so we want the DataProxyProvider to be able to use the Flagship's implementation This commit implements this by checking the feature availability on the Flagship app and then by redirecting the DataProxy calls through the webviewIntent --- .../src/dataproxy/DataProxyProvider.jsx | 49 +++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/packages/cozy-dataproxy-lib/src/dataproxy/DataProxyProvider.jsx b/packages/cozy-dataproxy-lib/src/dataproxy/DataProxyProvider.jsx index 2626a57539..5b5f33dccb 100644 --- a/packages/cozy-dataproxy-lib/src/dataproxy/DataProxyProvider.jsx +++ b/packages/cozy-dataproxy-lib/src/dataproxy/DataProxyProvider.jsx @@ -4,6 +4,7 @@ import React, { useContext, useState, useEffect } from 'react' import { useClient } from 'cozy-client' import { isFlagshipApp } from 'cozy-device-helper' import flag from 'cozy-flags' +import { useWebviewIntent } from 'cozy-intent' import Minilog from 'cozy-minilog' const log = Minilog('👷‍♂️ [DataProxyProvider]') @@ -18,6 +19,7 @@ export const useDataProxy = () => { export const DataProxyProvider = React.memo(({ children, options = {} }) => { const client = useClient() + const webviewIntent = useWebviewIntent() const [iframeUrl, setIframeUrl] = useState() const [dataProxy, setDataProxy] = useState() const [dataProxyServicesAvailable, setDataProxyServicesAvailable] = @@ -61,18 +63,59 @@ export const DataProxyProvider = React.memo(({ children, options = {} }) => { } catch (error) { setDataProxyServicesAvailable(false) log.error( - 'Error whild initializing Search intent, dataproxy features will be disabled', + 'Error while initializing Search intent, dataproxy features will be disabled', + error + ) + } + } + + const initFlagship = async () => { + try { + if (!webviewIntent) { + return + } + + if (!flag('cozy.search.enabled')) { + log.log( + 'Dataproxy features will be disabled due to missing feature flags' + ) + setDataProxyServicesAvailable(false) + return + } + + log.log('Initializing DataProxy intent in Flagship app') + const isSearchAvailable = + (await webviewIntent?.call('isAvailable', 'search')) ?? false + + if (!isSearchAvailable) { + log.log( + 'Dataproxy features will be disabled due to feature not supported by Flagship app' + ) + setDataProxyServicesAvailable(false) + return + } + + setDataProxy(() => ({ + search: (search, options) => + webviewIntent?.call('search', search, options) + })) + + setDataProxyServicesAvailable(isSearchAvailable) + } catch (error) { + setDataProxyServicesAvailable(false) + log.error( + `Error while initializing Flagship's Search, dataproxy features will be disabled`, error ) } } if (isFlagshipApp()) { - setDataProxyServicesAvailable(false) + initFlagship() } else { initIframe() } - }, [client]) + }, [client, webviewIntent]) const onIframeLoaded = () => { const ifr = document.getElementById('DataProxy')