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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import variables from '@styles/variables';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';

import React, {useCallback, useMemo} from 'react';
import React from 'react';
import {View} from 'react-native';

function WorkspaceOwnerRestrictedAction() {
Expand All @@ -26,11 +26,18 @@ function WorkspaceOwnerRestrictedAction() {
const styles = useThemeStyles();
const expensifyIcons = useMemoizedLazyExpensifyIcons(['Unlock']);

const activeRoute = useMemo(() => Navigation.getActiveRoute(), []);
const goToSubscription = useCallback(() => {
Navigation.closeRHPFlow();
Navigation.navigate(ROUTES.SETTINGS_SUBSCRIPTION.getRoute(activeRoute));
}, [activeRoute]);
const goToSubscription = () => {
// Read the active route at press time rather than snapshotting it at mount. This screen stays

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.

❌ CLEAN-REACT-PATTERNS-0 (docs)

React Compiler is enabled in this codebase and automatically memoizes closures based on their captured variables. The PR's own compliance table reports this component passes the React Compiler check, so the manual useCallback wrapper here is redundant. Now that the dependency array is [] and the active route is read at press time, the closure captures nothing, making the wrapper pure overhead.

Remove the useCallback and use a plain function so the compiler can handle memoization:

const goToSubscription = () => {
    // Read the active route at press time ...
    Navigation.navigate(ROUTES.SETTINGS_SUBSCRIPTION.getRoute(Navigation.getActiveRoute()));
};

You can then drop the useCallback import from the react import statement.


Reviewed at: f13831b | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

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.

Agreed, fixed in d4f41e4. goToSubscription is a plain function now and the useCallback import is gone from the react import.

The suggestion is right about why: once the active route moved to press time, the closure captures nothing and the dependency array is empty, so the wrapper was doing nothing the compiler doesn't already do.

Verified per the rule's prerequisite before applying — react-compiler-compliance-check check on this file reports no failures, so the file does compile and the rule applies.

// mounted underneath the Subscription page, so a mount-time snapshot can be taken while
// Subscription is the focused route, which would make `backTo` point at Subscription itself.
// `linkTo` then drops the navigation as a no-op and the button stops working after a swipe back.

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.

❌ CONSISTENCY-15 (docs)

Comments should read as plain, natural sentences without stylistic tics like em dashes. This comment uses an em dash (—) to join two clauses.

Split it into two plain sentences:

// ...would make `backTo` point at Subscription itself.
// `linkTo` then drops the navigation as a no-op and the button stops working after a swipe back.

Reviewed at: f13831b | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

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.

Fixed in d4f41e4, split into two plain sentences:

// Subscription is the focused route, which would make `backTo` point at Subscription itself.
// `linkTo` then drops the navigation as a no-op and the button stops working after a swipe back.

// A press can only happen while this screen is on top, so reading it here always resolves to
// the restricted-action route.
//
// Don't close the RHP here either. Doing so pops this screen off the root stack, so the native
// swipe-back gesture lands on whatever was underneath instead of returning here.
Navigation.navigate(ROUTES.SETTINGS_SUBSCRIPTION.getRoute(Navigation.getActiveRoute()));
};

return (
<ScreenWrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
import ONYXKEYS from '@src/ONYXKEYS';
import type SCREENS from '@src/SCREENS';

import {useIsFocused} from '@react-navigation/native';
import React, {useEffect, useRef} from 'react';

import WorkspaceAdminRestrictedAction from './WorkspaceAdminRestrictedAction';
Expand All @@ -35,6 +36,7 @@ function WorkspaceRestrictedActionPage({
const policy = usePolicy(policyID);
const styles = useThemeStyles();
const [isLoadingSubscriptionData] = useOnyx(ONYXKEYS.IS_LOADING_SUBSCRIPTION_DATA);
const isFocused = useIsFocused();

// Watch billing NVPs so the component re-renders when fresh data arrives from the server.
const [userBillingGracePeriods] = useOnyx(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END);
Expand All @@ -49,36 +51,46 @@ function WorkspaceRestrictedActionPage({
gracePeriodsRef.current = userBillingGracePeriods;
}, [userBillingGracePeriods]);

const {isOffline} = useNetwork({
onReconnect: () => openSubscriptionPage(gracePeriodsRef.current),
});

// Fetch fresh billing NVPs from the server on mount.
// The cached billing data may be stale, causing the restriction to persist
// even after the workspace owner has resolved their billing issue.
// Skip when offline since the API call won't go through and the optimistic
// clear would incorrectly lift the restriction.
const {isOffline} = useNetwork();

// Fetch fresh billing NVPs from the server on mount, and again whenever this screen regains focus or the
// device reconnects. The cached billing data may be stale, causing the restriction to persist even after
// the workspace owner has resolved their billing issue.
//
// Skip when offline since the API call won't go through and the optimistic clear would incorrectly lift
// the restriction. Skip while unfocused too: this screen stays mounted underneath the Subscription page,
// which runs the same fetch itself, so firing ours as well would duplicate the request and flip the
// shared `isLoadingSubscriptionData` flag out from under the focused page.
//
// A `useNetwork({onReconnect})` callback is deliberately not used here. `isOffline` flipping back to
// false already re-runs this effect on reconnect, so the callback only fired the same request a second
// time. It also fired outside this focus guard.
useEffect(() => {
if (isOffline) {
if (isOffline || !isFocused) {
return;
}
openSubscriptionPage(gracePeriodsRef.current);
}, [isOffline]);
}, [isOffline, isFocused]);

// Navigate back if the fresh server data shows the restriction no longer applies.
// Only do this while focused, since this screen stays mounted underneath the Subscription page.
// Resolving the billing issue there would otherwise pop the Subscription page out from under the user.
useEffect(() => {
if (isLoadingSubscriptionData !== false) {
if (isLoadingSubscriptionData !== false || !isFocused) {
return;
}
if (!shouldRestrictUserBillableActions(policy, ownerBillingGracePeriodEnd, userBillingGracePeriods, amountOwed, accountID)) {
Navigation.goBack();
}
}, [policy, isLoadingSubscriptionData, userBillingGracePeriods, ownerBillingGracePeriodEnd, amountOwed, accountID]);
}, [policy, isLoadingSubscriptionData, userBillingGracePeriods, ownerBillingGracePeriodEnd, amountOwed, accountID, isFocused]);

// Show a loading indicator while waiting for fresh billing data from the server,
// instead of flashing the restriction UI which may no longer apply.
// Skip the loading indicator when offline since the API call won't go through.
if (isLoadingSubscriptionData !== false && !isOffline) {
// Also skip it while unfocused: this screen stays mounted underneath the Subscription page, whose
// own fetch toggles the same global loading flag. Swapping the content out here would needlessly
// unmount and remount the restriction UI behind the user's back, discarding its state.
if (isLoadingSubscriptionData !== false && !isOffline && isFocused) {
return <FullScreenLoadingIndicator style={styles.opacity1} />;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ import React from 'react';
// Jest resolves index.native.tsx by default in the RN test environment; load web implementation explicitly.
const {default: WorkspaceOwnerRestrictedActionWeb} = jest.requireActual<{default: React.ComponentType}>('@src/pages/RestrictedAction/Workspace/WorkspaceOwnerRestrictedAction/index.tsx');

// Held in a variable so tests can change the active route between render and press, which is what
// happens on device when this screen is covered by the Subscription page.
const mockGetActiveRoute = jest.fn(() => 'r/123');

jest.mock('@libs/Navigation/Navigation', () => ({
dismissModal: jest.fn(),
closeRHPFlow: jest.fn(),
navigate: jest.fn(),
getActiveRoute: jest.fn(() => 'r/123'),
getActiveRoute: () => mockGetActiveRoute(),
goBack: jest.fn(),
}));

Expand Down Expand Up @@ -113,6 +117,7 @@ function getDismissModalAfterTransition(): (() => void) | undefined {
describe('WorkspaceOwnerRestrictedAction', () => {
beforeEach(() => {
jest.clearAllMocks();
mockGetActiveRoute.mockReturnValue('r/123');
});

it('dismisses modal before navigating to add payment card on web', () => {
Expand All @@ -132,14 +137,36 @@ describe('WorkspaceOwnerRestrictedAction', () => {
expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.SETTINGS_SUBSCRIPTION_ADD_PAYMENT_CARD);
});

it('uses unchanged native handler to navigate to subscription route', () => {
it('navigates to subscription without closing the RHP so swiping back returns to the restricted screen', () => {
// Given the native restricted action screen, which is rendered inside the RHP
render(<WorkspaceOwnerRestrictedActionNative />);

// When the owner taps "Go to Subscription"
fireEvent.press(screen.getByText('workspace.restrictedAction.goToSubscription'));

expect(Navigation.closeRHPFlow).toHaveBeenCalledTimes(1);
// Then the RHP must stay on the root stack. Closing it pops this screen off the stack, and the
// native swipe-back gesture would then reveal whatever was underneath instead of coming back here.
expect(Navigation.closeRHPFlow).not.toHaveBeenCalled();
expect(Navigation.navigate).toHaveBeenCalledTimes(1);
expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.SETTINGS_SUBSCRIPTION.getRoute('r/123'));
expect(Navigation.dismissModal).not.toHaveBeenCalled();
});

it('resolves backTo at press time so the button still works after swiping back from subscription', () => {
// Given this screen mounting while the Subscription page is the focused route. That really happens:
// the screen stays mounted underneath Subscription, and Subscription's own billing fetch toggles the
// shared loading flag that swaps this screen's content out and back in while it is covered.
mockGetActiveRoute.mockReturnValue('settings/subscription?backTo=r%2F123');
render(<WorkspaceOwnerRestrictedActionNative />);

// When the user swipes back, re-focusing this screen, and taps "Go to Subscription" again
mockGetActiveRoute.mockReturnValue('r/123');
fireEvent.press(screen.getByText('workspace.restrictedAction.goToSubscription'));

// Then backTo must be the restricted-action route, not Subscription. A mount-time snapshot of the
// active route could be captured while Subscription was on top, making backTo self-referential.
// `linkTo` discards such a navigation as a no-op and the button silently stops working.
expect(Navigation.navigate).toHaveBeenCalledTimes(1);
expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.SETTINGS_SUBSCRIPTION.getRoute('r/123'));
});
});
Loading
Loading