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-client/dist/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'
@@ -13,7 +18,7 @@ import AlertProvider from 'cozy-ui/transpiled/react/providers/Alert'
13
18
14
19
import configureStore from 'store/configureStore'
15
20
import { RealtimePlugin } from 'cozy-realtime'
16
- // import { isFlagshipApp } from 'cozy-device-helper'
21
+ import { isFlagshipApp , isFlagshipOfflineSupported } from 'cozy-device-helper'
17
22
18
23
import { usePreferedTheme } from 'hooks/usePreferedTheme'
19
24
@@ -30,12 +35,14 @@ export const AppContext = createContext()
30
35
*
31
36
* Is memoized to avoid several clients in case of hot-reload
32
37
*/
33
- export const setupAppContext = memoize ( ( ) => {
38
+ export const setupAppContext = memoize ( intent => {
34
39
const lang = document . documentElement . getAttribute ( 'lang' ) || 'en'
35
40
const context = window . context || 'cozy'
36
41
const root = document . querySelector ( '[role=application]' )
37
42
const data = root . dataset
38
43
44
+ const shouldUseFlagshipLink = isFlagshipApp ( ) && isFlagshipOfflineSupported ( )
45
+
39
46
// New improvements must be done with CozyClient
40
47
const cozyClient = new CozyClient ( {
41
48
uri : `${ window . location . protocol } //${ data . cozyDomain } ` ,
@@ -46,7 +53,10 @@ export const setupAppContext = memoize(() => {
46
53
'home.store.persist'
47
54
)
48
55
? true
49
- : false
56
+ : false ,
57
+ links : shouldUseFlagshipLink
58
+ ? new FlagshipLink ( { webviewIntent : intent } )
59
+ : null
50
60
} )
51
61
52
62
cozyClient . registerPlugin ( flag . plugin )
@@ -90,7 +100,21 @@ const ThemeProvider = ({ children }) => {
90
100
* for an app
91
101
*/
92
102
const AppWrapper = ( { children } ) => {
93
- const appContext = setupAppContext ( )
103
+ const webviewIntent = useWebviewIntent ( )
104
+ const [ appContext , setAppContext ] = useState ( undefined )
105
+
106
+ useEffect ( ( ) => {
107
+ if ( isFlagshipApp ( ) && ! webviewIntent ) return
108
+
109
+ const newAppContext = setupAppContext ( webviewIntent )
110
+
111
+ setAppContext ( newAppContext )
112
+ } , [ webviewIntent ] )
113
+
114
+ if ( ! appContext ) {
115
+ return null
116
+ }
117
+
94
118
const { store, cozyClient, context, lang, persistor } = appContext
95
119
96
120
return (
0 commit comments