Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Handle offline mode in Flagship app #2186

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions manifest.webapp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"version": "1.85.0",
"licence": "AGPL-3.0",
"offline_support": true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this documented somewhere? If not, we should do it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"permissions": {
"home": {
"description": "Required to manage default redirection update",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@sentry/react": "7.119.0",
"comlink": "4.4.1",
"cozy-client": "^51.6.0",
"cozy-dataproxy-lib": "^2.3.0",
"cozy-dataproxy-lib": "^2.4.1",
"cozy-device-helper": "3.7.1",
"cozy-devtools": "^1.2.1",
"cozy-doctypes": "1.83.8",
Expand Down
44 changes: 31 additions & 13 deletions src/components/AppHighlightAlert/AppHighlightAlertWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,52 @@ import React, { useEffect, useState } from 'react'

import { getAvailableAppHighlightAlerts } from 'components/AppHighlightAlert/helpers'
import { useClient } from 'cozy-client'
import log from 'cozy-logger'

const AppHighlightAlertWrapper = ({ apps }) => {
const [appHighlightAlerts, setAppHighlightAlerts] = useState([])
const [isAppHighlightAlertsError, setIsAppHighlightAlertsError] =
useState(false)
const client = useClient()

useEffect(() => {
const getAppHighlightAlerts = async () => {
const availableAppHighlightAlerts = await getAvailableAppHighlightAlerts(
client,
apps
)

setAppHighlightAlerts(availableAppHighlightAlerts)
try {
const availableAppHighlightAlerts =
await getAvailableAppHighlightAlerts(client, apps)

setAppHighlightAlerts(availableAppHighlightAlerts)
} catch (error) {
log('error', `App highlight error: ${error}`)
setIsAppHighlightAlertsError(true)
}
}

if (apps && appHighlightAlerts.length === 0) {
if (apps && !isAppHighlightAlertsError && appHighlightAlerts.length === 0) {
getAppHighlightAlerts()
}
}, [client, apps, appHighlightAlerts.length])
}, [client, apps, isAppHighlightAlertsError, appHighlightAlerts.length])

useEffect(() => {
if (appHighlightAlerts && appHighlightAlerts?.length > 0) {
appHighlightAlerts.forEach(component => {
if (component.displayed) {
component.onDisplayed()
} else {
component.onNotDisplayed()
if (component) {
component.displayed
? component.onDisplayed()
: component.onNotDisplayed()
}
})
}

useEffect(() => {
if (appHighlightAlerts && appHighlightAlerts?.length > 0) {
appHighlightAlerts.forEach(component => {
if (component) {
component.displayed
? component.onDisplayed()
: component.onNotDisplayed()
}
})
}
}, [appHighlightAlerts])

return (
Expand Down
39 changes: 33 additions & 6 deletions src/components/AppWrapper.jsx
Original file line number Diff line number Diff line change
@@ -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,
WebFlagshipLink
} from 'cozy-client'
import CozyDevtools from 'cozy-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'
Expand All @@ -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 { DataProxyProvider } from 'cozy-dataproxy-lib'
import { useWallpaperContext } from 'hooks/useWallpaperContext'
Expand All @@ -32,12 +37,19 @@ 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 shouldUseWebFlagshipLink =
isFlagshipApp() && isFlagshipOfflineSupported()

const links = shouldUseWebFlagshipLink
? [new WebFlagshipLink({ webviewIntent: intent })]
: null

// New improvements must be done with CozyClient
const cozyClient = new CozyClient({
uri: `${window.location.protocol}//${data.cozyDomain}`,
Expand All @@ -48,7 +60,8 @@ export const setupAppContext = memoize(() => {
'home.store.persist'
)
? true
: false
: false,
links
})

cozyClient.registerPlugin(flag.plugin)
Expand Down Expand Up @@ -103,7 +116,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 (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react'

import { useClient, useQuery } from 'cozy-client'
import { useSettings } from 'cozy-client'
import { useWebviewIntent } from 'cozy-intent'
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'
import { makeStyles } from 'cozy-ui/transpiled/react/styles'
Expand All @@ -10,13 +10,12 @@ import Button from 'cozy-ui/transpiled/react/Buttons'
import Icon from 'cozy-ui/transpiled/react/Icon'
import LightbulbIcon from 'cozy-ui/transpiled/react/Icons/Lightbulb'

import { instanceSettingsConn, homeSettingsConn } from 'queries'
import {
shouldShowDefaultRedirectionSnackbar,
disableDefaultRedirectionSnackbar,
setDefaultRedirectionToHome
} from './helpers'
HOME_DEFAULT_REDIRECTION,
useShouldShowDefaultRedirectionSnackbar
} from './useShouldShowDefaultRedirectionSnackbar'
import useIncrementDefaultRedirectionViewCount from './useIncrementDefaultRedirectionViewCount'
import { isFlagshipApp } from 'cozy-device-helper'

const useStyles = makeStyles(theme => ({
snackbar: {
Expand All @@ -29,39 +28,39 @@ const useStyles = makeStyles(theme => ({

const DefaultAppSnackbar = () => {
const { t } = useI18n()
const client = useClient()
const classes = useStyles()
const [isOpen, setIsOpen] = useState(true)

const webviewIntent = useWebviewIntent()

const instanceSettingsResult = useQuery(
instanceSettingsConn.query,
instanceSettingsConn
)
const { save: saveHome } = useSettings('home', [
'default_redirection_snackbar_disabled'
])

const homeSettingsResult = useQuery(homeSettingsConn.query, homeSettingsConn)
const { save: saveGlobal } = useSettings('instance', ['default_redirection'])

useIncrementDefaultRedirectionViewCount(
instanceSettingsResult,
homeSettingsResult
)
useIncrementDefaultRedirectionViewCount()

const showDefaultAppSnackbar = shouldShowDefaultRedirectionSnackbar(
instanceSettingsResult,
homeSettingsResult,
isOpen
)
const showDefaultAppSnackbar = useShouldShowDefaultRedirectionSnackbar(isOpen)

const onRefuse = () => {
setIsOpen(false)
disableDefaultRedirectionSnackbar(client, homeSettingsResult)
saveHome({
default_redirection_snackbar_disabled: true
})
}

const onAccept = () => {
setIsOpen(false)
disableDefaultRedirectionSnackbar(client, homeSettingsResult)
setDefaultRedirectionToHome(client, instanceSettingsResult, webviewIntent)
saveHome({
default_redirection_snackbar_disabled: true
})
saveGlobal({
default_redirection: HOME_DEFAULT_REDIRECTION
})
if (isFlagshipApp()) {
webviewIntent.call('setDefaultRedirection', HOME_DEFAULT_REDIRECTION)
}
}

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import React from 'react'
import { render, fireEvent } from '@testing-library/react'

import { useSettings } from 'cozy-client'

import CozyTheme from 'cozy-ui/transpiled/react/providers/CozyTheme'

import AppLike from 'test/AppLike'
import DefaultRedirectionSnackbar from './DefaultRedirectionSnackbar'
import {
shouldShowDefaultRedirectionSnackbar,
disableDefaultRedirectionSnackbar,
setDefaultRedirectionToHome
} from './helpers'
import { useShouldShowDefaultRedirectionSnackbar } from './useShouldShowDefaultRedirectionSnackbar'

jest.mock('cozy-client', () => ({
...jest.requireActual('cozy-client'),
useQuery: jest.fn(),
useClient: jest.fn()
useClient: jest.fn(),
useSettings: jest.fn()
}))
jest.mock('./helpers')
jest.mock('./useShouldShowDefaultRedirectionSnackbar')
jest.mock('./useIncrementDefaultRedirectionViewCount')

const setup = () => {
Expand All @@ -36,7 +35,26 @@ describe('DefaultRedirectionSnackbar', () => {
})

it('should display default redirection snackbar', () => {
shouldShowDefaultRedirectionSnackbar.mockReturnValue(true)
useShouldShowDefaultRedirectionSnackbar.mockReturnValue(true)
const mockSaveInstance = jest.fn()
const mockSaveHome = jest.fn()
useSettings.mockImplementation((...args) => {
if (args[0] === 'instance') {
return {
values: {
default_redirection: 'drive/#/folder'
},
save: mockSaveInstance
}
} else if (args[0] === 'home') {
return {
values: {
default_redirection_view_count: 4
},
save: mockSaveHome
}
}
})

const { root } = setup()

Expand All @@ -45,24 +63,70 @@ describe('DefaultRedirectionSnackbar', () => {
})

it('should disable default redirection snackbar and set default redirection to home when accepting', () => {
shouldShowDefaultRedirectionSnackbar.mockReturnValue(true)
useShouldShowDefaultRedirectionSnackbar.mockReturnValue(true)

const mockSaveInstance = jest.fn()
const mockSaveHome = jest.fn()
useSettings.mockImplementation((...args) => {
if (args[0] === 'instance') {
return {
values: {
default_redirection: 'drive/#/folder'
},
save: mockSaveInstance
}
} else if (args[0] === 'home') {
return {
values: {
default_redirection_view_count: 4
},
save: mockSaveHome
}
}
})

const { root } = setup()

fireEvent.click(root.queryByText('OK'))

expect(disableDefaultRedirectionSnackbar).toHaveBeenCalled()
expect(setDefaultRedirectionToHome).toHaveBeenCalled()
expect(mockSaveInstance).toHaveBeenCalledWith({
default_redirection: 'home/'
})
expect(mockSaveHome).toHaveBeenCalledWith({
default_redirection_snackbar_disabled: true
})
})

it('should disable default redirection snackbar when refusing', () => {
shouldShowDefaultRedirectionSnackbar.mockReturnValue(true)
useShouldShowDefaultRedirectionSnackbar.mockReturnValue(true)

const mockSaveInstance = jest.fn()
const mockSaveHome = jest.fn()
useSettings.mockImplementation((...args) => {
if (args[0] === 'instance') {
return {
values: {
default_redirection: 'drive/#/folder'
},
save: mockSaveInstance
}
} else if (args[0] === 'home') {
return {
values: {
default_redirection_view_count: 4
},
save: mockSaveHome
}
}
})

const { root } = setup()

fireEvent.click(root.queryByText('No, thank you'))

expect(disableDefaultRedirectionSnackbar).toHaveBeenCalled()
expect(setDefaultRedirectionToHome).not.toHaveBeenCalled()
expect(mockSaveHome).toHaveBeenCalledWith({
default_redirection_snackbar_disabled: true
})
expect(mockSaveInstance).not.toHaveBeenCalled()
})
})
Loading
Loading