Skip to content

Commit

Permalink
fix: Prevent infinite loop at startup
Browse files Browse the repository at this point in the history
At startup we check some "app highlights" status, typicallky for backup
or geolocation tracking.
In the tracking case, we need to query the database in order to check if
there is some geolocation data.
This query might fail in offline if the local database is not ready.

And this failure was causing an infinite loop, because a retry was made
for each failed query.
  • Loading branch information
paultranvan committed Jan 27, 2025
1 parent 9caa2a1 commit 9fec2d0
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/components/AppHighlightAlert/AppHighlightAlertWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,41 @@ 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, appHighlightAlerts, isAppHighlightAlertsError])

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()
}
})
}, [appHighlightAlerts])
}

return (
<>
Expand Down

0 comments on commit 9fec2d0

Please sign in to comment.