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
42 changes: 37 additions & 5 deletions src/components/Search/ExpenseGroupedSearchView.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type {ExtendedTargetedEvent} from '@components/SelectionList/ListItem/types';

import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useWindowDimensions from '@hooks/useWindowDimensions';

import getPlatform from '@libs/getPlatform';
import {isTransactionGroupListItemType, isTransactionMatchWithGroupItem, splitGroupsIntoPairs} from '@libs/SearchUIUtils';
import {isTransactionPendingDelete} from '@libs/TransactionUtils';

import variables from '@styles/variables';

Expand All @@ -28,6 +30,7 @@ import SelectionTopBar from './primitives/SelectionTopBar';
import BaseSearchList from './SearchList/BaseSearchList';
import GroupChildrenContainer from './SearchList/ListItem/GroupChildrenContainer';
import GroupHeader from './SearchList/ListItem/GroupHeader';
import shouldCollapseExpandedGroupAfterPendingDelete from './SearchList/ListItem/shouldCollapseExpandedGroupAfterPendingDelete';
import TransactionGroupListItem from './SearchList/ListItem/TransactionGroupListItem';
import {isGroupChildrenContainerItem, isGroupHeaderItem} from './SearchList/ListItem/types';
import SearchListViewLayout from './SearchListViewLayout';
Expand All @@ -38,8 +41,6 @@ const keyExtractor = (item: SearchListItem, index: number) => item.keyForList ??

const isRowDeleted = (item: SearchListItem) => item.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE;

const isGroupRowExiting = (item: SearchListItem) => isRowDeleted(item) || (isTransactionGroupListItemType(item) && item.transactions.length > 0 && item.transactions.every(isRowDeleted));

const isRowSelected = (key: string | undefined, selectedTransactions: SelectedTransactions) => !!(key && selectedTransactions[key]?.isSelected);

/**
Expand Down Expand Up @@ -95,7 +96,7 @@ function buildNewTransactionIDMap(data: SearchListItem[], newTransactions: Trans
*/
function ExpenseGroupedSearchView({
queryJSON,
data,
data: sourceData,
columns,
canSelectMultiple,
isActionColumnWide,
Expand All @@ -118,6 +119,12 @@ function ExpenseGroupedSearchView({
}: ExpenseGroupedSearchViewProps) {
const {type, groupBy} = queryJSON;
const {isLargeScreenWidth} = useResponsiveLayout();
const {isOffline} = useNetwork();

// Deleting every expense in a group flags the group's own snapshot entry. Online, drop the row from the list on
// that same render so it pops out rather than playing FadeOutUp. Offline the row stays put with its pending-delete
// styling, as elsewhere.
const data = isOffline ? sourceData : sourceData.filter((item) => !isRowDeleted(item));

// Read once for the whole list and handed to each GroupHeader, rather than each of them subscribing on its own:
// a group header is a recycled row, and it already pays for a useWindowDimensions inside useResponsiveLayout.
Expand All @@ -140,10 +147,35 @@ function ExpenseGroupedSearchView({
return next;
});

if (expandedGroups.size > 0) {
const nextExpandedGroups = new Set(expandedGroups);
let didCollapseGroup = false;
for (const item of data) {
if (!isTransactionGroupListItemType(item) || !item.keyForList || !nextExpandedGroups.has(item.keyForList)) {
continue;
}
const remainingChildrenCount = item.transactions.filter((transaction) => !isTransactionPendingDelete(transaction)).length;
if (
!shouldCollapseExpandedGroupAfterPendingDelete({
isExpanded: true,
groupPendingAction: item.pendingAction,
loadedChildrenCount: item.transactions.length,
remainingChildrenCount,
})
) {
continue;
}
nextExpandedGroups.delete(item.keyForList);
didCollapseGroup = true;
}
if (didCollapseGroup) {
setExpandedGroups(nextExpandedGroups);
}
}

const [visibleColumns] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM, {selector: columnsSelector});

const {
isOffline,
isKeyboardShown,
safeAreaPaddingBottomStyle,
toggle,
Expand Down Expand Up @@ -277,7 +309,7 @@ function ExpenseGroupedSearchView({
<AnimatedExitRow
shouldApplyAnimation={type === CONST.SEARCH.DATA_TYPES.EXPENSE && index < listData.length - 1}
hasItemsBeingRemoved={hasItemsBeingRemoved}
isRowExiting={isGroupRowExiting(item)}
isRowExiting={isRowDeleted(item)}
>
<TransactionGroupListItem
showTooltip
Expand Down
9 changes: 1 addition & 8 deletions src/components/Search/SearchList/ListItem/GroupHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import type {ModifiedMouseEvent} from '@libs/Navigation/helpers/openInternalRouteInNewTab';
import {queryHasViolationFilter} from '@libs/SearchQueryUtils';
import {getColumnsToShow, getGroupColumnWidthFlags, getGroupTableScrollLayout} from '@libs/SearchUIUtils';
import {isTransactionPendingDelete} from '@libs/TransactionUtils';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -196,12 +195,6 @@ function GroupHeader({
onCheckboxPress(withOriginalKey(item), isExpenseReportType ? undefined : groupItem.transactions);
};

const pendingAction =
item.pendingAction ??
(groupItem.transactions.length > 0 && groupItem.transactions.every((transaction) => isTransactionPendingDelete(transaction))
? CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE
: undefined);

const handleSelectRow = (rowItem: SearchListItem, event?: ModifiedMouseEvent) => {
onSelectRow(withOriginalKey(rowItem), transactionPreviewData, event);
};
Expand Down Expand Up @@ -376,7 +369,7 @@ function GroupHeader({
};

return (
<OfflineWithFeedback pendingAction={pendingAction}>
<OfflineWithFeedback pendingAction={item.pendingAction}>
<PressableWithFeedback
ref={pressableRef}
onPress={handlePress}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import MerchantListItemHeader from './MerchantListItemHeader';
import MonthListItemHeader from './MonthListItemHeader';
import QuarterListItemHeader from './QuarterListItemHeader';
import ReportListItemHeader from './ReportListItemHeader';
import shouldCollapseExpandedGroupAfterPendingDelete from './shouldCollapseExpandedGroupAfterPendingDelete';
import TagListItemHeader from './TagListItemHeader';
import TransactionGroupListExpandedItem from './TransactionGroupListExpanded';
import useGroupChildren from './useGroupChildren';
Expand Down Expand Up @@ -146,6 +147,18 @@ function TransactionGroupListItemImpl({

const transactionsWithoutPendingDelete = transactions.filter((transaction) => !isTransactionPendingDelete(transaction));

if (
shouldCollapseExpandedGroupAfterPendingDelete({
isExpanded,
groupPendingAction: item.pendingAction,
loadedChildrenCount: transactions.length,
remainingChildrenCount: transactionsWithoutPendingDelete.length,
})
) {
setIsExpanded(false);
setTransactionsVisibleLimit(CONST.TRANSACTION.RESULTS_PAGE_SIZE);
}

// A group whose children are lazily loaded (it has a transactionsQueryJSON) is not empty, it just hasn't been fetched yet
const isEmpty = groupItem.transactions.length === 0 && !groupItem.transactionsQueryJSON;

Expand Down Expand Up @@ -461,12 +474,6 @@ function TransactionGroupListItemImpl({

useSyncFocus(pressableRef, !!isFocused, shouldSyncFocus);

const pendingAction =
item.pendingAction ??
(groupItem.transactions.length > 0 && groupItem.transactions.every((transaction) => isTransactionPendingDelete(transaction))
? CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE
: undefined);

const snapshotData = transactionsSnapshot?.data;
const groupViolations: Record<string, TransactionViolations | undefined> = {};
if (snapshotData) {
Expand Down Expand Up @@ -512,7 +519,7 @@ function TransactionGroupListItemImpl({
}

return (
<OfflineWithFeedback pendingAction={pendingAction}>
<OfflineWithFeedback pendingAction={item.pendingAction}>
<PressableWithFeedback
ref={pressableRef}
onLongPress={onLongPress}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Decides when an expanded Search group should collapse after its loaded children are deleted.
*/
import CONST from '@src/CONST';
import type {PendingAction} from '@src/types/onyx/OnyxCommon';

type ShouldCollapseExpandedGroupAfterPendingDeleteParams = {
isExpanded: boolean;
groupPendingAction?: PendingAction;
loadedChildrenCount: number;
remainingChildrenCount: number;
};

/**
* Collapse when every loaded child is pending-delete and the group itself is not.
*
* A query `limit:` smaller than the group count only deletes the loaded rows, so the group stays in
* the list. Leaving it expanded would keep showing those deleted children instead of the remaining
* expenses. A whole-group pending delete stays expanded so offline pending-delete styling still
* shows. Zero loaded children means the group has not been fetched yet, so expanding a lazy group
* must not collapse immediately.
*/
function shouldCollapseExpandedGroupAfterPendingDelete({
isExpanded,
groupPendingAction,
loadedChildrenCount,
remainingChildrenCount,
}: ShouldCollapseExpandedGroupAfterPendingDeleteParams): boolean {
if (!isExpanded || loadedChildrenCount === 0 || remainingChildrenCount > 0) {
return false;
}

return groupPendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE;
}

export default shouldCollapseExpandedGroupAfterPendingDelete;
Loading
Loading