Skip to content
30 changes: 30 additions & 0 deletions src/hooks/useDeferUntilScreenRevealed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type {Primitive} from 'type-fest';

import {useState} from 'react';

import useIsScreenFocused from './useIsScreenFocused';
import useResponsiveLayout from './useResponsiveLayout';

/**
* Keeps a screen showing what the user last saw while the RHP covers part of it.
* Returns the live value while the screen has focus, and the value from when it last had focus otherwise,
* so changes made from the RHP show once it closes instead of shifting the layout visible beside it.
* Only primitives are accepted, since a value rebuilt on every render would never match the stored one and would set state on every render.
*/
function useDeferUntilScreenRevealed<TValue extends Primitive>(value: TValue): TValue {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WojtekBoman is this a new pattern or could this hook be used elsewhere? curious that we dont have similar behavior in app already

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s a new pattern. Usually we want the screen next to the RHP to update live, so this hasn’t come up much. Here, saving the onboarding removes the banner, which makes the page jump while the RHP is still open. The hook keeps the old value until the RHP closes. It’s generic, so it could be reused if the same case comes up somewhere else :)

const isFocused = useIsScreenFocused();
const {shouldUseNarrowLayout} = useResponsiveLayout();
const [deferredValue, setDeferredValue] = useState(value);

// On narrow layout the RHP covers the whole screen, so changes can apply while it is hidden.
// Holding them would make the screen jump as the user navigates back to it.
const shouldUseLiveValue = isFocused || shouldUseNarrowLayout;

if (shouldUseLiveValue && !Object.is(deferredValue, value)) {
setDeferredValue(value);
}

return shouldUseLiveValue ? value : deferredValue;
}

export default useDeferUntilScreenRevealed;
15 changes: 0 additions & 15 deletions src/hooks/useDismissWiseGlobalReimbursementsBanners.ts

This file was deleted.

19 changes: 19 additions & 0 deletions src/libs/BankAccountUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,24 @@ function isWiseOnboardingSubmitted(accountData: AccountData | undefined): boolea
return !!accountData?.additionalData?.wise?.status;
}

/**
* Whether the admin saved part of the Wise onboarding for any of their bank accounts. The server sets Wise as the provider on the first saved page,
* and from then on Wallet shows the account's setup so the admin can return to it.
* An account deleted offline still counts until the server removes it, so the banners don't show up before the deletion goes through.
*/
function hasStartedWiseOnboarding(bankAccountList: OnyxEntry<OnyxTypes.BankAccountList>): boolean {
return Object.values(bankAccountList ?? {}).some((bankAccount) => bankAccount?.accountData?.additionalData?.globalReimbursementProvider === CONST.GLOBAL_REIMBURSEMENT_PROVIDER.WISE);
}

/**
* Picks the bank account the Wallet global reimbursements banner points to: the most recently added eligible account.
*/
function getWiseGlobalReimbursementsBannerBankAccountID(bankAccountList: OnyxEntry<OnyxTypes.BankAccountList>): number | undefined {
// Once any account has a Wise setup, Wallet shows where it stands, so the banner has nothing left to promote
if (hasStartedWiseOnboarding(bankAccountList)) {
return undefined;
Comment on lines +307 to +308

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Ignore entry-click dismissals from the old trigger

For users who pressed Get started before this version and backed out before saving, the old handler has already persisted both banner entries with dismissedMethod: 'click'. Although this new gate will now find their eligible account, WalletWiseGlobalReimbursementsBanner.tsx:23-35 and WorkspaceWiseGlobalReimbursementsBanner.tsx:31-45 still treat any persisted entry as dismissed, and a repo-wide search shows no reset or migration for these names. Those existing users therefore still never see either banner; ignore or clear legacy click entries while retaining explicit x dismissals.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not addressing this one: the feature is behind the WISE_REIMBURSEMENTS beta, so we don’t need to migrate dismissals saved by the old trigger. QA can use a fresh account or clear nvp_dismissedProductTraining.

}

const newest = Object.values(bankAccountList ?? {}).reduce<AccountData | undefined>((latest, bankAccount) => {
// Accounts deleted offline stay in the list until the request syncs
if (bankAccount.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
Expand Down Expand Up @@ -334,6 +348,11 @@ function getWorkspaceWiseGlobalReimbursementsBannerBankAccountID(policy: OnyxEnt
return undefined;
}

// Once any account has a Wise setup, Wallet shows where it stands, so the banner has nothing left to promote
if (hasStartedWiseOnboarding(bankAccountList)) {
return undefined;
}

// Admins without a copy of the account can't see its currency, type or verifications, so the banner stays hidden for them
const bankAccount = bankAccountList?.[bankAccountID];
if (bankAccount?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || !isBankAccountEligibleForGlobalReimbursements(bankAccount?.accountData, bankAccount?.bankCurrency)) {
Expand Down
16 changes: 1 addition & 15 deletions src/libs/actions/WiseOnboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import {getMicroSecondOnyxErrorWithTranslationKey} from '@libs/ErrorUtils';
import getWiseOnboardingPageParams from '@libs/getWiseOnboardingPageParams';
import {rand64} from '@libs/NumberUtils';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {DynamicFormPage} from '@src/types/onyx';
import type {OnyxData} from '@src/types/onyx/Request';

import Onyx from 'react-native-onyx';

import {openReimbursementAccountPage} from './BankAccounts';
import {dismissProductTraining} from './Welcome';

/**
* Marks the page as unsaved until its own save succeeds, since `errors` is shared by every page and the next page's save clears it. It also covers a save
Expand Down Expand Up @@ -118,11 +116,6 @@ function clearWiseIntroReadBankAccountID() {
Onyx.set(ONYXKEYS.RAM_ONLY_WISE_INTRO_READ_BANK_ACCOUNT_ID, null);
}

function dismissWiseGlobalReimbursementsBanners() {
dismissProductTraining(CONST.WISE_GLOBAL_REIMBURSEMENTS_WALLET_BANNER);
dismissProductTraining(CONST.WISE_GLOBAL_REIMBURSEMENTS_WORKSPACE_BANNER);
}

/** Submits the business to Wise with the answers its pages saved. Auth creates the Wise profile and opens Wise's review in the same request. */
function submitWiseBusinessOnboarding(params: SubmitWiseBusinessOnboardingParams) {
const onyxData: OnyxData<typeof ONYXKEYS.REIMBURSEMENT_ACCOUNT> = {
Expand Down Expand Up @@ -165,11 +158,4 @@ function clearWiseBusinessOnboardingSubmission() {
}

export default saveWiseOnboardingPage;
export {
clearWiseIntroReadBankAccountID,
clearWiseBusinessOnboardingSubmission,
clearWiseOnboardingUnsavedPages,
dismissWiseGlobalReimbursementsBanners,
openReimbursementAccountPageForWiseIntro,
submitWiseBusinessOnboarding,
};
export {clearWiseIntroReadBankAccountID, clearWiseBusinessOnboardingSubmission, clearWiseOnboardingUnsavedPages, openReimbursementAccountPageForWiseIntro, submitWiseBusinessOnboarding};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import Header from '@components/Header';
import ScreenWrapper from '@components/ScreenWrapper';

import useDismissWiseGlobalReimbursementsBanners from '@hooks/useDismissWiseGlobalReimbursementsBanners';
import useDynamicBackPath from '@hooks/useDynamicBackPath';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
Expand Down Expand Up @@ -66,6 +65,10 @@ function WiseBusinessOnboardingPage({route}: WiseBusinessOnboardingPageProps) {
const [businessAccount, businessAccountMetadata] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {selector: businessAccountSelector});
const {isSaving, saveError, unsavedPageKeys} = useWiseOnboardingSaveStatus();
const bankAccount = bankAccountList?.[bankAccountID];
// A selector, so the check is cached now that only the early return below reads it
const [isEligible = false] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST, {
selector: (list) => isBankAccountEligibleForGlobalReimbursements(list?.[bankAccountID]?.accountData, list?.[bankAccountID]?.bankCurrency),
});

const {translate} = useLocalize();
const {isOffline} = useNetwork();
Expand Down Expand Up @@ -187,10 +190,6 @@ function WiseBusinessOnboardingPage({route}: WiseBusinessOnboardingPageProps) {
setIsPrefillDone(true);
}

// Starting onboarding for any account retires both banners for good, and the writes wait for the form so the read is not queued behind them
const isEligible = isBankAccountEligibleForGlobalReimbursements(bankAccount?.accountData, bankAccount?.bankCurrency);
useDismissWiseGlobalReimbursementsBanners(isNewVisit && isPrefillDone && isEligible);

useEffect(() => {
if (!isPrefillPending) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import WiseGlobalReimbursementsBanner from '@components/WiseGlobalReimbursementsBanner';

import useDeferUntilScreenRevealed from '@hooks/useDeferUntilScreenRevealed';
import useLayoutSpacing from '@hooks/useLayoutSpacing';
import useOnyx from '@hooks/useOnyx';
import usePermissions from '@hooks/usePermissions';
Expand Down Expand Up @@ -30,7 +31,8 @@ function WalletWiseGlobalReimbursementsBanner() {
const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST);
const [isDismissed = true] = useOnyx(ONYXKEYS.NVP_DISMISSED_PRODUCT_TRAINING, {selector: isBannerDismissedSelector});

const bankAccountID = getWiseGlobalReimbursementsBannerBankAccountID(bankAccountList);
// Saving the first onboarding page in the RHP retires the banner, so it waits for the RHP to close instead of vanishing behind it
const bankAccountID = useDeferUntilScreenRevealed(getWiseGlobalReimbursementsBannerBankAccountID(bankAccountList));

if (!isBetaEnabled(CONST.BETAS.WISE_REIMBURSEMENTS) || shouldSuppressPromotionalUI || isDismissed || !bankAccountID) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import type {SettingsNavigatorParamList} from '@navigation/types';

import variables from '@styles/variables';

import {dismissWiseGlobalReimbursementsBanners} from '@userActions/WiseOnboarding';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {DYNAMIC_ROUTES} from '@src/ROUTES';
Expand Down Expand Up @@ -112,13 +110,10 @@ function WiseGlobalReimbursementsIntroPage({route}: WiseGlobalReimbursementsIntr
}
text={translate(isOnboardingSubmitted || isWiseOnboardingPendingSubmit(bankAccount) ? 'common.continue' : 'walletPage.wiseGlobalReimbursementsIntro.getStarted')}
onPress={() => {
// A submitted business goes to its status page, which leaves the banners alone, so they are dismissed here. The form dismisses them itself
if (isOnboardingSubmitted) {
dismissWiseGlobalReimbursementsBanners();
Navigation.navigate(createDynamicRoute(DYNAMIC_ROUTES.WISE_ONBOARDING_STATUS.path));
return;
}
Navigation.navigate(createDynamicRoute(DYNAMIC_ROUTES.WISE_BUSINESS_ONBOARDING.getRoute()));
// A submitted business has nothing left to submit, so it sees where its onboarding stands instead of ending on the terms
Navigation.navigate(
createDynamicRoute(isOnboardingSubmitted ? DYNAMIC_ROUTES.WISE_ONBOARDING_STATUS.path : DYNAMIC_ROUTES.WISE_BUSINESS_ONBOARDING.getRoute()),
);
}}
/>
</FixedFooter>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import WiseGlobalReimbursementsBanner from '@components/WiseGlobalReimbursementsBanner';

import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useDeferUntilScreenRevealed from '@hooks/useDeferUntilScreenRevealed';
import useOnyx from '@hooks/useOnyx';
import usePermissions from '@hooks/usePermissions';
import usePolicy from '@hooks/usePolicy';
Expand Down Expand Up @@ -40,7 +41,8 @@ function WorkspaceWiseGlobalReimbursementsBanner({policyID}: WorkspaceWiseGlobal
const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST);
const [isDismissed = true] = useOnyx(ONYXKEYS.NVP_DISMISSED_PRODUCT_TRAINING, {selector: isBannerDismissedSelector});

const bankAccountID = getWorkspaceWiseGlobalReimbursementsBannerBankAccountID(policy, bankAccountList);
// Saving the first onboarding page in the RHP retires the banner, so it waits for the RHP to close instead of vanishing behind it
const bankAccountID = useDeferUntilScreenRevealed(getWorkspaceWiseGlobalReimbursementsBannerBankAccountID(policy, bankAccountList));

if (!isBetaEnabled(CONST.BETAS.WISE_REIMBURSEMENTS) || !isPolicyAdmin(policy, currentUserLogin) || shouldSuppressPromotionalUI || isDismissed || !bankAccountID) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,46 +475,19 @@ describe('WiseBusinessOnboardingPage', () => {
expect(screen.getByTestId(FLOW_TEST_ID)).toBeOnTheScreen();
});

it('retires the Wallet and workspace banners once the form shows, not while the loader waits for the read', async () => {
it('leaves the Wallet and workspace banners alone when the form shows, since they hide once a page is saved', async () => {
// Given a visit whose read of the bank account details has not returned
await renderPage({shouldLeaveReadRunning: true});
expect(screen.getByTestId(LOADER_TEST_ID)).toBeOnTheScreen();

// Then neither banner is dismissed yet, since a write queued ahead of the read would hold the loader
expect(await readDismissedProductTraining()).toBeUndefined();

// When the read returns and the form shows
await act(() => Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {isLoading: true}));
await waitForBatchedUpdatesWithAct();
await act(() => Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {...REIMBURSEMENT_ACCOUNT, isLoading: false}));
await waitForBatchedUpdatesWithAct();
expect(screen.getByTestId(FLOW_TEST_ID)).toBeOnTheScreen();

// Then both banners are dismissed, since onboarding has started
const dismissed = await readDismissedProductTraining();
expect(dismissed?.[CONST.WISE_GLOBAL_REIMBURSEMENTS_WALLET_BANNER]).toMatchObject({dismissedMethod: 'click'});
expect(dismissed?.[CONST.WISE_GLOBAL_REIMBURSEMENTS_WORKSPACE_BANNER]).toMatchObject({dismissedMethod: 'click'});
});

it('does not retire the banners again when Next or Back mounts the page on one of its pages', async () => {
// Given the details read earlier in the visit
// When the page mounts on the business page, as Next does
await renderPage({reimbursementAccount: REIMBURSEMENT_ACCOUNT, subPage: 'business'});
expect(screen.getByTestId(FLOW_TEST_ID)).toBeOnTheScreen();

// Then no dismiss is written, since the visit already did when its form opened
expect(await readDismissedProductTraining()).toBeUndefined();
});

it('does not retire the banners for a bank account that cannot use global reimbursements', async () => {
// Given a business account in euros, which Wise reimbursements do not cover
const euroBankAccount: BankAccount = {...ELIGIBLE_BANK_ACCOUNT, bankCurrency: CONST.CURRENCY.EUR};

// When the page opens for it
await renderPage({bankAccount: euroBankAccount});

// Then onboarding has not started, so both banners stay
expect(screen.getByTestId(NOT_FOUND_TEST_ID)).toBeOnTheScreen();
// Then neither banner is dismissed, so an admin who leaves before saving still sees them
expect(await readDismissedProductTraining()).toBeUndefined();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,29 +137,28 @@ describe('WiseGlobalReimbursementsIntroPage', () => {

it.each([
[
'retires both banners when Continue opens the status page of a submitted onboarding',
'leaves the banners alone when Continue opens the status page of a submitted onboarding',
CONST.GLOBAL_REIMBURSEMENT_PROVIDER.WISE,
{status: CONST.WISE.ONBOARDING_STATUS.IN_REVIEW},
true,
],
['leaves the banners to the form when Get started opens it', undefined, undefined, false],
])('%s', async (_label, globalReimbursementProvider, wise, shouldDismiss) => {
['leaves the banners alone when Get started opens the form', undefined, undefined],
])('%s', async (_label, globalReimbursementProvider, wise) => {
// Given the intro for a bank account at some point of its Wise onboarding
await renderPage(createBusinessBankAccount(globalReimbursementProvider, wise));

// When the admin presses the intro's button
fireEvent.press(screen.getByTestId(PRIMARY_BUTTON_TEST_ID));
await waitForBatchedUpdatesWithAct();

// Then only the status page path dismisses them here, since the form dismisses them itself once it opens
// Then neither banner is dismissed, since they hide on their own once any account has a saved Wise setup
let dismissedProductTraining: OnyxEntry<DismissedProductTraining>;
await TestHelper.getOnyxData({
key: ONYXKEYS.NVP_DISMISSED_PRODUCT_TRAINING,
callback: (value) => {
dismissedProductTraining = value;
},
});
expect(!!dismissedProductTraining?.[CONST.WISE_GLOBAL_REIMBURSEMENTS_WALLET_BANNER]).toBe(shouldDismiss);
expect(!!dismissedProductTraining?.[CONST.WISE_GLOBAL_REIMBURSEMENTS_WORKSPACE_BANNER]).toBe(shouldDismiss);
expect(dismissedProductTraining?.[CONST.WISE_GLOBAL_REIMBURSEMENTS_WALLET_BANNER]).toBeUndefined();
expect(dismissedProductTraining?.[CONST.WISE_GLOBAL_REIMBURSEMENTS_WORKSPACE_BANNER]).toBeUndefined();
});
});
Loading
Loading