Skip to content

Commit 38cfeb8

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 86c8c34 commit 38cfeb8

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-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'
@@ -14,7 +19,7 @@ import { useCozyTheme } from 'cozy-ui/transpiled/react/providers/CozyTheme'
1419

1520
import configureStore from 'store/configureStore'
1621
import { RealtimePlugin } from 'cozy-realtime'
17-
// import { isFlagshipApp } from 'cozy-device-helper'
22+
import { isFlagshipApp, isFlagshipOfflineSupported } from 'cozy-device-helper'
1823

1924
import { DataProxyProvider } from 'cozy-dataproxy-lib'
2025
import { useWallpaperContext } from 'hooks/useWallpaperContext'
@@ -32,12 +37,14 @@ export const AppContext = createContext()
3237
*
3338
* Is memoized to avoid several clients in case of hot-reload
3439
*/
35-
export const setupAppContext = memoize(() => {
40+
export const setupAppContext = memoize(intent => {
3641
const lang = document.documentElement.getAttribute('lang') || 'en'
3742
const context = window.context || 'cozy'
3843
const root = document.querySelector('[role=application]')
3944
const data = root.dataset
4045

46+
const shouldUseFlagshipLink = isFlagshipApp() && isFlagshipOfflineSupported()
47+
4148
// New improvements must be done with CozyClient
4249
const cozyClient = new CozyClient({
4350
uri: `${window.location.protocol}//${data.cozyDomain}`,
@@ -48,7 +55,10 @@ export const setupAppContext = memoize(() => {
4855
'home.store.persist'
4956
)
5057
? true
51-
: false
58+
: false,
59+
links: shouldUseFlagshipLink
60+
? new FlagshipLink({ webviewIntent: intent })
61+
: null
5262
})
5363

5464
cozyClient.registerPlugin(flag.plugin)
@@ -103,7 +113,21 @@ const ThemeProvider = ({ children }) => {
103113
* for an app
104114
*/
105115
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+
107131
const { store, cozyClient, context, lang, persistor } = appContext
108132

109133
return (

0 commit comments

Comments
 (0)