Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
nkdengineer committed Feb 6, 2025
1 parent 72b9ea7 commit ed37d21
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/hooks/useNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ type UseNetworkProps = {
onReconnect?: () => void;
};

type UseNetwork = {isOffline: boolean};
type UseNetwork = {isOffline: boolean; lastOfflineAt?: Date};

export default function useNetwork({onReconnect = () => {}}: UseNetworkProps = {}): UseNetwork {
const callback = useRef(onReconnect);
// eslint-disable-next-line react-compiler/react-compiler
callback.current = onReconnect;

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

// If the network status is undefined, we don't treat it as offline. Otherwise, we utilize the isOffline prop.
return {isOffline: networkStatus === CONST.NETWORK.NETWORK_STATUS.UNKNOWN ? false : isOffline};
return {isOffline: networkStatus === CONST.NETWORK.NETWORK_STATUS.UNKNOWN ? false : isOffline, lastOfflineAt};
}
8 changes: 2 additions & 6 deletions src/hooks/useNetworkWithOfflineStatus.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import type {MutableRefObject} from 'react';
import {useEffect, useRef} from 'react';
import {useOnyx} from 'react-native-onyx';
import DateUtils from '@libs/DateUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import useLocalize from './useLocalize';
import useNetwork from './useNetwork';
import usePrevious from './usePrevious';

type UseNetworkWithOfflineStatus = {isOffline: boolean; lastOfflineAt: MutableRefObject<Date | undefined>; lastOnlineAt: MutableRefObject<Date | undefined>};

export default function useNetworkWithOfflineStatus(): UseNetworkWithOfflineStatus {
const {isOffline} = useNetwork();
const {isOffline, lastOfflineAt: lastOfflineAtFromOnyx} = useNetwork();
const prevIsOffline = usePrevious(isOffline);
const {preferredLocale} = useLocalize();

const [network] = useOnyx(ONYXKEYS.NETWORK);

// The last time/date the user went/was offline. If the user was never offline, it is set to undefined.
const lastOfflineAt = useRef(isOffline ? network?.lastOfflineAt : undefined);
const lastOfflineAt = useRef(isOffline ? lastOfflineAtFromOnyx : undefined);

// The last time/date the user went/was online. If the user was never online, it is set to undefined.
const lastOnlineAt = useRef(isOffline ? undefined : DateUtils.getLocalDateFromDatetime(preferredLocale));
Expand Down

0 comments on commit ed37d21

Please sign in to comment.