From 189dc77860137c60189153d60d4d9b3891f8c120 Mon Sep 17 00:00:00 2001 From: Ldoppea Date: Wed, 19 Jun 2024 17:03:19 +0200 Subject: [PATCH] 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 --- src/components/AppWrapper.jsx | 36 +++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/components/AppWrapper.jsx b/src/components/AppWrapper.jsx index dac323112b..146765e9ff 100644 --- a/src/components/AppWrapper.jsx +++ b/src/components/AppWrapper.jsx @@ -1,10 +1,15 @@ -import React, { createContext } from 'react' +import React, { createContext, useEffect, useState } from 'react' import { Provider as ReduxProvider } from 'react-redux' import memoize from 'lodash/memoize' import flag from 'cozy-flags' -import CozyClient, { CozyProvider, RealTimeQueries } from 'cozy-client' +import CozyClient, { + CozyProvider, + RealTimeQueries, + FlagshipLink +} from 'cozy-client' import CozyDevtools from 'cozy-client/dist/devtools' +import { useWebviewIntent } from 'cozy-intent' import I18n from 'cozy-ui/transpiled/react/providers/I18n' import CozyTheme from 'cozy-ui/transpiled/react/providers/CozyTheme' import { BreakpointsProvider } from 'cozy-ui/transpiled/react/providers/Breakpoints' @@ -14,7 +19,7 @@ import { useCozyTheme } from 'cozy-ui/transpiled/react/providers/CozyTheme' import configureStore from 'store/configureStore' import { RealtimePlugin } from 'cozy-realtime' -// import { isFlagshipApp } from 'cozy-device-helper' +import { isFlagshipApp, isFlagshipOfflineSupported } from 'cozy-device-helper' import { useWallpaperContext } from 'hooks/useWallpaperContext' @@ -31,12 +36,14 @@ export const AppContext = createContext() * * Is memoized to avoid several clients in case of hot-reload */ -export const setupAppContext = memoize(() => { +export const setupAppContext = memoize(intent => { const lang = document.documentElement.getAttribute('lang') || 'en' const context = window.context || 'cozy' const root = document.querySelector('[role=application]') const data = root.dataset + const shouldUseFlagshipLink = isFlagshipApp() && isFlagshipOfflineSupported() + // New improvements must be done with CozyClient const cozyClient = new CozyClient({ uri: `${window.location.protocol}//${data.cozyDomain}`, @@ -47,7 +54,10 @@ export const setupAppContext = memoize(() => { 'home.store.persist' ) ? true - : false + : false, + links: shouldUseFlagshipLink + ? new FlagshipLink({ webviewIntent: intent }) + : null }) cozyClient.registerPlugin(flag.plugin) @@ -100,7 +110,21 @@ const ThemeProvider = ({ children }) => { * for an app */ const AppWrapper = ({ children }) => { - const appContext = setupAppContext() + const webviewIntent = useWebviewIntent() + const [appContext, setAppContext] = useState(undefined) + + useEffect(() => { + if (isFlagshipApp() && !webviewIntent) return + + const newAppContext = setupAppContext(webviewIntent) + + setAppContext(newAppContext) + }, [webviewIntent]) + + if (!appContext) { + return null + } + const { store, cozyClient, context, lang, persistor } = appContext return (