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
3 changes: 3 additions & 0 deletions src/components/Search/SearchBulkActionsButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function SearchBulkActionsButton({queryJSON}: SearchBulkActionsButtonProps) {
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const {
headerButtonsOptions,
bulkActionsMenuHeaderText,
selectedPolicyIDs,
selectedTransactionReportIDs,
selectedReportIDs,
Expand Down Expand Up @@ -223,6 +224,7 @@ function SearchBulkActionsButton({queryJSON}: SearchBulkActionsButtonProps) {
<ButtonWithDropdownMenu
buttonRef={buttonRef}
options={headerButtonsOptions}
menuHeaderText={bulkActionsMenuHeaderText}
customText={selectionButtonText}
isLoading={isAllMatchingItemsCountLoading}
shouldAlwaysShowDropdownMenu
Expand Down Expand Up @@ -250,6 +252,7 @@ function SearchBulkActionsButton({queryJSON}: SearchBulkActionsButtonProps) {
customText={selectionButtonText}
isLoading={isAllMatchingItemsCountLoading}
options={headerButtonsOptions}
menuHeaderText={bulkActionsMenuHeaderText}
shouldPopoverUseScrollView={popoverUseScrollView}
onSubItemSelected={(subItem) => payBulkSelectedItem(subItem, triggerKYCFlow)}
isSplitButton={false}
Expand Down
74 changes: 47 additions & 27 deletions src/hooks/useSearchBulkActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,13 @@ function getChatReportForBulkPay(
return getReportFromSearchSnapshot(resolvedChatReportID, searchData, allReports) ?? getReportOrDraftReport(resolvedChatReportID);
}

/**
* A single export option. It is typed as a dropdown option rather than a plain menu item because it is rendered at
* either level of the bulk-action dropdown: nested inside the Export submenu, or directly at the top level when
* Export is the only bulk action available.
*/
type ExportMenuItem = DropdownOption<SearchHeaderOptionValue> & Pick<PopoverMenuItem, 'accessibilityLabel' | 'shouldCallAfterModalHide'>;

function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
const {translate, localeCompare} = useLocalize();
const styles = useThemeStyles();
Expand Down Expand Up @@ -1808,7 +1815,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
? defaultTemplates.filter((template) => template.templateName === CONST.REPORT.EXPORT_OPTIONS.DOWNLOAD_CSV)
: defaultTemplates;

const exportOptions: PopoverMenuItem[] = [];
const exportOptions: ExportMenuItem[] = [];

const isReportsTab = isExpenseReportType;
const includesGroupExport = isGroupedSearch && Object.entries(selectedTransactions).some(([key, selectedTransaction]) => isGroupSelection(key, selectedTransaction));
Expand Down Expand Up @@ -2019,6 +2026,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
const handleExportAction = buildIntegrationHandleExportAction(exportableReportIDs, integration, integrationGroupSize, true, connectionNameFriendly);
exportOptions.push({
text: connectionNameFriendly,
value: CONST.SEARCH.BULK_ACTION_TYPES.EXPORT,
icon: integrationIcon,
onSelected: () => handleExportAction(() => exportToIntegrationOnSearch(hash, exportableReportIDs, integration, integrationPolicy, currentSearchKey)),
shouldCloseModalOnSelect: true,
Expand All @@ -2036,6 +2044,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
const handleMarkAction = buildIntegrationHandleExportAction(reportIDsToMark, integration, integrationGroupSize, false, connectionNameFriendly);
exportOptions.push({
text: translate('workspace.common.markAsExported'),
value: CONST.SEARCH.BULK_ACTION_TYPES.EXPORT,
// Every integration's "Mark as exported" option shares the same visible text and differs only by icon,
// which screen readers can't announce. Append the integration name so assistive tech can distinguish them.
accessibilityLabel: `${translate('workspace.common.markAsExported')}, ${connectionNameFriendly}`,
Expand All @@ -2052,6 +2061,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
// "Current view" is pinned directly under the accounting actions.
exportOptions.push({
text: translate('export.currentView'),
value: CONST.SEARCH.BULK_ACTION_TYPES.EXPORT,
icon: expensifyIcons.Table,
onSelected: () => {
handleExportCurrentView();
Expand All @@ -2064,11 +2074,12 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {

if (!allSelectedAreDeleted && !includesGroupExport) {
// Builds a single export sub-menu item for a template. `isDefaultTemplate` picks the icon and `addSeparatorBefore` draws the divider at the top of each group.
const buildExportOption = (template: ExportTemplate, isDefaultTemplate: boolean, addSeparatorBefore: boolean): PopoverMenuItem => {
const buildExportOption = (template: ExportTemplate, isDefaultTemplate: boolean, addSeparatorBefore: boolean): ExportMenuItem => {
// The basic export is a plain CSV download, so it uses its own handler rather than the template export flow
const isBasicExport = template.templateName === CONST.REPORT.EXPORT_OPTIONS.DOWNLOAD_CSV;
return {
text: template.name,
value: CONST.SEARCH.BULK_ACTION_TYPES.EXPORT,
icon: isDefaultTemplate ? expensifyIcons.Table : expensifyIcons.TablePencil,
description: template.description,
onSelected: () => {
Expand All @@ -2095,6 +2106,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
// The templates aren't available for this selection, but the basic export (a plain CSV download) still is
exportOptions.push({
text: translate('export.basicExport'),
value: CONST.SEARCH.BULK_ACTION_TYPES.EXPORT,
icon: expensifyIcons.Table,
onSelected: () => {
handleBasicExport();
Expand All @@ -2110,32 +2122,32 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
};

const subMenuItems = getExportOptions();
const singleExportSubMenuItem = subMenuItems.length === 1 ? subMenuItems.at(0) : undefined;

const exportButtonOption: DropdownOption<SearchHeaderOptionValue> & Pick<PopoverMenuItem, 'rightIcon' | 'shouldCallAfterModalHide'> = singleExportSubMenuItem
? {
icon: expensifyIcons.Export,
text: singleExportSubMenuItem.text,
value: CONST.SEARCH.BULK_ACTION_TYPES.EXPORT,
shouldCloseModalOnSelect: singleExportSubMenuItem.shouldCloseModalOnSelect ?? true,
shouldCallAfterModalHide: singleExportSubMenuItem.shouldCallAfterModalHide,
onSelected: () => singleExportSubMenuItem.onSelected?.(),
description: singleExportSubMenuItem.description,
displayInDefaultIconColor: singleExportSubMenuItem.displayInDefaultIconColor,
additionalIconStyles: singleExportSubMenuItem.additionalIconStyles,
}
: {
icon: expensifyIcons.Export,
rightIcon: expensifyIcons.ArrowRight,
text: translate('common.export'),
backButtonText: translate('common.export'),
value: CONST.SEARCH.BULK_ACTION_TYPES.EXPORT,
shouldCloseModalOnSelect: true,
subMenuItems,
};

// Always open the Export submenu, even when there is only one option, so the user keeps the context that
// they are entering an export flow instead of being navigated straight into the single option.
const exportButtonOption: DropdownOption<SearchHeaderOptionValue> & Pick<PopoverMenuItem, 'rightIcon'> = {
icon: expensifyIcons.Export,
rightIcon: expensifyIcons.ArrowRight,
text: translate('common.export'),
backButtonText: translate('common.export'),
value: CONST.SEARCH.BULK_ACTION_TYPES.EXPORT,
shouldCloseModalOnSelect: true,
subMenuItems,
};

/**
* Export is sometimes the only bulk action available (most commonly under "select all"). There is no main menu
* to go back to then, so the dropdown opens directly onto the export options rather than nesting them behind
* an "Export" row whose submenu would render a back arrow leading nowhere. The "Export" label is not lost:
* `bulkActionsMenuHeaderText` below puts it back as a plain (non-interactive) header above the options.
*/
const openExportOptionsDirectlyIfSoleAction = (builtOptions: Array<DropdownOption<SearchHeaderOptionValue>>): Array<DropdownOption<SearchHeaderOptionValue>> => {
const isExportTheOnlyAction = builtOptions.length === 1 && builtOptions.at(0)?.value === CONST.SEARCH.BULK_ACTION_TYPES.EXPORT;
return isExportTheOnlyAction && subMenuItems.length > 0 ? subMenuItems : builtOptions;
};

if (areAllMatchingItemsSelected) {
return [exportButtonOption];
return openExportOptionsDirectlyIfSoleAction([exportButtonOption]);
}

if (allSelectedAreDeleted) {
Expand Down Expand Up @@ -2745,7 +2757,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
});
}

return options;
return openExportOptionsDirectlyIfSoleAction(options);
}, [
selectedTransactionsKeys,
hash,
Expand Down Expand Up @@ -2834,6 +2846,13 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
shouldCalculateTotalsOnRefresh,
]);

// When the export options are surfaced directly there is no "Export" row above them, so on its own the list gives
// no clue what the options refer to. Put "Export" back as a plain header — it reads the same as the label the back
// button normally carries, minus the caret, since there is no menu to go back to.
const isShowingExportOptionsDirectly =
headerButtonsOptions.length > 0 && headerButtonsOptions.every((option) => option.value === CONST.SEARCH.BULK_ACTION_TYPES.EXPORT && !option.subMenuItems);
const bulkActionsMenuHeaderText = isShowingExportOptionsDirectly ? translate('common.export') : undefined;

const handleOfflineModalClose = useCallback(() => {
setIsOfflineModalVisible(false);
}, [setIsOfflineModalVisible]);
Expand Down Expand Up @@ -2882,6 +2901,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {

return {
headerButtonsOptions,
bulkActionsMenuHeaderText,
selectedPolicyIDs,
selectedTransactionReportIDs,
selectedReportIDs,
Expand Down
101 changes: 87 additions & 14 deletions tests/unit/hooks/useSearchBulkActionsExportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,22 +420,27 @@ function makeSearchResults(reports: Report[], reportActionsByReportID: Record<st
};
}

function getExportSubMenuItems(headerButtonsOptions: ReturnType<typeof useSearchBulkActions>['headerButtonsOptions']) {
return headerButtonsOptions.find((option) => option.value === CONST.SEARCH.BULK_ACTION_TYPES.EXPORT)?.subMenuItems;
/**
* The export options take one of two shapes: normally they sit inside the Export entry's `subMenuItems`, but when
* Export is the only bulk action available the dropdown opens straight onto them, so they sit at the top level of
* `headerButtonsOptions` with the EXPORT value on each one.
*/
function getExportMenuItems(headerButtonsOptions: ReturnType<typeof useSearchBulkActions>['headerButtonsOptions']) {
const exportOptions = headerButtonsOptions.filter((option) => option.value === CONST.SEARCH.BULK_ACTION_TYPES.EXPORT);
return exportOptions.at(0)?.subMenuItems ?? exportOptions;
}

function getExportOptionTexts(headerButtonsOptions: ReturnType<typeof useSearchBulkActions>['headerButtonsOptions']) {
const exportOption = headerButtonsOptions.find((option) => option.value === CONST.SEARCH.BULK_ACTION_TYPES.EXPORT);
return exportOption?.subMenuItems?.map((item) => item.text) ?? (exportOption ? [exportOption.text] : []);
return getExportMenuItems(headerButtonsOptions).map((item) => item.text);
}

/** The export menu collapses into a single top-level option when it holds only one item, so look in both shapes. */
function getExportOptionByText(headerButtonsOptions: ReturnType<typeof useSearchBulkActions>['headerButtonsOptions'], text: string) {
const exportOption = headerButtonsOptions.find((option) => option.value === CONST.SEARCH.BULK_ACTION_TYPES.EXPORT);
if (!exportOption) {
return undefined;
}
return exportOption.subMenuItems?.find((item) => item.text === text) ?? (exportOption.text === text ? exportOption : undefined);
return getExportMenuItems(headerButtonsOptions).find((item) => item.text === text);
}

/** The Export entry's nested submenu items. Only valid for selections where Export is not the only bulk action. */
function getExportSubMenuItems(headerButtonsOptions: ReturnType<typeof useSearchBulkActions>['headerButtonsOptions']) {
return headerButtonsOptions.find((option) => option.value === CONST.SEARCH.BULK_ACTION_TYPES.EXPORT)?.subMenuItems;
}

/** The parameters the last plain-CSV export sent to the backend, with the serialized query parsed back. */
Expand Down Expand Up @@ -545,10 +550,9 @@ describe('useSearchBulkActions - export options', () => {
expect(result.current.headerButtonsOptions.find((option) => option.value === CONST.SEARCH.BULK_ACTION_TYPES.EXPORT)).toBeDefined();
});

const exportOption = result.current.headerButtonsOptions.find((option) => option.value === CONST.SEARCH.BULK_ACTION_TYPES.EXPORT);
const subMenuItems = exportOption?.subMenuItems ?? [];
expect(subMenuItems.some((item) => item.text === NETSUITE_FRIENDLY_NAME)).toBe(false);
expect(subMenuItems.some((item) => item.text === 'workspace.common.markAsExported')).toBe(false);
const exportMenuItems = getExportMenuItems(result.current.headerButtonsOptions);
expect(exportMenuItems.some((item) => item.text === NETSUITE_FRIENDLY_NAME)).toBe(false);
expect(exportMenuItems.some((item) => item.text === 'workspace.common.markAsExported')).toBe(false);
});

it('offers per-integration export options when reports span workspaces connected to different integrations', async () => {
Expand Down Expand Up @@ -1175,6 +1179,75 @@ describe('useSearchBulkActions - export options', () => {
expect(getExportOptionTexts(result.current.headerButtonsOptions)).not.toContain('export.basicExport');
});

it('keeps the Export entry as a submenu even when only one export option is available', async () => {
// Regression test for https://github.com/Expensify/App/issues/98779: a full group selection offers a single
// export option ('export.currentView'). While other bulk actions sit alongside it (Hold here), the Export
// entry must still open the Export submenu (keeping its generic label and subMenuItems) rather than
// collapsing straight into that single option.
mockSelectedTransactions = {
tx1: makeSelectedTransaction({
groupKey: `${CONST.SEARCH.GROUP_PREFIX}category`,
isSelectedViaGroup: true,
canHold: true,
}),
};

const {result} = renderHook(() => useSearchBulkActions({queryJSON: groupedExpenseQueryJSON}), {wrapper: OnyxListItemProvider});

await waitFor(() => {
expect(getExportOptionTexts(result.current.headerButtonsOptions)).toEqual(['export.currentView']);
});

const exportOption = result.current.headerButtonsOptions.find((option) => option.value === CONST.SEARCH.BULK_ACTION_TYPES.EXPORT);
expect(exportOption?.subMenuItems).toHaveLength(1);
expect(exportOption?.text).toBe('common.export');
// The Export row itself labels the submenu here, so the dropdown must not also carry an "Export" header.
expect(result.current.bulkActionsMenuHeaderText).toBeUndefined();
});

it('opens directly onto the single export option when Export is the only bulk action', async () => {
// Export is the only bulk action offered under select all, so there is no main menu to go back to. The one
// export option is surfaced directly instead of behind an "Export" row whose submenu would render a back
// arrow leading nowhere, with "Export" kept as a plain dropdown header so the option still has context.
mockAreAllMatchingItemsSelected = true;
mockSelectedTransactions = {
tx1: makeSelectedTransaction({
groupKey: `${CONST.SEARCH.GROUP_PREFIX}category`,
isSelectedViaGroup: true,
}),
};

const {result} = renderHook(() => useSearchBulkActions({queryJSON: groupedExpenseQueryJSON}), {wrapper: OnyxListItemProvider});

await waitFor(() => {
expect(result.current.headerButtonsOptions.map((option) => option.text)).toEqual(['export.currentView']);
});

const soleOption = result.current.headerButtonsOptions.at(0);
expect(soleOption?.value).toBe(CONST.SEARCH.BULK_ACTION_TYPES.EXPORT);
expect(soleOption?.subMenuItems).toBeUndefined();
expect(soleOption?.backButtonText).toBeUndefined();
expect(result.current.bulkActionsMenuHeaderText).toBe('common.export');
});

it('opens directly onto every export option when Export is the only bulk action', async () => {
mockAreAllMatchingItemsSelected = true;
mockSelectedTransactions = {tx1: makeSelectedTransaction()};

const {result} = renderHook(() => useSearchBulkActions({queryJSON: groupedExpenseQueryJSON}), {wrapper: OnyxListItemProvider});

await waitFor(() => {
expect(result.current.headerButtonsOptions.length).toBeGreaterThan(1);
});

// Every entry is an export option itself — there is no "Export" row wrapping them and so no back arrow.
expect(result.current.headerButtonsOptions.every((option) => option.value === CONST.SEARCH.BULK_ACTION_TYPES.EXPORT)).toBe(true);
expect(result.current.headerButtonsOptions.some((option) => option.text === 'common.export')).toBe(false);
expect(result.current.headerButtonsOptions.some((option) => !!option.subMenuItems)).toBe(false);
// "Export" moves to the dropdown header instead, so the options are still labeled without a back caret.
expect(result.current.bulkActionsMenuHeaderText).toBe('common.export');
});

it('exports the current view of a grouped search with the default expense columns', async () => {
mockSelectedTransactions = {tx1: makeSelectedTransaction()};

Expand Down
Loading
Loading