1
- import React , { createContext } from 'react'
1
+ import React , { createContext , useEffect , useState } from 'react'
2
2
import { Provider as ReduxProvider } from 'react-redux'
3
3
import memoize from 'lodash/memoize'
4
4
5
5
import 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'
7
11
import CozyDevtools from 'cozy-devtools'
12
+ import { useWebviewIntent } from 'cozy-intent'
8
13
import I18n from 'cozy-ui/transpiled/react/providers/I18n'
9
14
import CozyTheme from 'cozy-ui/transpiled/react/providers/CozyTheme'
10
15
import { BreakpointsProvider } from 'cozy-ui/transpiled/react/providers/Breakpoints'
@@ -14,7 +19,7 @@ import { useCozyTheme } from 'cozy-ui/transpiled/react/providers/CozyTheme'
14
19
15
20
import configureStore from 'store/configureStore'
16
21
import { RealtimePlugin } from 'cozy-realtime'
17
- // import { isFlagshipApp } from 'cozy-device-helper'
22
+ import { isFlagshipApp , isFlagshipOfflineSupported } from 'cozy-device-helper'
18
23
19
24
import { DataProxyProvider } from 'cozy-dataproxy-lib'
20
25
import { useWallpaperContext } from 'hooks/useWallpaperContext'
@@ -32,12 +37,14 @@ export const AppContext = createContext()
32
37
*
33
38
* Is memoized to avoid several clients in case of hot-reload
34
39
*/
35
- export const setupAppContext = memoize ( ( ) => {
40
+ export const setupAppContext = memoize ( intent => {
36
41
const lang = document . documentElement . getAttribute ( 'lang' ) || 'en'
37
42
const context = window . context || 'cozy'
38
43
const root = document . querySelector ( '[role=application]' )
39
44
const data = root . dataset
40
45
46
+ const shouldUseFlagshipLink = isFlagshipApp ( ) && isFlagshipOfflineSupported ( )
47
+
41
48
// New improvements must be done with CozyClient
42
49
const cozyClient = new CozyClient ( {
43
50
uri : `${ window . location . protocol } //${ data . cozyDomain } ` ,
@@ -48,7 +55,10 @@ export const setupAppContext = memoize(() => {
48
55
'home.store.persist'
49
56
)
50
57
? true
51
- : false
58
+ : false ,
59
+ links : shouldUseFlagshipLink
60
+ ? new FlagshipLink ( { webviewIntent : intent } )
61
+ : null
52
62
} )
53
63
54
64
cozyClient . registerPlugin ( flag . plugin )
@@ -103,7 +113,21 @@ const ThemeProvider = ({ children }) => {
103
113
* for an app
104
114
*/
105
115
const AppWrapper = ( { children } ) => {
106
- const appContext = setupAppContext ( )
116
+ const webviewIntent = useWebviewIntent ( )
117
+ const [ appContext , setAppContext ] = useState ( undefined )
118
+
119
+ useEffect ( ( ) => {
120
+ if ( isFlagshipApp ( ) && ! webviewIntent ) return
121
+
122
+ const newAppContext = setupAppContext ( webviewIntent )
123
+
124
+ setAppContext ( newAppContext )
125
+ } , [ webviewIntent ] )
126
+
127
+ if ( ! appContext ) {
128
+ return null
129
+ }
130
+
107
131
const { store, cozyClient, context, lang, persistor } = appContext
108
132
109
133
return (
0 commit comments