Skip to content

Commit

Permalink
feat: Make the DataProxyProvider work in Flagship app
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Ldoppea committed Jan 13, 2025
1 parent 56e9eed commit 42705b3
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions packages/cozy-dataproxy-lib/src/dataproxy/DataProxyProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]')
Expand All @@ -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] =
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 42705b3

Please sign in to comment.