From d15bce1869dc9fde2bdc7728f90e65d29332c861 Mon Sep 17 00:00:00 2001 From: Kai Vandivier <49666798+KaiVandivier@users.noreply.github.com> Date: Thu, 14 Dec 2023 12:27:02 +0100 Subject: [PATCH] fix: handle low cli-app-scripts version [LIBS-501] (#1349) * fix: handle low cli-app-scripts version * fix: validate isConnected --- .../dhis2-connection-status.test.tsx | 6 ++-- .../dhis2-connection-status.tsx | 32 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/services/offline/src/lib/dhis2-connection-status/dhis2-connection-status.test.tsx b/services/offline/src/lib/dhis2-connection-status/dhis2-connection-status.test.tsx index 71f44328..c5801d93 100644 --- a/services/offline/src/lib/dhis2-connection-status/dhis2-connection-status.test.tsx +++ b/services/offline/src/lib/dhis2-connection-status/dhis2-connection-status.test.tsx @@ -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) }) }) diff --git a/services/offline/src/lib/dhis2-connection-status/dhis2-connection-status.tsx b/services/offline/src/lib/dhis2-connection-status/dhis2-connection-status.tsx index accf3ac6..35bf1b2a 100644 --- a/services/offline/src/lib/dhis2-connection-status/dhis2-connection-status.tsx +++ b/services/offline/src/lib/dhis2-connection-status/dhis2-connection-status.tsx @@ -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 + } + const unsubscribe = offlineInterface.subscribeToDhis2ConnectionStatus({ onUpdate, }) @@ -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 ? 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 (