Skip to content

Commit 792df1a

Browse files
authored
Merge pull request #43888 from tienifr/fix/43536
Revert: Backend unreachability message
2 parents 0ea9791 + 9c060fb commit 792df1a

File tree

15 files changed

+80
-165
lines changed

15 files changed

+80
-165
lines changed

.storybook/preview.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import './fonts.css';
1616
Onyx.init({
1717
keys: ONYXKEYS,
1818
initialKeyStates: {
19-
[ONYXKEYS.NETWORK]: {isOffline: false, isBackendReachable: true},
19+
[ONYXKEYS.NETWORK]: {isOffline: false},
2020
},
2121
});
2222

src/CONST.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -561,10 +561,8 @@ const CONST = {
561561
EMPTY_ARRAY,
562562
EMPTY_OBJECT,
563563
USE_EXPENSIFY_URL,
564-
STATUS_EXPENSIFY_URL: 'https://status.expensify.com',
565564
GOOGLE_MEET_URL_ANDROID: 'https://meet.google.com',
566565
GOOGLE_DOC_IMAGE_LINK_MATCH: 'googleusercontent.com',
567-
GOOGLE_CLOUD_URL: 'https://clients3.google.com/generate_204',
568566
IMAGE_BASE64_MATCH: 'base64',
569567
DEEPLINK_BASE_URL: 'new-expensify://',
570568
PDF_VIEWER_URL: '/pdf/web/viewer.html',
@@ -1061,7 +1059,7 @@ const CONST = {
10611059
MAX_RETRY_WAIT_TIME_MS: 10 * 1000,
10621060
PROCESS_REQUEST_DELAY_MS: 1000,
10631061
MAX_PENDING_TIME_MS: 10 * 1000,
1064-
BACKEND_CHECK_INTERVAL_MS: 60 * 1000,
1062+
RECHECK_INTERVAL_MS: 60 * 1000,
10651063
MAX_REQUEST_RETRIES: 10,
10661064
NETWORK_STATUS: {
10671065
ONLINE: 'online',
@@ -1073,7 +1071,7 @@ const CONST = {
10731071
DEFAULT_TIME_ZONE: {automatic: true, selected: 'America/Los_Angeles'},
10741072
DEFAULT_ACCOUNT_DATA: {errors: null, success: '', isLoading: false},
10751073
DEFAULT_CLOSE_ACCOUNT_DATA: {errors: null, success: '', isLoading: false},
1076-
DEFAULT_NETWORK_DATA: {isOffline: false, isBackendReachable: true},
1074+
DEFAULT_NETWORK_DATA: {isOffline: false},
10771075
FORMS: {
10781076
LOGIN_FORM: 'LoginForm',
10791077
VALIDATE_CODE_FORM: 'ValidateCodeForm',

src/Expensify.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ function Expensify({
146146
// Initialize this client as being an active client
147147
ActiveClientManager.init();
148148

149-
// Used for the offline indicator appearing when someone is offline or backend is unreachable
150-
const unsubscribeNetworkStatus = NetworkConnection.subscribeToNetworkStatus();
149+
// Used for the offline indicator appearing when someone is offline
150+
const unsubscribeNetInfo = NetworkConnection.subscribeToNetInfo();
151151

152-
return () => unsubscribeNetworkStatus();
152+
return unsubscribeNetInfo;
153153
}, []);
154154

155155
useEffect(() => {

src/components/OfflineIndicator.tsx

+3-20
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout';
77
import useTheme from '@hooks/useTheme';
88
import useThemeStyles from '@hooks/useThemeStyles';
99
import variables from '@styles/variables';
10-
import CONST from '@src/CONST';
1110
import Icon from './Icon';
1211
import * as Expensicons from './Icon/Expensicons';
1312
import Text from './Text';
14-
import TextLink from './TextLink';
1513

1614
type OfflineIndicatorProps = {
1715
/** Optional styles for container element that will override the default styling for the offline indicator */
@@ -25,7 +23,7 @@ function OfflineIndicator({style, containerStyles}: OfflineIndicatorProps) {
2523
const theme = useTheme();
2624
const styles = useThemeStyles();
2725
const {translate} = useLocalize();
28-
const {isOffline, isBackendReachable} = useNetwork();
26+
const {isOffline} = useNetwork();
2927
const {shouldUseNarrowLayout} = useResponsiveLayout();
3028

3129
const computedStyles = useMemo((): StyleProp<ViewStyle> => {
@@ -36,7 +34,7 @@ function OfflineIndicator({style, containerStyles}: OfflineIndicatorProps) {
3634
return shouldUseNarrowLayout ? styles.offlineIndicatorMobile : styles.offlineIndicator;
3735
}, [containerStyles, shouldUseNarrowLayout, styles.offlineIndicatorMobile, styles.offlineIndicator]);
3836

39-
if (!isOffline && isBackendReachable) {
37+
if (!isOffline) {
4038
return null;
4139
}
4240

@@ -48,22 +46,7 @@ function OfflineIndicator({style, containerStyles}: OfflineIndicatorProps) {
4846
width={variables.iconSizeSmall}
4947
height={variables.iconSizeSmall}
5048
/>
51-
<Text style={[styles.ml3, styles.chatItemComposeSecondaryRowSubText]}>
52-
{isOffline ? (
53-
translate('common.youAppearToBeOffline')
54-
) : (
55-
<>
56-
{translate('common.weMightHaveProblem')}
57-
<TextLink
58-
href={CONST.STATUS_EXPENSIFY_URL}
59-
style={[styles.chatItemComposeSecondaryRowSubText, styles.link]}
60-
>
61-
{new URL(CONST.STATUS_EXPENSIFY_URL).host}
62-
</TextLink>
63-
.
64-
</>
65-
)}
66-
</Text>
49+
<Text style={[styles.ml3, styles.chatItemComposeSecondaryRowSubText]}>{translate('common.youAppearToBeOffline')}</Text>
6750
</View>
6851
);
6952
}

src/hooks/useNetwork.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ type UseNetworkProps = {
66
onReconnect?: () => void;
77
};
88

9-
type UseNetwork = {isOffline: boolean; isBackendReachable: boolean};
9+
type UseNetwork = {isOffline: boolean};
1010

1111
export default function useNetwork({onReconnect = () => {}}: UseNetworkProps = {}): UseNetwork {
1212
const callback = useRef(onReconnect);
1313
callback.current = onReconnect;
1414

15-
const {isOffline, networkStatus, isBackendReachable} = useContext(NetworkContext) ?? {...CONST.DEFAULT_NETWORK_DATA, networkStatus: CONST.NETWORK.NETWORK_STATUS.UNKNOWN};
16-
const isNetworkStatusUnknown = networkStatus === CONST.NETWORK.NETWORK_STATUS.UNKNOWN;
15+
const {isOffline, networkStatus} = useContext(NetworkContext) ?? {...CONST.DEFAULT_NETWORK_DATA, networkStatus: CONST.NETWORK.NETWORK_STATUS.UNKNOWN};
1716
const prevOfflineStatusRef = useRef(isOffline);
1817
useEffect(() => {
1918
// If we were offline before and now we are not offline then we just reconnected
@@ -30,6 +29,6 @@ export default function useNetwork({onReconnect = () => {}}: UseNetworkProps = {
3029
prevOfflineStatusRef.current = isOffline;
3130
}, [isOffline]);
3231

33-
// If the network status is unknown, we fallback to default state, i.e. we're online and backend is reachable.
34-
return isNetworkStatusUnknown ? CONST.DEFAULT_NETWORK_DATA : {isOffline, isBackendReachable};
32+
// If the network status is undefined, we don't treat it as offline. Otherwise, we utilize the isOffline prop.
33+
return {isOffline: networkStatus === CONST.NETWORK.NETWORK_STATUS.UNKNOWN ? false : isOffline};
3534
}

src/languages/en.ts

-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ export default {
273273
your: 'your',
274274
conciergeHelp: 'Please reach out to Concierge for help.',
275275
youAppearToBeOffline: 'You appear to be offline.',
276-
weMightHaveProblem: 'We might have a problem. Check out ',
277276
thisFeatureRequiresInternet: 'This feature requires an active internet connection to be used.',
278277
attachementWillBeAvailableOnceBackOnline: 'Attachment will become available once back online.',
279278
areYouSure: 'Are you sure?',

src/languages/es.ts

-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ export default {
264264
your: 'tu',
265265
conciergeHelp: 'Por favor, contacta con Concierge para obtener ayuda.',
266266
youAppearToBeOffline: 'Parece que estás desconectado.',
267-
weMightHaveProblem: 'Peude que te tengamos un problema. Echa un vistazo a ',
268267
thisFeatureRequiresInternet: 'Esta función requiere una conexión a Internet activa para ser utilizada.',
269268
attachementWillBeAvailableOnceBackOnline: 'El archivo adjunto estará disponible cuando vuelvas a estar en línea.',
270269
areYouSure: '¿Estás seguro?',

src/libs/NetworkConnection.ts

+42-70
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import CONST from '@src/CONST';
88
import ONYXKEYS from '@src/ONYXKEYS';
99
import * as NetworkActions from './actions/Network';
1010
import AppStateMonitor from './AppStateMonitor';
11-
import checkInternetReachability from './checkInternetReachability';
1211
import Log from './Log';
1312

1413
let isOffline = false;
@@ -43,23 +42,12 @@ function setOfflineStatus(isCurrentlyOffline: boolean, reason = ''): void {
4342
// When reconnecting, ie, going from offline to online, all the reconnection callbacks
4443
// are triggered (this is usually Actions that need to re-download data from the server)
4544
if (isOffline && !isCurrentlyOffline) {
46-
NetworkActions.setIsBackendReachable(true, 'moved from offline to online');
4745
triggerReconnectionCallbacks('offline status changed');
4846
}
4947

5048
isOffline = isCurrentlyOffline;
5149
}
5250

53-
function setNetWorkStatus(isInternetReachable: boolean | null): void {
54-
let networkStatus;
55-
if (!isBoolean(isInternetReachable)) {
56-
networkStatus = CONST.NETWORK.NETWORK_STATUS.UNKNOWN;
57-
} else {
58-
networkStatus = isInternetReachable ? CONST.NETWORK.NETWORK_STATUS.ONLINE : CONST.NETWORK.NETWORK_STATUS.OFFLINE;
59-
}
60-
NetworkActions.setNetWorkStatus(networkStatus);
61-
}
62-
6351
// Update the offline status in response to changes in shouldForceOffline
6452
let shouldForceOffline = false;
6553
Onyx.connect({
@@ -103,71 +91,39 @@ Onyx.connect({
10391
});
10492

10593
/**
106-
* Set interval to periodically (re)check backend status.
107-
* Because backend unreachability might imply lost internet connection, we need to check internet reachability.
108-
* @returns clearInterval cleanup
94+
* Set up the event listener for NetInfo to tell whether the user has
95+
* internet connectivity or not. This is more reliable than the Pusher
96+
* `disconnected` event which takes about 10-15 seconds to emit.
97+
* @returns unsubscribe method
10998
*/
110-
function subscribeToBackendAndInternetReachability(): () => void {
111-
const intervalID = setInterval(() => {
112-
// Offline status also implies backend unreachability
113-
if (isOffline) {
114-
// Periodically recheck the network connection
115-
// More info: https://github.com/Expensify/App/issues/42988
116-
recheckNetworkConnection();
117-
Log.info(`[NetworkStatus] Rechecking the network connection with "isOffline" set to "true" to double-check internet reachability.`);
118-
return;
119-
}
120-
// Using the API url ensures reachability is tested over internet
121-
fetch(`${CONFIG.EXPENSIFY.DEFAULT_API_ROOT}api/Ping?accountID=${accountID || 'unknown'}`, {
122-
method: 'GET',
123-
cache: 'no-cache',
124-
})
125-
.then((response) => {
99+
function subscribeToNetInfo(): () => void {
100+
// Note: We are disabling the configuration for NetInfo when using the local web API since requests can get stuck in a 'Pending' state and are not reliable indicators for "offline".
101+
// If you need to test the "recheck" feature then switch to the production API proxy server.
102+
if (!CONFIG.IS_USING_LOCAL_WEB) {
103+
// Calling NetInfo.configure (re)checks current state. We use it to force a recheck whenever we (re)subscribe
104+
NetInfo.configure({
105+
// By default, NetInfo uses `/` for `reachabilityUrl`
106+
// When App is served locally (or from Electron) this address is always reachable - even offline
107+
// Using the API url ensures reachability is tested over internet
108+
reachabilityUrl: `${CONFIG.EXPENSIFY.DEFAULT_API_ROOT}api/Ping?accountID=${accountID || 'unknown'}`,
109+
reachabilityMethod: 'GET',
110+
reachabilityTest: (response) => {
126111
if (!response.ok) {
127112
return Promise.resolve(false);
128113
}
129114
return response
130115
.json()
131116
.then((json) => Promise.resolve(json.jsonCode === 200))
132117
.catch(() => Promise.resolve(false));
133-
})
134-
.then((isBackendReachable: boolean) => {
135-
if (isBackendReachable) {
136-
NetworkActions.setIsBackendReachable(true, 'successfully completed API request');
137-
return;
138-
}
139-
NetworkActions.setIsBackendReachable(false, 'request succeeded, but internet reachability test failed');
140-
checkInternetReachability().then((isInternetReachable: boolean) => {
141-
setOfflineStatus(!isInternetReachable, 'checkInternetReachability was called after api/ping returned a non-200 jsonCode');
142-
setNetWorkStatus(isInternetReachable);
143-
});
144-
})
145-
.catch(() => {
146-
NetworkActions.setIsBackendReachable(false, 'request failed and internet reachability test failed');
147-
checkInternetReachability().then((isInternetReachable: boolean) => {
148-
setOfflineStatus(!isInternetReachable, 'checkInternetReachability was called after api/ping request failed');
149-
setNetWorkStatus(isInternetReachable);
150-
});
151-
});
152-
}, CONST.NETWORK.BACKEND_CHECK_INTERVAL_MS);
153-
154-
return () => {
155-
clearInterval(intervalID);
156-
};
157-
}
118+
},
158119

159-
/**
160-
* Monitor internet connectivity and perform periodic backend reachability checks
161-
* @returns unsubscribe method
162-
*/
163-
function subscribeToNetworkStatus(): () => void {
164-
// Note: We are disabling the reachability check when using the local web API since requests can get stuck in a 'Pending' state and are not reliable indicators for reachability.
165-
// If you need to test the "recheck" feature then switch to the production API proxy server.
166-
const unsubscribeFromBackendReachability = !CONFIG.IS_USING_LOCAL_WEB ? subscribeToBackendAndInternetReachability() : undefined;
120+
// If a check is taking longer than this time we're considered offline
121+
reachabilityRequestTimeout: CONST.NETWORK.MAX_PENDING_TIME_MS,
122+
});
123+
}
167124

168-
// Set up the event listener for NetInfo to tell whether the user has
169-
// internet connectivity or not. This is more reliable than the Pusher
170-
// `disconnected` event which takes about 10-15 seconds to emit.
125+
// Subscribe to the state change event via NetInfo so we can update
126+
// whether a user has internet connectivity or not.
171127
const unsubscribeNetInfo = NetInfo.addEventListener((state) => {
172128
Log.info('[NetworkConnection] NetInfo state change', false, {...state});
173129
if (shouldForceOffline) {
@@ -176,11 +132,27 @@ function subscribeToNetworkStatus(): () => void {
176132
}
177133
setOfflineStatus(state.isInternetReachable === false, 'NetInfo received a state change event');
178134
Log.info(`[NetworkStatus] NetInfo.addEventListener event coming, setting "offlineStatus" to ${!!state.isInternetReachable} with network state: ${JSON.stringify(state)}`);
179-
setNetWorkStatus(state.isInternetReachable);
135+
let networkStatus;
136+
if (!isBoolean(state.isInternetReachable)) {
137+
networkStatus = CONST.NETWORK.NETWORK_STATUS.UNKNOWN;
138+
} else {
139+
networkStatus = state.isInternetReachable ? CONST.NETWORK.NETWORK_STATUS.ONLINE : CONST.NETWORK.NETWORK_STATUS.OFFLINE;
140+
}
141+
NetworkActions.setNetWorkStatus(networkStatus);
180142
});
181143

144+
// Periodically recheck the network connection
145+
// More info: https://github.com/Expensify/App/issues/42988
146+
const recheckIntervalID = setInterval(() => {
147+
if (!isOffline) {
148+
return;
149+
}
150+
recheckNetworkConnection();
151+
Log.info(`[NetworkStatus] Rechecking the network connection with "isOffline" set to "true" to double-check internet reachability.`);
152+
}, CONST.NETWORK.RECHECK_INTERVAL_MS);
153+
182154
return () => {
183-
unsubscribeFromBackendReachability?.();
155+
clearInterval(recheckIntervalID);
184156
unsubscribeNetInfo();
185157
};
186158
}
@@ -231,6 +203,6 @@ export default {
231203
onReconnect,
232204
triggerReconnectionCallbacks,
233205
recheckNetworkConnection,
234-
subscribeToNetworkStatus,
206+
subscribeToNetInfo,
235207
};
236208
export type {NetworkStatus};

src/libs/actions/Network.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,6 @@ import Log from '@libs/Log';
33
import type {NetworkStatus} from '@libs/NetworkConnection';
44
import ONYXKEYS from '@src/ONYXKEYS';
55

6-
function setIsBackendReachable(isBackendReachable: boolean, reason: string) {
7-
if (isBackendReachable) {
8-
Log.info(`[Network] Backend is reachable because: ${reason}`);
9-
} else {
10-
Log.info(`[Network] Backend is not reachable because: ${reason}`);
11-
}
12-
Onyx.merge(ONYXKEYS.NETWORK, {isBackendReachable});
13-
}
14-
156
function setIsOffline(isOffline: boolean, reason = '') {
167
if (reason) {
178
let textToLog = '[Network] Client is';
@@ -41,4 +32,4 @@ function setShouldFailAllRequests(shouldFailAllRequests: boolean) {
4132
Onyx.merge(ONYXKEYS.NETWORK, {shouldFailAllRequests});
4233
}
4334

44-
export {setIsBackendReachable, setIsOffline, setShouldForceOffline, setShouldFailAllRequests, setTimeSkew, setNetWorkStatus};
35+
export {setIsOffline, setShouldForceOffline, setShouldFailAllRequests, setTimeSkew, setNetWorkStatus};

src/libs/checkInternetReachability/index.android.ts

-15
This file was deleted.

src/libs/checkInternetReachability/index.ts

-5
This file was deleted.

src/libs/checkInternetReachability/types.ts

-3
This file was deleted.

src/types/onyx/Network.ts

-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ type Network = {
55
/** Is the network currently offline or not */
66
isOffline: boolean;
77

8-
/** Is the backend reachable when online */
9-
isBackendReachable: boolean;
10-
118
/** Should the network be forced offline */
129
shouldForceOffline?: boolean;
1310

0 commit comments

Comments
 (0)