diff --git a/src/CONST/index.ts b/src/CONST/index.ts index 431f3a56b04b..424a029cc777 100644 --- a/src/CONST/index.ts +++ b/src/CONST/index.ts @@ -10247,7 +10247,7 @@ const CONST = { WORKSPACE_EXPENSIFY_CARDS: 'workspaceExpensifyCards', WORKSPACE_COMPANY_CARDS: 'workspaceCompanyCards', - /** Prefix of the Search table IDs, one per data type. See `getSearchColumnResizingID`. */ + /** Prefix of the Search table IDs, one per search. See `getSearchColumnResizingID`. */ SEARCH_PREFIX: 'search', }, diff --git a/src/components/Search/SearchListViewLayout.tsx b/src/components/Search/SearchListViewLayout.tsx index 2c1203d8323f..e71e28f5bfdf 100644 --- a/src/components/Search/SearchListViewLayout.tsx +++ b/src/components/Search/SearchListViewLayout.tsx @@ -8,7 +8,7 @@ import useStyleUtils from '@hooks/useStyleUtils'; import useThemeStyles from '@hooks/useThemeStyles'; import {canMeasureText} from '@libs/measureTextWidth'; -import {getSearchColumnResizingID} from '@libs/SearchUIUtils'; +import {getSearchColumnResizingID} from '@libs/SearchKeyUtils'; import type {GetReportTableColumnStylesParams} from '@styles/utils'; import variables from '@styles/variables'; @@ -30,6 +30,7 @@ import useSearchColumnWidths from './hooks/useSearchColumnWidths'; import useSearchTableWidth from './hooks/useSearchTableWidth'; import HorizontalTableScroll from './primitives/HorizontalTableScroll'; import {SearchColumnWidthsProvider} from './SearchColumnWidthsContext'; +import {useSearchQueryContext} from './SearchContext'; type SearchListViewLayoutProps = { /** Columns rendered in the table (drives the min-width for horizontal scroll). */ @@ -227,6 +228,7 @@ function SearchListViewLayoutWithStoredColumnWidths(props: Omit ); } diff --git a/src/libs/SearchKeyUtils.ts b/src/libs/SearchKeyUtils.ts index a26e5e7baae1..c69fc1c0b5d2 100644 --- a/src/libs/SearchKeyUtils.ts +++ b/src/libs/SearchKeyUtils.ts @@ -41,9 +41,18 @@ function getSearchKeyForDataType(type: SearchDataTypes | undefined): SearchKey | return type ? DATA_TYPE_TO_SEARCH_KEY[type] : undefined; } +/** + * One stored layout per search, kept across filter changes. Includes the data type, since a saved search can be edited + * to another type with other columns. Queries matching no search share their data type's layout. + */ +function getSearchColumnResizingID(type: SearchDataTypes, searchKey: SearchKey | undefined): string { + const dataTypeResizingID = `${CONST.TABLES.COLUMN_RESIZING_IDS.SEARCH_PREFIX}:${type}`; + return searchKey ? `${dataTypeResizingID}:${searchKey}` : dataTypeResizingID; +} + function isReportEntry(key: string): key is ReportKey { return key.startsWith(ONYXKEYS.COLLECTION.REPORT); } -export {getSearchKeyForDataType, isExistingSearchKey, isReportEntry, savedSearchIDToSearchKey, searchKeyToSavedSearchID, TODO_SEARCH_KEYS}; +export {getSearchColumnResizingID, getSearchKeyForDataType, isExistingSearchKey, isReportEntry, savedSearchIDToSearchKey, searchKeyToSavedSearchID, TODO_SEARCH_KEYS}; export type {ReportKey, SearchKey}; diff --git a/src/libs/SearchUIUtils.ts b/src/libs/SearchUIUtils.ts index fdda01c8763a..53ec1f5d1880 100644 --- a/src/libs/SearchUIUtils.ts +++ b/src/libs/SearchUIUtils.ts @@ -7509,14 +7509,6 @@ function getSearchTableRowChromeWidths(columnCount: number): RowChromeWidths { }; } -/** - * One stored layout per data type, since each shows its own columns. Grouped tables don't resize, so the group-by isn't - * part of it. Not per query, so a filter change keeps the widths. - */ -function getSearchColumnResizingID(type: SearchDataTypes): string { - return `${CONST.TABLES.COLUMN_RESIZING_IDS.SEARCH_PREFIX}:${type}`; -} - function getTableMinWidth( columns: SearchColumnType[], type?: SearchDataTypes, @@ -8018,7 +8010,6 @@ export { getSearchColumnTranslationKey, getSearchTableRowInsetWidth, getSearchTableRowChromeWidths, - getSearchColumnResizingID, getTableMinWidth, getGroupColumnWidthFlags, getGroupTableScrollLayout, diff --git a/src/libs/actions/Search.ts b/src/libs/actions/Search.ts index 5ad9dd79219c..4640778384ce 100644 --- a/src/libs/actions/Search.ts +++ b/src/libs/actions/Search.ts @@ -68,7 +68,7 @@ import { isIOUReport as isIOUReportUtil, } from '@libs/ReportUtils'; import type {SearchKey} from '@libs/SearchKeyUtils'; -import {isReportEntry, savedSearchIDToSearchKey, TODO_SEARCH_KEYS} from '@libs/SearchKeyUtils'; +import {getSearchColumnResizingID, isReportEntry, savedSearchIDToSearchKey, TODO_SEARCH_KEYS} from '@libs/SearchKeyUtils'; import {buildSearchQueryJSON, buildSearchQueryString, getFooterSelectionFromQuery, normalizeDatePresetsInQuery, serializeQueryJSONForBackend} from '@libs/SearchQueryUtils'; import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils'; import {cancelSpan, endSpan, startSpan} from '@libs/telemetry/activeSpans'; @@ -1012,7 +1012,10 @@ function deleteSavedSearch(savedSearchID: string) { }, }, ]; - const successData: Array> = [ + const searchKey = savedSearchIDToSearchKey(savedSearchID); + // The saved query may have been edited across data types, and each type keeps its own widths. + const tableColumnWidths = Object.fromEntries(Object.values(CONST.SEARCH.DATA_TYPES).map((type) => [getSearchColumnResizingID(type, searchKey), null])); + const successData: Array> = [ { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.SAVED_SEARCHES, @@ -1024,9 +1027,14 @@ function deleteSavedSearch(savedSearchID: string) { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.SEARCH_FILTERS, value: { - [savedSearchIDToSearchKey(savedSearchID)]: null, + [searchKey]: null, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.TABLE_COLUMN_WIDTHS, + value: tableColumnWidths, + }, ]; const failureData: Array> = [ { diff --git a/tests/unit/Search/ChatSearchViewTest.tsx b/tests/unit/Search/ChatSearchViewTest.tsx index 5875316a1ccf..b59e4e894f7a 100644 --- a/tests/unit/Search/ChatSearchViewTest.tsx +++ b/tests/unit/Search/ChatSearchViewTest.tsx @@ -104,6 +104,7 @@ const mockSelectedTransactions: {current: Record} jest.mock('@components/Search/SearchContext', () => ({ useSearchRowSelectionActions: () => ({toggle: mockToggle, toggleAll: mockToggleAll}), useSearchSelectionContext: () => ({selectedTransactions: mockSelectedTransactions.current}), + useSearchQueryContext: () => ({currentSearchKey: undefined}), })); function selectKeys(...keys: string[]): Record { diff --git a/tests/unit/Search/ExpenseFlatSearchViewTest.tsx b/tests/unit/Search/ExpenseFlatSearchViewTest.tsx index b19b808d06a4..9e12fbf88aec 100644 --- a/tests/unit/Search/ExpenseFlatSearchViewTest.tsx +++ b/tests/unit/Search/ExpenseFlatSearchViewTest.tsx @@ -105,6 +105,7 @@ const mockSelectedTransactions: {current: Record} jest.mock('@components/Search/SearchContext', () => ({ useSearchRowSelectionActions: () => ({toggle: mockToggle, toggleAll: mockToggleAll}), useSearchSelectionContext: () => ({selectedTransactions: mockSelectedTransactions.current}), + useSearchQueryContext: () => ({currentSearchKey: undefined}), })); function selectKeys(...keys: string[]): Record { diff --git a/tests/unit/Search/ExpenseGroupedSearchViewSplitTest.tsx b/tests/unit/Search/ExpenseGroupedSearchViewSplitTest.tsx index 760b73d92e2e..24c36e282dd3 100644 --- a/tests/unit/Search/ExpenseGroupedSearchViewSplitTest.tsx +++ b/tests/unit/Search/ExpenseGroupedSearchViewSplitTest.tsx @@ -111,6 +111,7 @@ jest.mock('@components/Search/SearchContext', () => ({ useSearchRowSelectionActions: () => ({toggle: jest.fn(), toggleAll: jest.fn()}), useSearchSelectionContext: () => ({selectedTransactions: {}}), useSearchShiftRangeGroups: () => mockShiftRangeGroups, + useSearchQueryContext: () => ({currentSearchKey: undefined}), })); const STABLE_QUERY_JSON: SearchQueryJSON = { diff --git a/tests/unit/Search/ExpenseGroupedSearchViewTest.tsx b/tests/unit/Search/ExpenseGroupedSearchViewTest.tsx index 3f634a8f6476..53635e53dc70 100644 --- a/tests/unit/Search/ExpenseGroupedSearchViewTest.tsx +++ b/tests/unit/Search/ExpenseGroupedSearchViewTest.tsx @@ -118,6 +118,7 @@ jest.mock('@components/Search/SearchContext', () => ({ useSearchRowSelectionActions: () => ({toggle: mockToggle, toggleAll: mockToggleAll}), useSearchSelectionContext: () => ({selectedTransactions: mockSelectedTransactions.current, excludedTransactions: {}, areAllMatchingItemsSelected: false}), useSearchShiftRangeGroups: () => mockShiftRangeGroups, + useSearchQueryContext: () => ({currentSearchKey: undefined}), })); function selectKeys(...keys: string[]): Record { diff --git a/tests/unit/Search/ExpenseReportSearchViewTest.tsx b/tests/unit/Search/ExpenseReportSearchViewTest.tsx index 0414e9f8e374..386cc1b71f16 100644 --- a/tests/unit/Search/ExpenseReportSearchViewTest.tsx +++ b/tests/unit/Search/ExpenseReportSearchViewTest.tsx @@ -104,6 +104,7 @@ const mockSelectedTransactions: {current: Record} jest.mock('@components/Search/SearchContext', () => ({ useSearchRowSelectionActions: () => ({toggle: mockToggle, toggleAll: mockToggleAll}), useSearchSelectionContext: () => ({selectedTransactions: mockSelectedTransactions.current}), + useSearchQueryContext: () => ({currentSearchKey: undefined}), })); function selectKeys(...keys: string[]): Record { diff --git a/tests/unit/Search/SearchColumnResizingTest.ts b/tests/unit/Search/SearchColumnResizingTest.ts index c94d8a7b3c32..2c578c7eddfd 100644 --- a/tests/unit/Search/SearchColumnResizingTest.ts +++ b/tests/unit/Search/SearchColumnResizingTest.ts @@ -1,19 +1,48 @@ import {getAvailableColumnsWidth} from '@components/Table/columnResize/getResizableColumnLayout'; -import {getSearchColumnResizingID, getSearchTableRowChromeWidths, getSearchTableRowInsetWidth} from '@libs/SearchUIUtils'; +import {getSearchColumnResizingID, savedSearchIDToSearchKey} from '@libs/SearchKeyUtils'; +import {getSearchTableRowChromeWidths, getSearchTableRowInsetWidth} from '@libs/SearchUIUtils'; import CONST from '@src/CONST'; describe('getSearchColumnResizingID', () => { - it('stores every data type under its own ID', () => { - // Given the data types whose tables show different columns - const types = [CONST.SEARCH.DATA_TYPES.EXPENSE, CONST.SEARCH.DATA_TYPES.INVOICE, CONST.SEARCH.DATA_TYPES.TRIP, CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT]; + it('stores every search under its own ID', () => { + // Given the Expenses and Reports suggested searches, a to-do search, and a saved search + const searches = [ + {type: CONST.SEARCH.DATA_TYPES.EXPENSE, searchKey: CONST.SEARCH.SEARCH_KEYS.EXPENSES}, + {type: CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT, searchKey: CONST.SEARCH.SEARCH_KEYS.REPORTS}, + {type: CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT, searchKey: CONST.SEARCH.SEARCH_KEYS.APPROVE}, + {type: CONST.SEARCH.DATA_TYPES.EXPENSE, searchKey: savedSearchIDToSearchKey('123')}, + ]; // When each one's ID is built - const ids = types.map((type) => getSearchColumnResizingID(type)); + const ids = searches.map(({type, searchKey}) => getSearchColumnResizingID(type, searchKey)); - // Then no two share an entry, so resizing one table never overwrites another's widths - expect(ids).toEqual(['search:expense', 'search:invoice', 'search:trip', 'search:expense-report']); + // Then no two share an entry, so resizing one search never changes another's widths + expect(ids).toEqual(['search:expense:expenses', 'search:expense-report:reports', 'search:expense-report:approve', 'search:expense:savedSearch_123']); + }); + + it('keeps a search edited to another data type apart from its original columns', () => { + // Given a saved search that was edited from expenses to reports + const searchKey = savedSearchIDToSearchKey('123'); + + // When the ID is built for each type + const expenseID = getSearchColumnResizingID(CONST.SEARCH.DATA_TYPES.EXPENSE, searchKey); + const reportID = getSearchColumnResizingID(CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT, searchKey); + + // Then the report columns don't replay widths saved for the expense columns + expect(expenseID).not.toBe(reportID); + }); + + it('falls back to one ID per data type when no search matches the query', () => { + // Given queries that match no suggested or saved search + const types = [CONST.SEARCH.DATA_TYPES.INVOICE, CONST.SEARCH.DATA_TYPES.TASK]; + + // When each one's ID is built + const ids = types.map((type) => getSearchColumnResizingID(type, undefined)); + + // Then they share their data type's layout + expect(ids).toEqual(['search:invoice', 'search:task']); }); }); diff --git a/tests/unit/Search/SearchListViewLayoutTest.tsx b/tests/unit/Search/SearchListViewLayoutTest.tsx new file mode 100644 index 000000000000..ede1dae571d7 --- /dev/null +++ b/tests/unit/Search/SearchListViewLayoutTest.tsx @@ -0,0 +1,74 @@ +import {render} from '@testing-library/react-native'; + +import SearchListViewLayout from '@components/Search/SearchListViewLayout'; +import type {SearchColumnType} from '@components/Search/types'; +import useStoredColumnWidths from '@components/Table/columnResize/useStoredColumnWidths'; + +import type {SearchKey} from '@libs/SearchKeyUtils'; + +import CONST from '@src/CONST'; + +import React from 'react'; + +const mockSearchQueryContext: {currentSearchKey: SearchKey | undefined} = {currentSearchKey: undefined}; +jest.mock('@components/Search/SearchContext', () => ({ + useSearchQueryContext: () => mockSearchQueryContext, +})); + +jest.mock('@hooks/usePermissions', () => () => ({isBetaEnabled: () => true})); + +jest.mock('@libs/measureTextWidth', () => ({ + __esModule: true, + default: (text: string) => text.length * 10, + canMeasureText: () => true, +})); + +jest.mock('@components/Table/columnResize/useStoredColumnWidths', () => ({ + __esModule: true, + default: jest.fn(() => undefined), +})); + +const COLUMNS: SearchColumnType[] = [CONST.SEARCH.TABLE_COLUMNS.DATE, CONST.SEARCH.TABLE_COLUMNS.TITLE, CONST.SEARCH.TABLE_COLUMNS.TOTAL]; + +function renderLayoutFor(searchKey: SearchKey) { + mockSearchQueryContext.currentSearchKey = searchKey; + + render( + + {null} + , + ); +} + +describe('SearchListViewLayout', () => { + beforeEach(() => { + jest.mocked(useStoredColumnWidths).mockClear(); + mockSearchQueryContext.currentSearchKey = undefined; + }); + + it('reads the column widths saved for the current search, not for every search of its data type', () => { + // Given two searches with the same data type, so their tables show the same columns + const reportsSearchKey = CONST.SEARCH.SEARCH_KEYS.REPORTS; + const submitSearchKey = CONST.SEARCH.SEARCH_KEYS.SUBMIT; + + // When the table is rendered for each of them + renderLayoutFor(reportsSearchKey); + renderLayoutFor(submitSearchKey); + + // Then each one reads its own widths, so resizing a column on Reports leaves Submit untouched + expect(useStoredColumnWidths).toHaveBeenCalledWith('search:expense-report:reports'); + expect(useStoredColumnWidths).toHaveBeenCalledWith('search:expense-report:submit'); + expect(useStoredColumnWidths).not.toHaveBeenCalledWith('search:expense-report'); + }); +}); diff --git a/tests/unit/Search/TaskSearchViewTest.tsx b/tests/unit/Search/TaskSearchViewTest.tsx index bc82c989b973..e663c33f67ab 100644 --- a/tests/unit/Search/TaskSearchViewTest.tsx +++ b/tests/unit/Search/TaskSearchViewTest.tsx @@ -104,6 +104,7 @@ const mockSelectedTransactions: {current: Record} jest.mock('@components/Search/SearchContext', () => ({ useSearchRowSelectionActions: () => ({toggle: mockToggle, toggleAll: mockToggleAll}), useSearchSelectionContext: () => ({selectedTransactions: mockSelectedTransactions.current}), + useSearchQueryContext: () => ({currentSearchKey: undefined}), })); function selectKeys(...keys: string[]): Record { diff --git a/tests/unit/SearchActionsTest.ts b/tests/unit/SearchActionsTest.ts index e9407c055866..cbf0dddc91ca 100644 --- a/tests/unit/SearchActionsTest.ts +++ b/tests/unit/SearchActionsTest.ts @@ -178,10 +178,14 @@ describe('SearchActions', () => { }); describe('deleteSavedSearch', () => { - it('sends the optimistic DELETE, failure revert, and success removal keyed by the savedSearchID', () => { + it('sends the optimistic DELETE, failure revert, and success removal of the search and its column widths', () => { + // Given a saved search const savedSearchID = '987654321'; + + // When it's deleted deleteSavedSearch(savedSearchID); + // Then once the server confirms, its column widths go too, for every data type its query could have had expect(mockWrite).toHaveBeenCalledWith( WRITE_COMMANDS.DELETE_SAVED_SEARCH, {savedSearchID}, @@ -191,6 +195,18 @@ describe('SearchActions', () => { successData: [ {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.SAVED_SEARCHES, value: {[savedSearchID]: null}}, {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.SEARCH_FILTERS, value: {[savedSearchIDToSearchKey(savedSearchID)]: null}}, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.TABLE_COLUMN_WIDTHS, + value: { + [`search:expense:${savedSearchIDToSearchKey(savedSearchID)}`]: null, + [`search:expense-report:${savedSearchIDToSearchKey(savedSearchID)}`]: null, + [`search:invoice:${savedSearchIDToSearchKey(savedSearchID)}`]: null, + [`search:task:${savedSearchIDToSearchKey(savedSearchID)}`]: null, + [`search:trip:${savedSearchIDToSearchKey(savedSearchID)}`]: null, + [`search:chat:${savedSearchIDToSearchKey(savedSearchID)}`]: null, + }, + }, ], }, );