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
@@ -1,24 +1,36 @@
import {emailSelector} from '@selectors/Session';
import React, {useCallback, useMemo, useRef, useState} from 'react';
import {View} from 'react-native';
import type {OnyxCollection} from 'react-native-onyx';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import Button from '@components/Button';
import {ModalActions} from '@components/Modal/Global/ModalContext';
import type {PopoverMenuItem} from '@components/PopoverMenu';
import PopoverMenu from '@components/PopoverMenu';
import useConfirmModal from '@hooks/useConfirmModal';
import useCreateEmptyReportConfirmation from '@hooks/useCreateEmptyReportConfirmation';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useHasEmptyReportsForPolicy from '@hooks/useHasEmptyReportsForPolicy';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import usePermissions from '@hooks/usePermissions';
import usePolicyForMovingExpenses from '@hooks/usePolicyForMovingExpenses';
import usePopoverPosition from '@hooks/usePopoverPosition';
import useThemeStyles from '@hooks/useThemeStyles';
import {startDistanceRequest, startMoneyRequest} from '@libs/actions/IOU';
import {openOldDotLink} from '@libs/actions/Link';
import {createNewReport} from '@libs/actions/Report';
import getIconForAction from '@libs/getIconForAction';
import interceptAnonymousUser from '@libs/interceptAnonymousUser';
import isSearchTopmostFullScreenRoute from '@libs/Navigation/helpers/isSearchTopmostFullScreenRoute';
import Navigation from '@libs/Navigation/Navigation';
import {generateReportID} from '@libs/ReportUtils';
import {areAllGroupPoliciesExpenseChatDisabled, getDefaultChatEnabledPolicy} from '@libs/PolicyUtils';
import {generateReportID, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import {groupPaidPoliciesWithExpenseChatEnabledSelector, shouldRedirectToExpensifyClassicSelector} from '@src/selectors/Policy';
import {groupPaidPoliciesWithExpenseChatEnabledSelector} from '@src/selectors/Policy';
import type * as OnyxTypes from '@src/types/onyx';

function SearchFiltersBarCreateButton() {
Expand All @@ -31,11 +43,80 @@ function SearchFiltersBarCreateButton() {
const [createMenuPosition, setCreateMenuPosition] = useState<{horizontal: number; vertical: number}>({horizontal: 0, vertical: 0});
const {calculatePopoverPosition} = usePopoverPosition();

const [session] = useOnyx(ONYXKEYS.SESSION);
const [email] = useOnyx(ONYXKEYS.SESSION, {selector: emailSelector});
const [allBetas] = useOnyx(ONYXKEYS.BETAS);
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);
const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
const groupPaidPoliciesWithChatEnabledSelector = useCallback((policies: OnyxCollection<OnyxTypes.Policy>) => groupPaidPoliciesWithExpenseChatEnabledSelector(policies, email), [email]);
const [groupPoliciesWithChatEnabled = CONST.EMPTY_ARRAY] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: groupPaidPoliciesWithChatEnabledSelector}, [email]);
const [shouldRedirectToExpensifyClassic = false] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: shouldRedirectToExpensifyClassicSelector});
const shouldShowCreateReportOption = shouldRedirectToExpensifyClassic || groupPoliciesWithChatEnabled.length > 0;
const currentUserPersonalDetails = useCurrentUserPersonalDetails();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@mukhrr I am reviewing this code and I can see most of this is similar/picked from the FloatingActionButtonAndPopver? Is this the correct solution?

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.

@mananjadhav I thought so since they almost do the same behave

@mananjadhav mananjadhav Mar 3, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I've posted the same question here, primarily about the duplication of the code and moving to another hook.

cc - @puneetlath

const {isBetaEnabled} = usePermissions();
const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT);
const hasViolations = hasViolationsReportUtils(undefined, transactionViolations, session?.accountID ?? CONST.DEFAULT_NUMBER_ID, session?.email ?? '');
const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID);
const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`);
const {policyForMovingExpensesID, shouldSelectPolicy} = usePolicyForMovingExpenses();
const shouldNavigateToUpgradePath = !policyForMovingExpensesID && !shouldSelectPolicy;
const {showConfirmModal} = useConfirmModal();

const shouldRedirectToExpensifyClassic = useMemo(() => {
return areAllGroupPoliciesExpenseChatDisabled((allPolicies as OnyxCollection<OnyxTypes.Policy>) ?? {});
}, [allPolicies]);

const defaultChatEnabledPolicy = useMemo(
() => getDefaultChatEnabledPolicy(groupPoliciesWithChatEnabled as Array<OnyxEntry<OnyxTypes.Policy>>, activePolicy),
[activePolicy, groupPoliciesWithChatEnabled],
);
const defaultChatEnabledPolicyID = defaultChatEnabledPolicy?.id;

const hasEmptyReport = useHasEmptyReportsForPolicy(defaultChatEnabledPolicyID);
const [hasDismissedEmptyReportsConfirmation] = useOnyx(ONYXKEYS.NVP_EMPTY_REPORTS_CONFIRMATION_DISMISSED);
const shouldShowEmptyReportConfirmationForDefaultChatEnabledPolicy = hasEmptyReport && hasDismissedEmptyReportsConfirmation !== true;

const showRedirectToExpensifyClassicModal = useCallback(async () => {
const {action} = await showConfirmModal({
title: translate('sidebarScreen.redirectToExpensifyClassicModal.title'),
prompt: translate('sidebarScreen.redirectToExpensifyClassicModal.description'),
confirmText: translate('exitSurvey.goToExpensifyClassic'),
cancelText: translate('common.cancel'),
});
if (action === ModalActions.CONFIRM) {
openOldDotLink(CONST.OLDDOT_URLS.INBOX);
}
}, [showConfirmModal, translate]);

const handleCreateWorkspaceReport = useCallback(
(shouldDismissEmptyReportsConfirmation?: boolean) => {
if (!defaultChatEnabledPolicy?.id) {
return;
}

const {reportID: createdReportID} = createNewReport(
currentUserPersonalDetails,
hasViolations,
isASAPSubmitBetaEnabled,
defaultChatEnabledPolicy,
allBetas,
false,
shouldDismissEmptyReportsConfirmation,
);
Navigation.setNavigationActionToMicrotaskQueue(() => {
Navigation.navigate(
isSearchTopmostFullScreenRoute()
? ROUTES.SEARCH_MONEY_REQUEST_REPORT.getRoute({reportID: createdReportID, backTo: Navigation.getActiveRoute()})
: ROUTES.REPORT_WITH_ID.getRoute(createdReportID, undefined, undefined, Navigation.getActiveRoute()),
);
});
},
[currentUserPersonalDetails, hasViolations, defaultChatEnabledPolicy, isASAPSubmitBetaEnabled, allBetas],
);

const {openCreateReportConfirmation, CreateReportConfirmationModal} = useCreateEmptyReportConfirmation({
policyID: defaultChatEnabledPolicyID,
policyName: defaultChatEnabledPolicy?.name ?? '',
onConfirm: handleCreateWorkspaceReport,
});

const hideCreateMenu = useCallback(() => setIsCreateMenuActive(false), []);
const showCreateMenu = useCallback(() => {
Expand Down Expand Up @@ -69,24 +150,72 @@ function SearchFiltersBarCreateButton() {
startDistanceRequest(CONST.IOU.TYPE.CREATE, generateReportID());
}),
},
...(shouldShowCreateReportOption
? [
{
icon: expensifyIcons.Document,
text: translate('report.newReport.createReport'),
onSelected: () =>
interceptAnonymousUser(() => {
Navigation.navigate(ROUTES.NEW_REPORT_WORKSPACE_SELECTION.getRoute());
}),
},
]
: []),
{
icon: expensifyIcons.Document,
text: translate('report.newReport.createReport'),
onSelected: () =>
interceptAnonymousUser(() => {
if (shouldRedirectToExpensifyClassic) {
showRedirectToExpensifyClassicModal();
return;
}

// No valid policy at all → upgrade + create workspace flow
if (shouldNavigateToUpgradePath) {
const freshReportID = generateReportID();
const freshTransactionID = generateReportID();
Navigation.navigate(
ROUTES.MONEY_REQUEST_UPGRADE.getRoute({
action: CONST.IOU.ACTION.CREATE,
iouType: CONST.IOU.TYPE.CREATE,
transactionID: freshTransactionID,
reportID: freshReportID,
upgradePath: CONST.UPGRADE_PATHS.REPORTS,
}),
);
return;
}

const workspaceIDForReportCreation = defaultChatEnabledPolicyID;

// No default or restricted with multiple workspaces → workspace selector
if (!workspaceIDForReportCreation || (shouldRestrictUserBillableActions(workspaceIDForReportCreation) && groupPoliciesWithChatEnabled.length > 1)) {
Navigation.navigate(ROUTES.NEW_REPORT_WORKSPACE_SELECTION.getRoute());
return;
}

// Default workspace is not restricted → create report directly
if (!shouldRestrictUserBillableActions(workspaceIDForReportCreation)) {
// Check if empty report confirmation should be shown
if (shouldShowEmptyReportConfirmationForDefaultChatEnabledPolicy) {
openCreateReportConfirmation();
} else {
handleCreateWorkspaceReport(false);
}
return;
}

Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(workspaceIDForReportCreation));
}),
},
],
[
translate,
expensifyIcons,
shouldRedirectToExpensifyClassic,
showRedirectToExpensifyClassicModal,
shouldNavigateToUpgradePath,
groupPoliciesWithChatEnabled.length,
defaultChatEnabledPolicyID,
shouldShowEmptyReportConfirmationForDefaultChatEnabledPolicy,
openCreateReportConfirmation,
handleCreateWorkspaceReport,
],
[translate, expensifyIcons, shouldShowCreateReportOption],
);

return (
<View style={[styles.pr5, styles.searchFiltersBarCreateButton]}>
{CreateReportConfirmationModal}
<PopoverMenu
onClose={hideCreateMenu}
isVisible={isCreateMenuActive}
Expand Down
7 changes: 6 additions & 1 deletion src/pages/iou/request/step/IOURequestStepUpgrade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ function IOURequestStepUpgrade({
}
case CONST.UPGRADE_PATHS.REPORTS:
Navigation.goBack();
navigateWithMicrotask(ROUTES.MONEY_REQUEST_STEP_REPORT.getRoute(action, CONST.IOU.TYPE.SUBMIT, transactionID, reportID));
if (action === CONST.IOU.ACTION.CREATE) {
// Coming from "Create report" button (no workspace) → go to workspace selection which creates the report
navigateWithMicrotask(ROUTES.NEW_REPORT_WORKSPACE_SELECTION.getRoute());
} else {
navigateWithMicrotask(ROUTES.MONEY_REQUEST_STEP_REPORT.getRoute(action, CONST.IOU.TYPE.SUBMIT, transactionID, reportID));
}

break;
case CONST.UPGRADE_PATHS.CATEGORIES:
Expand Down
Loading
Loading