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
2 changes: 1 addition & 1 deletion src/CONST/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},

Expand Down
6 changes: 4 additions & 2 deletions src/components/Search/SearchListViewLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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). */
Expand Down Expand Up @@ -227,6 +228,7 @@ function SearchListViewLayoutWithStoredColumnWidths(props: Omit<SearchListViewLa
*/
function SearchListViewLayout(props: SearchListViewLayoutProps) {
const {isBetaEnabled} = usePermissions();
const {currentSearchKey} = useSearchQueryContext();

// Saved widths are replayed by content sizing, so only tables that size from their rows can resize.
// The header is left out so crossing the wide breakpoint doesn't remount the list.
Expand All @@ -246,7 +248,7 @@ function SearchListViewLayout(props: SearchListViewLayoutProps) {
return (
<SearchListViewLayoutWithStoredColumnWidths
{...props}
columnResizingID={getSearchColumnResizingID(props.type)}
columnResizingID={getSearchColumnResizingID(props.type, currentSearchKey)}
Comment thread
war-in marked this conversation as resolved.
Resolved
/>
);
}
Expand Down
11 changes: 10 additions & 1 deletion src/libs/SearchKeyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};
9 changes: 0 additions & 9 deletions src/libs/SearchUIUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -8018,7 +8010,6 @@ export {
getSearchColumnTranslationKey,
getSearchTableRowInsetWidth,
getSearchTableRowChromeWidths,
getSearchColumnResizingID,
getTableMinWidth,
getGroupColumnWidthFlags,
getGroupTableScrollLayout,
Expand Down
14 changes: 11 additions & 3 deletions src/libs/actions/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -1012,7 +1012,10 @@ function deleteSavedSearch(savedSearchID: string) {
},
},
];
const successData: Array<OnyxUpdate<typeof ONYXKEYS.SAVED_SEARCHES | typeof ONYXKEYS.SEARCH_FILTERS>> = [
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<OnyxUpdate<typeof ONYXKEYS.SAVED_SEARCHES | typeof ONYXKEYS.SEARCH_FILTERS | typeof ONYXKEYS.TABLE_COLUMN_WIDTHS>> = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.SAVED_SEARCHES,
Expand All @@ -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<OnyxUpdate<typeof ONYXKEYS.SAVED_SEARCHES>> = [
{
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Search/ChatSearchViewTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const mockSelectedTransactions: {current: Record<string, {isSelected: boolean}>}
jest.mock('@components/Search/SearchContext', () => ({
useSearchRowSelectionActions: () => ({toggle: mockToggle, toggleAll: mockToggleAll}),
useSearchSelectionContext: () => ({selectedTransactions: mockSelectedTransactions.current}),
useSearchQueryContext: () => ({currentSearchKey: undefined}),
}));

function selectKeys(...keys: string[]): Record<string, {isSelected: boolean}> {
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Search/ExpenseFlatSearchViewTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const mockSelectedTransactions: {current: Record<string, {isSelected: boolean}>}
jest.mock('@components/Search/SearchContext', () => ({
useSearchRowSelectionActions: () => ({toggle: mockToggle, toggleAll: mockToggleAll}),
useSearchSelectionContext: () => ({selectedTransactions: mockSelectedTransactions.current}),
useSearchQueryContext: () => ({currentSearchKey: undefined}),
}));

function selectKeys(...keys: string[]): Record<string, {isSelected: boolean}> {
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Search/ExpenseGroupedSearchViewSplitTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Search/ExpenseGroupedSearchViewTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, {isSelected: boolean}> {
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Search/ExpenseReportSearchViewTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const mockSelectedTransactions: {current: Record<string, {isSelected: boolean}>}
jest.mock('@components/Search/SearchContext', () => ({
useSearchRowSelectionActions: () => ({toggle: mockToggle, toggleAll: mockToggleAll}),
useSearchSelectionContext: () => ({selectedTransactions: mockSelectedTransactions.current}),
useSearchQueryContext: () => ({currentSearchKey: undefined}),
}));

function selectKeys(...keys: string[]): Record<string, {isSelected: boolean}> {
Expand Down
43 changes: 36 additions & 7 deletions tests/unit/Search/SearchColumnResizingTest.ts
Original file line number Diff line number Diff line change
@@ -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']);
});
});

Expand Down
74 changes: 74 additions & 0 deletions tests/unit/Search/SearchListViewLayoutTest.tsx
Original file line number Diff line number Diff line change
@@ -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(
<SearchListViewLayout
columns={COLUMNS}
type={CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT}
isActionColumnWide={false}
columnSizeOptions={{}}
isHeaderVisible
dataKey={undefined}
isKeyboardShown={false}
safeAreaPaddingBottomStyle={{}}
containerStyle={{}}
data={[]}
>
{null}
</SearchListViewLayout>,
);
}

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');
});
});
1 change: 1 addition & 0 deletions tests/unit/Search/TaskSearchViewTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const mockSelectedTransactions: {current: Record<string, {isSelected: boolean}>}
jest.mock('@components/Search/SearchContext', () => ({
useSearchRowSelectionActions: () => ({toggle: mockToggle, toggleAll: mockToggleAll}),
useSearchSelectionContext: () => ({selectedTransactions: mockSelectedTransactions.current}),
useSearchQueryContext: () => ({currentSearchKey: undefined}),
}));

function selectKeys(...keys: string[]): Record<string, {isSelected: boolean}> {
Expand Down
18 changes: 17 additions & 1 deletion tests/unit/SearchActionsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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,
},
},
],
},
);
Expand Down
Loading