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 16, 2025
1 parent 56e9eed commit a1afc01
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 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 All @@ -28,14 +30,6 @@ export const DataProxyProvider = React.memo(({ children, options = {} }) => {

const initIframe = async () => {
try {
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')
const result = await client.stackClient.fetchJSON('POST', '/intents', {
data: {
Expand All @@ -61,18 +55,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
)
}
}

if (isFlagshipApp()) {
const initFlagship = async () => {
try {
if (!webviewIntent) {
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 (!flag('cozy.search.enabled')) {
log.log(
'Dataproxy features will be disabled due to missing feature flags'
)
setDataProxyServicesAvailable(false)
return
}

if (isFlagshipApp()) {
initFlagship()
} else {
initIframe()
}
}, [client])
}, [client, webviewIntent])

const onIframeLoaded = () => {
const ifr = document.getElementById('DataProxy')
Expand Down

0 comments on commit a1afc01

Please sign in to comment.