Skip to content
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
5 changes: 5 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,11 @@ const CONST = {
PROCESS_REQUEST_DELAY_MS: 1000,
MAX_PENDING_TIME_MS: 10 * 1000,
MAX_REQUEST_RETRIES: 10,
NETWORK_STATUS: {
ONLINE: 'online',
OFFLINE: 'offline',
UNKNOWN: 'unknown',
},
},
WEEK_STARTS_ON: 1, // Monday
DEFAULT_TIME_ZONE: {automatic: true, selected: 'America/Los_Angeles'},
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default function useNetwork({onReconnect = () => {}}: UseNetworkProps = {
const callback = useRef(onReconnect);
callback.current = onReconnect;

const {isOffline} = useContext(NetworkContext) ?? CONST.DEFAULT_NETWORK_DATA;
const {networkStatus} = useContext(NetworkContext) ?? {...CONST.DEFAULT_NETWORK_DATA, networkStatus: CONST.NETWORK.NETWORK_STATUS.UNKNOWN};
const isOffline = networkStatus === CONST.NETWORK.NETWORK_STATUS.OFFLINE;
const prevOfflineStatusRef = useRef(isOffline);
useEffect(() => {
// If we were offline before and now we are not offline then we just reconnected
Expand All @@ -29,5 +30,5 @@ export default function useNetwork({onReconnect = () => {}}: UseNetworkProps = {
prevOfflineStatusRef.current = isOffline;
}, [isOffline]);

return {isOffline: isOffline ?? false};
return {isOffline};
}
11 changes: 11 additions & 0 deletions src/libs/NetworkConnection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import NetInfo from '@react-native-community/netinfo';
import {isBoolean} from 'lodash';
import throttle from 'lodash/throttle';
import Onyx from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import CONFIG from '@src/CONFIG';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand All @@ -10,6 +12,7 @@ import Log from './Log';

let isOffline = false;
let hasPendingNetworkCheck = false;
type NetworkStatus = ValueOf<typeof CONST.NETWORK.NETWORK_STATUS>;

// Holds all of the callbacks that need to be triggered when the network reconnects
let callbackID = 0;
Expand Down Expand Up @@ -107,6 +110,13 @@ function subscribeToNetInfo(): void {
return;
}
setOfflineStatus((state.isInternetReachable ?? false) === false);
let networkStatus;
if (!isBoolean(state.isInternetReachable)) {
networkStatus = CONST.NETWORK.NETWORK_STATUS.UNKNOWN;
} else {
networkStatus = state.isInternetReachable ? CONST.NETWORK.NETWORK_STATUS.ONLINE : CONST.NETWORK.NETWORK_STATUS.OFFLINE;
}
NetworkActions.setNetWorkStatus(networkStatus);
});
}

Expand Down Expand Up @@ -158,3 +168,4 @@ export default {
recheckNetworkConnection,
subscribeToNetInfo,
};
export type {NetworkStatus};
7 changes: 6 additions & 1 deletion src/libs/actions/Network.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import Onyx from 'react-native-onyx';
import type {NetworkStatus} from '@libs/NetworkConnection';
import ONYXKEYS from '@src/ONYXKEYS';

function setIsOffline(isOffline: boolean) {
Onyx.merge(ONYXKEYS.NETWORK, {isOffline});
}

function setNetWorkStatus(status: NetworkStatus) {
Onyx.merge(ONYXKEYS.NETWORK, {networkStatus: status});
}

function setTimeSkew(skew: number) {
Onyx.merge(ONYXKEYS.NETWORK, {timeSkew: skew});
}
Expand All @@ -20,4 +25,4 @@ function setShouldFailAllRequests(shouldFailAllRequests: boolean) {
Onyx.merge(ONYXKEYS.NETWORK, {shouldFailAllRequests});
}

export {setIsOffline, setShouldForceOffline, setShouldFailAllRequests, setTimeSkew};
export {setIsOffline, setShouldForceOffline, setShouldFailAllRequests, setTimeSkew, setNetWorkStatus};
5 changes: 4 additions & 1 deletion src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import * as ErrorUtils from '@libs/ErrorUtils';
import Log from '@libs/Log';
import * as LoginUtils from '@libs/LoginUtils';
import Navigation from '@libs/Navigation/Navigation';
import type {NetworkStatus} from '@libs/NetworkConnection';
import LocalNotification from '@libs/Notification/LocalNotification';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import * as PhoneNumber from '@libs/PhoneNumber';
Expand Down Expand Up @@ -208,10 +209,12 @@ Onyx.connect({
});

let isNetworkOffline = false;
let networkStatus: NetworkStatus;
Onyx.connect({
key: ONYXKEYS.NETWORK,
callback: (value) => {
isNetworkOffline = value?.isOffline ?? false;
networkStatus = value?.networkStatus ?? CONST.NETWORK.NETWORK_STATUS.UNKNOWN;
},
});

Expand Down Expand Up @@ -2406,7 +2409,7 @@ function openReportFromDeepLink(url: string) {
openReport(reportID, '', [], {}, '0', true);

// Show the sign-in page if the app is offline
if (isNetworkOffline) {
if (networkStatus === CONST.NETWORK.NETWORK_STATUS.OFFLINE) {
Onyx.set(ONYXKEYS.IS_CHECKING_PUBLIC_ROOM, false);
}
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/types/onyx/Network.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type {NetworkStatus} from '@libs/NetworkConnection';

type Network = {
/** Is the network currently offline or not */
isOffline: boolean;
Expand All @@ -10,6 +12,9 @@ type Network = {

/** Skew between the client and server clocks */
timeSkew?: number;

/** The network's status */
networkStatus?: NetworkStatus;
};

export default Network;