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,5 +1,5 @@
/* eslint-disable rulesdir/prefer-early-return */
import {useIsFocused, useRoute} from '@react-navigation/native';
import {useFocusEffect, useIsFocused, useRoute} from '@react-navigation/native';
import {isUserValidatedSelector} from '@selectors/Account';
import {tierNameSelector} from '@selectors/UserWallet';
import isEmpty from 'lodash/isEmpty';
Expand Down Expand Up @@ -204,10 +204,20 @@ function MoneyRequestReportActionsList({
const [enableScrollToEnd, setEnableScrollToEnd] = useState<boolean>(false);
const [lastActionEventId, setLastActionEventId] = useState<string>('');

const {selectedTransactionIDs} = useSearchStateContext();
const {setSelectedTransactions, clearSelectedTransactions} = useSearchActionsContext();
const {selectedTransactionIDs, currentSelectedTransactionReportID} = useSearchStateContext();
const {setSelectedTransactions, clearSelectedTransactions, setCurrentSelectedTransactionReportID} = useSearchActionsContext();

useFilterSelectedTransactions(transactions);
useFocusEffect(
useCallback(() => {
if (reportID && currentSelectedTransactionReportID !== reportID && selectedTransactionIDs.length > 0) {
clearSelectedTransactions(true);
}

setCurrentSelectedTransactionReportID(reportID);
}, [clearSelectedTransactions, currentSelectedTransactionReportID, reportID, selectedTransactionIDs.length, setCurrentSelectedTransactionReportID]),
);

useFilterSelectedTransactions(transactions, reportID);

const isMobileSelectionModeEnabled = useMobileSelectionMode();
const {showConfirmModal} = useConfirmModal();
Expand Down
16 changes: 16 additions & 0 deletions src/components/Search/SearchContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const defaultSearchContextData: SearchContextData = {
currentSearchKey: undefined,
currentSearchQueryJSON: undefined,
currentSearchResults: undefined,
currentSelectedTransactionReportID: undefined,
selectedTransactions: {},
selectedTransactionIDs: [],
selectedReports: [],
Expand All @@ -67,6 +68,7 @@ const defaultSearchStateContext: SearchStateContextValue = {

const defaultSearchActionsContext: SearchActionsContextValue = {
setLastSearchType: () => {},
setCurrentSelectedTransactionReportID: () => {},
setSelectedTransactions: () => {},
removeTransaction: () => {},
clearSelectedTransactions: () => {},
Expand Down Expand Up @@ -211,6 +213,19 @@ function SearchContextProvider({children}: SearchContextProps) {
}));
};

const setCurrentSelectedTransactionReportID: SearchActionsContextValue['setCurrentSelectedTransactionReportID'] = (reportID) => {
setSearchContextData((prevState) => {
if (reportID === prevState.currentSelectedTransactionReportID) {
return prevState;
}

return {
...prevState,
currentSelectedTransactionReportID: reportID,
};
});
};

const clearSelectedTransactions: SearchActionsContextValue['clearSelectedTransactions'] = useCallback(
(searchHashOrClearIDsFlag, shouldTurnOffSelectionMode = false) => {
if (typeof searchHashOrClearIDsFlag === 'boolean') {
Expand Down Expand Up @@ -292,6 +307,7 @@ function SearchContextProvider({children}: SearchContextProps) {
const searchActionsContextValue: SearchActionsContextValue = {
removeTransaction,
setSelectedTransactions,
setCurrentSelectedTransactionReportID,
clearSelectedTransactions,
setShouldShowFiltersBarLoading,
setLastSearchType,
Expand Down
2 changes: 2 additions & 0 deletions src/components/Search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ type SearchContextData = {
currentSearchKey: SearchKey | undefined;
currentSearchQueryJSON: SearchQueryJSON | undefined;
currentSearchResults: SearchResults | undefined;
currentSelectedTransactionReportID: string | undefined;
selectedTransactions: SelectedTransactions;
selectedTransactionIDs: string[];
selectedReports: SelectedReports[];
Expand All @@ -188,6 +189,7 @@ type SearchActionsContextValue = {
(selectedTransactionIDs: string[], unused?: undefined): void;
(selectedTransactions: SelectedTransactions, data: TransactionListItemType[] | TransactionGroupListItemType[] | ReportActionListItemType[] | TaskListItemType[]): void;
};
setCurrentSelectedTransactionReportID: (reportID: string | undefined) => void;
/** If you want to clear `selectedTransactionIDs`, pass `true` as the first argument */
clearSelectedTransactions: {
(hash?: number, shouldTurnOffSelectionMode?: boolean): void;
Expand Down
13 changes: 9 additions & 4 deletions src/hooks/useFilterSelectedTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import type {Transaction} from '@src/types/onyx';
* This is useful when transactions are deleted and we need to clean up the selection state.
*
* @param transactions - The current list of transactions
* @param reportID - The report that owns the current transaction list
*/
function useFilterSelectedTransactions(transactions: Transaction[]) {
const {selectedTransactionIDs} = useSearchStateContext();
function useFilterSelectedTransactions(transactions: Transaction[], reportID?: string) {
const {selectedTransactionIDs, currentSelectedTransactionReportID} = useSearchStateContext();
const {setSelectedTransactions} = useSearchActionsContext();

const transactionIDs = useMemo(() => transactions.map((transaction) => transaction.transactionID), [transactions]);
Expand All @@ -18,9 +19,13 @@ function useFilterSelectedTransactions(transactions: Transaction[]) {
if (filteredSelectedTransactionIDs.length === selectedTransactionIDs.length) {
return;
}

if (reportID && currentSelectedTransactionReportID && currentSelectedTransactionReportID !== reportID) {
return;
}

setSelectedTransactions(filteredSelectedTransactionIDs);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [filteredSelectedTransactionIDs]);
}, [currentSelectedTransactionReportID, filteredSelectedTransactionIDs, reportID, selectedTransactionIDs.length, setSelectedTransactions]);
}

export default useFilterSelectedTransactions;
2 changes: 2 additions & 0 deletions tests/ui/CategoryListItemHeaderTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const mockSearchStateContext = {
currentSearchKey: undefined,
currentSearchQueryJSON: undefined,
currentSearchResults: undefined,
currentSelectedTransactionReportID: undefined,
selectedReports: [],
selectedTransactionIDs: [],
selectedTransactions: {},
Expand All @@ -43,6 +44,7 @@ const mockSearchStateContext = {

const mockSearchActionsContext = {
setLastSearchType: jest.fn(),
setCurrentSelectedTransactionReportID: jest.fn(),
setSelectedTransactions: jest.fn(),
removeTransaction: jest.fn(),
clearSelectedTransactions: jest.fn(),
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/MerchantListItemHeaderTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const mockSearchStateContext = {
currentSearchKey: undefined,
currentSearchQueryJSON: undefined,
currentSearchResults: undefined,
currentSelectedTransactionReportID: undefined,
selectedReports: [],
selectedTransactionIDs: [],
selectedTransactions: {},
Expand All @@ -43,6 +44,7 @@ const mockSearchStateContext = {

const mockSearchActionsContext = {
setLastSearchType: jest.fn(),
setCurrentSelectedTransactionReportID: jest.fn(),
setSelectedTransactions: jest.fn(),
removeTransaction: jest.fn(),
clearSelectedTransactions: jest.fn(),
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/MonthListItemHeaderTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const mockSearchStateContext = {
currentSearchKey: undefined,
currentSearchQueryJSON: undefined,
currentSearchResults: undefined,
currentSelectedTransactionReportID: undefined,
selectedReports: [],
selectedTransactionIDs: [],
selectedTransactions: {},
Expand All @@ -43,6 +44,7 @@ const mockSearchStateContext = {

const mockSearchActionsContext = {
setLastSearchType: jest.fn(),
setCurrentSelectedTransactionReportID: jest.fn(),
setSelectedTransactions: jest.fn(),
removeTransaction: jest.fn(),
clearSelectedTransactions: jest.fn(),
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/ReportListItemHeaderTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const mockSearchStateContext = {
currentSearchKey: undefined,
currentSearchQueryJSON: undefined,
currentSearchResults: undefined,
currentSelectedTransactionReportID: undefined,
shouldShowSelectAllMatchingItems: false,
shouldShowFiltersBarLoading: false,
shouldUseLiveData: false,
Expand All @@ -40,6 +41,7 @@ const mockSearchStateContext = {
const mockSearchActionsContext = {
clearSelectedTransactions: jest.fn(),
setLastSearchType: jest.fn(),
setCurrentSelectedTransactionReportID: jest.fn(),
setSelectedTransactions: jest.fn(),
setShouldShowFiltersBarLoading: jest.fn(),
setShouldShowSelectAllMatchingItems: jest.fn(),
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/WeekListItemHeaderTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const mockSearchStateContext = {
currentSearchKey: undefined,
currentSearchQueryJSON: undefined,
currentSearchResults: undefined,
currentSelectedTransactionReportID: undefined,
selectedReports: [],
selectedTransactionIDs: [],
selectedTransactions: {},
Expand All @@ -42,6 +43,7 @@ const mockSearchStateContext = {

const mockSearchActionsContext = {
setLastSearchType: jest.fn(),
setCurrentSelectedTransactionReportID: jest.fn(),
setSelectedTransactions: jest.fn(),
removeTransaction: jest.fn(),
clearSelectedTransactions: jest.fn(),
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/YearListItemHeaderTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const mockSearchStateContext = {
currentSearchKey: undefined,
currentSearchQueryJSON: undefined,
currentSearchResults: undefined,
currentSelectedTransactionReportID: undefined,
selectedReports: [],
selectedTransactionIDs: [],
selectedTransactions: {},
Expand All @@ -43,6 +44,7 @@ const mockSearchStateContext = {

const mockSearchActionsContext = {
setLastSearchType: jest.fn(),
setCurrentSelectedTransactionReportID: jest.fn(),
setSelectedTransactions: jest.fn(),
removeTransaction: jest.fn(),
clearSelectedTransactions: jest.fn(),
Expand Down
Loading