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
34 changes: 12 additions & 22 deletions src/components/Announcements/AnnouncementsDialogContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ const AnnouncementsDialogContent: FC<AnnouncementsDialogContentProps> = ({
const { isMobile } = useBreakpoints()
const { t, f } = useI18n()
const primaryImage = useAnnouncementsImage(
announcement.attributes.primary_image.data.attributes.formats.small?.url ??
announcement.attributes.primary_image.data.attributes.url
announcement.primary_image.data.attributes.formats.small?.url ??
announcement.primary_image.data.attributes.url
)
const secondaryImage = useAnnouncementsImage(
announcement.attributes.secondary_image.data?.attributes.formats.thumbnail
.url
announcement.secondary_image.data?.attributes.formats.thumbnail.url
)

const handleMainAction = (): void => {
if (announcement.attributes.main_action?.link) {
window.open(announcement.attributes.main_action.link, '_blank')
if (announcement.main_action?.link) {
window.open(announcement.main_action.link, '_blank')
}
}

Expand All @@ -44,10 +43,7 @@ const AnnouncementsDialogContent: FC<AnnouncementsDialogContentProps> = ({
{primaryImage ? (
<img
src={primaryImage}
alt={
announcement.attributes.primary_image.data.attributes
.alternativeText
}
alt={announcement.primary_image.data.attributes.alternativeText}
className="u-mb-2 u-bdrs-3 u-maw-100 u-mt-2-s"
style={{
objectFit: 'cover',
Expand All @@ -57,18 +53,15 @@ const AnnouncementsDialogContent: FC<AnnouncementsDialogContentProps> = ({
/>
) : null}
<Typography align="center" className="u-mb-half" variant="h3">
{announcement.attributes.title}
{announcement.title}
</Typography>
<Typography
align="center"
color="textSecondary"
className="u-mb-1"
variant="body2"
>
{f(
announcement.attributes.start_at,
t('AnnouncementsDialogContent.dateFormat')
)}
{f(announcement.start_at, t('AnnouncementsDialogContent.dateFormat'))}
</Typography>
<div
className={cx(
Expand All @@ -77,14 +70,14 @@ const AnnouncementsDialogContent: FC<AnnouncementsDialogContentProps> = ({
'u-ta-center u-maw-100'
)}
>
<Markdown content={announcement.attributes.content} />
<Markdown content={announcement.content} />
</div>
{announcement.attributes.main_action ? (
{announcement.main_action ? (
<Buttons
fullWidth
className="u-mb-half"
variant="secondary"
label={announcement.attributes.main_action.label}
label={announcement.main_action.label}
onClick={handleMainAction}
/>
) : null}
Expand All @@ -97,10 +90,7 @@ const AnnouncementsDialogContent: FC<AnnouncementsDialogContentProps> = ({
{secondaryImage ? (
<img
src={secondaryImage}
alt={
announcement.attributes.secondary_image.data?.attributes
.alternativeText
}
alt={announcement.secondary_image.data?.attributes.alternativeText}
className="u-mt-1 u-w-2 u-h-2"
style={{
objectFit: 'cover',
Expand Down
54 changes: 26 additions & 28 deletions src/components/Announcements/types.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
export interface Announcement {
id: string
type: string
attributes: {
title: string
content: string
start_at: string
uuid: string
main_action?: {
label: string
link: string
}
primary_image: {
data: {
attributes: {
formats: {
small?: {
url: string
}
title: string
content: string
start_at: string
uuid: string
main_action?: {
label: string
link: string
}
primary_image: {
data: {
attributes: {
formats: {
small?: {
url: string
}
alternativeText?: string
url: string
}
alternativeText?: string
url: string
}
}
secondary_image: {
data: {
attributes: {
formats: {
thumbnail: {
url: string
}
}
secondary_image: {
data: {
attributes: {
formats: {
thumbnail: {
url: string
}
alternativeText?: string
}
} | null
}
alternativeText?: string
}
} | null
}
}

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
Loading
Loading