Skip to content

Commit 6e38629

Browse files
committed
feat: Handle offline mode in Flagship app
We want cozy-home to be compatible with the new Flagship app's Offline mode When hosted in a Flagship app's WebView we now want to use FlagshipLink instead of StackLink in cozy-client This link will allow to redirect all queries to the Flagship app that will handle data access when offline but also when online Related PR: cozy/cozy-client#1507 Related PR: cozy/cozy-flagship-app#1239
1 parent 932f0c6 commit 6e38629

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src/components/AppWrapper.jsx

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import React, { createContext } from 'react'
1+
import React, { createContext, useEffect, useState } from 'react'
22
import { Provider as ReduxProvider } from 'react-redux'
33
import memoize from 'lodash/memoize'
44

55
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'
711
import CozyDevtools from 'cozy-client/dist/devtools'
12+
import { useWebviewIntent } from 'cozy-intent'
813
import I18n from 'cozy-ui/transpiled/react/providers/I18n'
914
import CozyTheme from 'cozy-ui/transpiled/react/providers/CozyTheme'
1015
import { BreakpointsProvider } from 'cozy-ui/transpiled/react/providers/Breakpoints'
@@ -13,7 +18,7 @@ import AlertProvider from 'cozy-ui/transpiled/react/providers/Alert'
1318

1419
import configureStore from 'store/configureStore'
1520
import { RealtimePlugin } from 'cozy-realtime'
16-
// import { isFlagshipApp } from 'cozy-device-helper'
21+
import { isFlagshipApp, isFlagshipOfflineSupported } from 'cozy-device-helper'
1722

1823
import { usePreferedTheme } from 'hooks/usePreferedTheme'
1924

@@ -30,12 +35,14 @@ export const AppContext = createContext()
3035
*
3136
* Is memoized to avoid several clients in case of hot-reload
3237
*/
33-
export const setupAppContext = memoize(() => {
38+
export const setupAppContext = memoize(intent => {
3439
const lang = document.documentElement.getAttribute('lang') || 'en'
3540
const context = window.context || 'cozy'
3641
const root = document.querySelector('[role=application]')
3742
const data = root.dataset
3843

44+
const shouldUseFlagshipLink = isFlagshipApp() && isFlagshipOfflineSupported()
45+
3946
// New improvements must be done with CozyClient
4047
const cozyClient = new CozyClient({
4148
uri: `${window.location.protocol}//${data.cozyDomain}`,
@@ -46,7 +53,10 @@ export const setupAppContext = memoize(() => {
4653
'home.store.persist'
4754
)
4855
? true
49-
: false
56+
: false,
57+
links: shouldUseFlagshipLink
58+
? new FlagshipLink({ webviewIntent: intent })
59+
: null
5060
})
5161

5262
cozyClient.registerPlugin(flag.plugin)
@@ -90,7 +100,21 @@ const ThemeProvider = ({ children }) => {
90100
* for an app
91101
*/
92102
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+
94118
const { store, cozyClient, context, lang, persistor } = appContext
95119

96120
return (

0 commit comments

Comments
 (0)