Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make the DataProxyProvider work in Flagship app #2697

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading