Skip to content

Commit b9137ee

Browse files
committed
General wrapper hook & dedicated hooks
1 parent 3d036ca commit b9137ee

14 files changed

Lines changed: 72 additions & 35 deletions

src/components/MoneyReportHeader.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import useNetwork from '@hooks/useNetwork';
1010
import usePaymentAnimations from '@hooks/usePaymentAnimations';
1111
import usePaymentOptions from '@hooks/usePaymentOptions';
1212
import usePermissions from '@hooks/usePermissions';
13-
import useReportDataLoading from '@hooks/useReportDataLoading';
13+
import useLoadingBarVisibility from '@hooks/useLoadingBarVisibility';
1414
import useReportIsArchived from '@hooks/useReportIsArchived';
1515
import useResponsiveLayout from '@hooks/useResponsiveLayout';
1616
import useSelectedTransactionsActions from '@hooks/useSelectedTransactionsActions';
@@ -298,7 +298,7 @@ function MoneyReportHeader({
298298
const isAnyTransactionOnHold = hasHeldExpensesReportUtils(moneyRequestReport?.reportID);
299299
const {isDelegateAccessRestricted} = useDelegateUserDetails();
300300
const [isNoDelegateAccessMenuVisible, setIsNoDelegateAccessMenuVisible] = useState(false);
301-
const shouldShowLoadingBar = useReportDataLoading();
301+
const shouldShowLoadingBar = useLoadingBarVisibility();
302302

303303
const isReportInRHP = route.name === SCREENS.SEARCH.REPORT_RHP;
304304
const shouldDisplaySearchRouter = !isReportInRHP || isSmallScreenWidth;

src/components/MoneyRequestHeader.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {useOnyx} from 'react-native-onyx';
77
import type {ValueOf} from 'type-fest';
88
import useLocalize from '@hooks/useLocalize';
99
import usePermissions from '@hooks/usePermissions';
10-
import useReportDataLoading from '@hooks/useReportDataLoading';
10+
import useLoadingBarVisibility from '@hooks/useLoadingBarVisibility';
1111
import useResponsiveLayout from '@hooks/useResponsiveLayout';
1212
import useTheme from '@hooks/useTheme';
1313
import useThemeStyles from '@hooks/useThemeStyles';
@@ -84,7 +84,7 @@ function MoneyRequestHeader({report, parentReportAction, policy, onBackButtonPre
8484
const [isDeleteModalVisible, setIsDeleteModalVisible] = useState(false);
8585
const [downloadErrorModalVisible, setDownloadErrorModalVisible] = useState(false);
8686
const [dismissedHoldUseExplanation, dismissedHoldUseExplanationResult] = useOnyx(ONYXKEYS.NVP_DISMISSED_HOLD_USE_EXPLANATION, {initialValue: true, canBeMissing: false});
87-
const shouldShowLoadingBar = useReportDataLoading();
87+
const shouldShowLoadingBar = useLoadingBarVisibility();
8888
const isLoadingHoldUseExplained = isLoadingOnyxValue(dismissedHoldUseExplanationResult);
8989
const styles = useThemeStyles();
9090
const theme = useTheme();

src/components/Navigation/TopBar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import SearchButton from '@components/Search/SearchRouter/SearchButton';
77
import HelpButton from '@components/SidePanel/HelpComponents/HelpButton';
88
import Text from '@components/Text';
99
import useLocalize from '@hooks/useLocalize';
10-
import useReportDataLoading from '@hooks/useReportDataLoading';
10+
import useLoadingBarVisibility from '@hooks/useLoadingBarVisibility';
1111
import useThemeStyles from '@hooks/useThemeStyles';
1212
import SignInButton from '@pages/home/sidebar/SignInButton';
1313
import {isAnonymousUser as isAnonymousUserUtil} from '@userActions/Session';
@@ -26,7 +26,7 @@ function TopBar({breadcrumbLabel, shouldDisplaySearch = true, shouldDisplayHelpB
2626
const styles = useThemeStyles();
2727
const {translate} = useLocalize();
2828
const [session] = useOnyx(ONYXKEYS.SESSION, {selector: (sessionValue) => sessionValue && {authTokenType: sessionValue.authTokenType}, canBeMissing: true});
29-
const shouldShowLoadingBarForReports = useReportDataLoading();
29+
const shouldShowLoadingBarForReports = useLoadingBarVisibility();
3030
const isAnonymousUser = isAnonymousUserUtil(session);
3131

3232
const displaySignIn = isAnonymousUser;

src/components/PriorityModeController.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import FocusModeNotification from './FocusModeNotification';
2525
*/
2626
export default function PriorityModeController() {
2727
const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.accountID, canBeMissing: true});
28-
const isLoadingReportData = useReportDataLoading(false);
28+
const isLoadingReportData = useReportDataLoading();
2929
const [isInFocusMode] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE, {selector: (priorityMode) => priorityMode === CONST.PRIORITY_MODE.GSD, canBeMissing: true});
3030
const [hasTriedFocusMode] = useOnyx(ONYXKEYS.NVP_TRY_FOCUS_MODE, {canBeMissing: true});
3131
const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: true});

src/hooks/useCommandsLoading.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {useMemo} from 'react';
2+
import ONYXKEYS from '@src/ONYXKEYS';
3+
import useOnyx from './useOnyx';
4+
5+
/**
6+
* Hook that determines if any of the specified commands are currently being processed
7+
*
8+
* Monitors persisted requests queue for the provided commands that are not initiated offline.
9+
*
10+
* @param commands - Array of command strings to monitor
11+
* @returns boolean indicating if any of the specified commands are currently loading
12+
*/
13+
export default function useCommandsLoading(commands: string[]): boolean {
14+
const [req] = useOnyx(ONYXKEYS.PERSISTED_REQUESTS, {canBeMissing: false});
15+
16+
const commandsSet = useMemo(() => new Set(commands), [commands]);
17+
18+
return req?.some((request) =>
19+
commandsSet.has(request.command) && !request.initiatedOffline
20+
) ?? false;
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {WRITE_COMMANDS} from '@libs/API/types';
2+
import ONYXKEYS from '@src/ONYXKEYS';
3+
import useCommandsLoading from './useCommandsLoading';
4+
import useOnyx from './useOnyx';
5+
6+
// Commands that should trigger loading bar visibility
7+
const LOADING_BAR_COMMANDS = [WRITE_COMMANDS.OPEN_APP, WRITE_COMMANDS.RECONNECT_APP, WRITE_COMMANDS.OPEN_REPORT];
8+
9+
/**
10+
* Hook that determines when the loading bar should be visible
11+
*
12+
* Monitors persisted requests queue for OpenApp, ReconnectApp, and OpenReport commands
13+
* that trigger report data fetching from the server. The loading bar is hidden when offline
14+
* to provide better UX since users can't interact with loading content while offline.
15+
*
16+
* @returns boolean indicating if the loading bar should be visible
17+
*/
18+
export default function useLoadingBarVisibility(): boolean {
19+
const [network] = useOnyx(ONYXKEYS.NETWORK, {canBeMissing: false});
20+
const hasRelevantCommands = useCommandsLoading(LOADING_BAR_COMMANDS);
21+
22+
// Loading bar should not be shown when offline since users can't interact with loading content
23+
if (network?.isOffline) {
24+
return false;
25+
}
26+
27+
return hasRelevantCommands;
28+
}

src/hooks/useReportDataLoading.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
import {WRITE_COMMANDS} from '@libs/API/types';
2-
import ONYXKEYS from '@src/ONYXKEYS';
3-
import useOnyx from './useOnyx';
2+
import useCommandsLoading from './useCommandsLoading';
43

5-
// Commands that should trigger loading states
6-
const RELEVANT_COMMANDS = new Set<string>([WRITE_COMMANDS.OPEN_APP, WRITE_COMMANDS.RECONNECT_APP, WRITE_COMMANDS.OPEN_REPORT]);
4+
// Commands that should trigger report data loading states
5+
const REPORT_DATA_COMMANDS = [WRITE_COMMANDS.OPEN_APP, WRITE_COMMANDS.RECONNECT_APP, WRITE_COMMANDS.OPEN_REPORT];
76

87
/**
98
* Hook that determines if report data is currently being loaded
109
*
1110
* Monitors persisted requests queue for OpenApp, ReconnectApp, and OpenReport commands
12-
* that trigger report data fetching from the server.
11+
* that trigger report data fetching from the server. This hook ignores offline state
12+
* and is primarily used for full-screen loading indicators.
1313
*
14-
* @param respectOfflineState - If true (default), won't show loading when offline (for loading bars).
15-
* If false, shows loading regardless of offline state (for full-screen loading).
14+
* @returns boolean indicating if report data is currently loading
1615
*/
17-
export default function useReportDataLoading(respectOfflineState = true): boolean {
18-
const [req] = useOnyx(ONYXKEYS.PERSISTED_REQUESTS, {canBeMissing: false});
19-
const [network] = useOnyx(ONYXKEYS.NETWORK, {canBeMissing: false});
20-
21-
const hasRelevantRequests = req?.some((request) => RELEVANT_COMMANDS.has(request.command) && !request.initiatedOffline) ?? false;
22-
23-
// For loading bars, respect offline state (don't show when offline)
24-
// For full-screen loading, ignore offline state
25-
if (respectOfflineState && network?.isOffline) {
26-
return false;
27-
}
28-
29-
return hasRelevantRequests;
16+
export default function useReportDataLoading(): boolean {
17+
return useCommandsLoading(REPORT_DATA_COMMANDS);
3018
}

src/pages/ConciergePage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function ConciergePage() {
2424
const styles = useThemeStyles();
2525
const isUnmounted = useRef(false);
2626
const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false});
27-
const isLoadingReportData = useReportDataLoading(false);
27+
const isLoadingReportData = useReportDataLoading();
2828
const route = useRoute();
2929

3030
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {canBeMissing: true});

src/pages/home/HeaderView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import Tooltip from '@components/Tooltip';
2626
import useHasTeam2025Pricing from '@hooks/useHasTeam2025Pricing';
2727
import useLocalize from '@hooks/useLocalize';
2828
import usePolicy from '@hooks/usePolicy';
29-
import useReportDataLoading from '@hooks/useReportDataLoading';
29+
import useLoadingBarVisibility from '@hooks/useLoadingBarVisibility';
3030
import useResponsiveLayout from '@hooks/useResponsiveLayout';
3131
import useSubscriptionPlan from '@hooks/useSubscriptionPlan';
3232
import useTheme from '@hooks/useTheme';
@@ -112,7 +112,7 @@ function HeaderView({report, parentReportAction, onNavigationMenuButtonClicked,
112112
const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(report?.parentReportID) ?? getNonEmptyStringOnyxID(report?.reportID)}`, {canBeMissing: true});
113113
const policy = usePolicy(report?.policyID);
114114
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {canBeMissing: true});
115-
const shouldShowLoadingBar = useReportDataLoading();
115+
const shouldShowLoadingBar = useLoadingBarVisibility();
116116
const [firstDayFreeTrial] = useOnyx(ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL, {canBeMissing: true});
117117
const [lastDayFreeTrial] = useOnyx(ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL, {canBeMissing: true});
118118
const [account] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: true});

src/pages/home/report/withReportAndReportActionOrNotFound.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default function <TProps extends WithReportAndReportActionOrNotFoundProps
4242
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
4343
const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`, {canBeMissing: true});
4444
const [reportMetadata] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${props.route.params.reportID}`, {canBeMissing: true});
45-
const isLoadingReportData = useReportDataLoading(false);
45+
const isLoadingReportData = useReportDataLoading();
4646
const [betas] = useOnyx(ONYXKEYS.BETAS, {canBeMissing: false});
4747
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
4848
const [reportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${props.route.params.reportID}`, {canEvict: false, canBeMissing: true});

0 commit comments

Comments
 (0)