Skip to content

Commit fa4678b

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: linagora/cozy-client#1507 Related PR: linagora/cozy-flagship-app#1239
1 parent df1c405 commit fa4678b

1 file changed

Lines changed: 30 additions & 6 deletions

File tree

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'
@@ -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 { 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
*/
102112
const 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

Comments
 (0)