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

fix: handle low cli-app-scripts version #1349

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ describe('initialization to the right values based on offline interface', () =>
wrapper: customWrapper,
})

expect(result.current.isConnected).toBe(false)
expect(result.current.isDisconnected).toBe(true)
expect(result.current.lastConnected).toEqual(testCurrentDate)
expect(result.current.isConnected).toBe(true)
expect(result.current.isDisconnected).toBe(false)
expect(result.current.lastConnected).toBe(null)
Comment on lines -158 to +160
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See the fix to the fail-safe below

})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ export const Dhis2ConnectionStatusProvider = ({
}, [pingAndHandleStatus, serverVersion])

useEffect(() => {
if (!offlineInterface.subscribeToDhis2ConnectionStatus) {
// Missing this functionality from the offline interface --
// use a ping on startup to get the status
smartIntervalRef.current?.invokeCallbackImmediately()
console.warn(
'Please upgrade to @dhis2/cli-app-scripts@>10.3.8 for full connection status features'
)
return
}

Comment on lines +184 to +193
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This bit protects against the compatibility error

const unsubscribe = offlineInterface.subscribeToDhis2ConnectionStatus({
onUpdate,
})
Expand All @@ -190,23 +200,23 @@ export const Dhis2ConnectionStatusProvider = ({
}, [offlineInterface, onUpdate])

// Memoize this value to prevent unnecessary rerenders of context provider
const contextValue = useMemo(
() => ({
// in the unlikely circumstance that offlineInterface.latestIsConnected
// is `null` when this initializes, fail safe by defaulting to
// `isConnected: false`
isConnected: Boolean(isConnected),
isDisconnected: !isConnected,
lastConnected: isConnected
const contextValue = useMemo(() => {
// in the unlikely circumstance that offlineInterface.latestIsConnected
// is `null` or `undefined` when this initializes, fail safe by defaulting to
// `isConnected: true`. A ping or SW update should update the status shortly.
const validatedIsConnected = isConnected ?? true
return {
isConnected: validatedIsConnected,
isDisconnected: !validatedIsConnected,
lastConnected: validatedIsConnected
Comment on lines -193 to +211
Copy link
Contributor Author

@KaiVandivier KaiVandivier Aug 17, 2023

Choose a reason for hiding this comment

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

This is a small fix: the correct fail-safe should be to default to isConnected: true so the app can be accessed (the UI might be blocked or changed if isConnected === false)

? null
: // Only evaluate if disconnected, since local storage
// is synchronous and disk-based.
// If lastConnected is not set in localStorage though, set it.
// (relevant on startup)
getLastConnected(appName) || updateLastConnected(appName),
}),
[isConnected, appName]
)
}
}, [isConnected, appName])

return (
<Dhis2ConnectionStatusContext.Provider value={contextValue}>
Expand Down
Loading