Skip to content

Commit

Permalink
feat(backup): Show BackupAppHighlightAlert after 2 flagship app open
Browse files Browse the repository at this point in the history
By using localStorage to store the number of times the user open the
home app inside flagship app, we can very simply show
BackupAppHighlightAlert when user open flagship app for the second
time.

It also avoid to add code in flagship app.
  • Loading branch information
zatteo committed Jul 27, 2023
1 parent 4142e3f commit 433608b
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions src/components/AppHighlightAlert/BackupAppHighlightAlert.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,77 @@
import React from 'react'
import React, { useEffect, useState } from 'react'

import flag from 'cozy-flags'
import { isFlagshipApp } from 'cozy-device-helper'
import { useI18n } from 'cozy-ui/transpiled/react'

import AppHighlightAlert from 'components/AppHighlightAlert/AppHighlightAlert'

const APP_START_COUNT_KEY = 'BackupAppHighlightAlert__appStartCount'

const MAX_COUNT_VALUE = 2
const DISABLED_COUNT_VALUE = -1

const useBackupAppHighlightAlert = () => {
const [
shouldShowBackupAppHighlightAlert,
setShouldShowBackupAppHighlightAlert
] = useState(false)

useEffect(() => {
if (
!isFlagshipApp() ||
!flag('flagship.backup.enabled') ||
!flag('flagship.backup.homeHighlightEnabled')
) {
return
}

const appStartCount = parseInt(
localStorage.getItem(APP_START_COUNT_KEY),
10
)

let newAppStartCount

if (isNaN(appStartCount)) {
newAppStartCount = 1
} else if (appStartCount === DISABLED_COUNT_VALUE) {
return
} else if (appStartCount < MAX_COUNT_VALUE) {
newAppStartCount = appStartCount + 1
} else {
newAppStartCount = DISABLED_COUNT_VALUE
}

localStorage.setItem(APP_START_COUNT_KEY, newAppStartCount.toString())

setShouldShowBackupAppHighlightAlert(newAppStartCount === 2)
}, [])

return [
shouldShowBackupAppHighlightAlert,
setShouldShowBackupAppHighlightAlert
]
}

const BackupAppHighlightAlert = ({ apps }) => {
const { t } = useI18n()
const [
shouldShowBackupAppHighlightAlert,
setShouldShowBackupAppHighlightAlert
] = useBackupAppHighlightAlert()

const onClose = () => {
setShouldShowBackupAppHighlightAlert(false)
}

const onClose = () => {}
if (!shouldShowBackupAppHighlightAlert) {
return null
}

return (
<AppHighlightAlert
key="BackupAppHighlightAlert"
apps={apps}
appToHighlightSlug="photos"
onClose={onClose}
Expand Down

0 comments on commit 433608b

Please sign in to comment.