1- import React , { createContext } from 'react'
1+ import React , { createContext , useEffect , useState } from 'react'
22import { Provider as ReduxProvider } from 'react-redux'
33import memoize from 'lodash/memoize'
44
55import flag from 'cozy-flags'
6- import CozyClient , { CozyProvider , RealTimeQueries } from 'cozy-client'
6+ import CozyClient , {
7+ CozyProvider ,
8+ RealTimeQueries ,
9+ FlagshipLink
10+ } from 'cozy-client'
711import CozyDevtools from 'cozy-client/dist/devtools'
12+ import { useWebviewIntent } from 'cozy-intent'
813import I18n from 'cozy-ui/transpiled/react/providers/I18n'
914import CozyTheme from 'cozy-ui/transpiled/react/providers/CozyTheme'
1015import { BreakpointsProvider } from 'cozy-ui/transpiled/react/providers/Breakpoints'
@@ -14,7 +19,7 @@ import { useCozyTheme } from 'cozy-ui/transpiled/react/providers/CozyTheme'
1419
1520import configureStore from 'store/configureStore'
1621import { RealtimePlugin } from 'cozy-realtime'
17- // import { isFlagshipApp } from 'cozy-device-helper'
22+ import { isFlagshipApp , isFlagshipOfflineSupported } from 'cozy-device-helper'
1823
1924import { useWallpaperContext } from 'hooks/useWallpaperContext'
2025
@@ -31,12 +36,14 @@ export const AppContext = createContext()
3136 *
3237 * Is memoized to avoid several clients in case of hot-reload
3338 */
34- export const setupAppContext = memoize ( ( ) => {
39+ export const setupAppContext = memoize ( intent => {
3540 const lang = document . documentElement . getAttribute ( 'lang' ) || 'en'
3641 const context = window . context || 'cozy'
3742 const root = document . querySelector ( '[role=application]' )
3843 const data = root . dataset
3944
45+ const shouldUseFlagshipLink = isFlagshipApp ( ) && isFlagshipOfflineSupported ( )
46+
4047 // New improvements must be done with CozyClient
4148 const cozyClient = new CozyClient ( {
4249 uri : `${ window . location . protocol } //${ data . cozyDomain } ` ,
@@ -47,7 +54,10 @@ export const setupAppContext = memoize(() => {
4754 'home.store.persist'
4855 )
4956 ? true
50- : false
57+ : false ,
58+ links : shouldUseFlagshipLink
59+ ? new FlagshipLink ( { webviewIntent : intent } )
60+ : null
5161 } )
5262
5363 cozyClient . registerPlugin ( flag . plugin )
@@ -100,7 +110,21 @@ const ThemeProvider = ({ children }) => {
100110 * for an app
101111 */
102112const AppWrapper = ( { children } ) => {
103- const appContext = setupAppContext ( )
113+ const webviewIntent = useWebviewIntent ( )
114+ const [ appContext , setAppContext ] = useState ( undefined )
115+
116+ useEffect ( ( ) => {
117+ if ( isFlagshipApp ( ) && ! webviewIntent ) return
118+
119+ const newAppContext = setupAppContext ( webviewIntent )
120+
121+ setAppContext ( newAppContext )
122+ } , [ webviewIntent ] )
123+
124+ if ( ! appContext ) {
125+ return null
126+ }
127+
104128 const { store, cozyClient, context, lang, persistor } = appContext
105129
106130 return (
0 commit comments