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 config/eslint/eslint.seatbelt.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@
"../../src/pages/Search/SearchPage.tsx" "react-hooks/set-state-in-effect" 1
"../../src/pages/Search/SearchSavePage.tsx" "@typescript-eslint/no-unsafe-type-assertion" 3
"../../src/pages/Search/SearchTransactionsChangeReport.tsx" "@typescript-eslint/no-deprecated/InteractionManager.runAfterInteractions" 1
"../../src/pages/Search/SearchTransactionsChangeReport.tsx" "@typescript-eslint/no-unsafe-type-assertion" 1
"../../src/pages/Search/SearchTransactionsChangeReport.tsx" "@typescript-eslint/no-unsafe-type-assertion" 2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karan-1302 why do we need this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The seatbelt bump (1 → 2) is needed because we cast the string key from Object.keys() to a typed Onyx report key. TypeScript can't narrow this automatically, so the as cast is unavoidable — same pattern used elsewhere in Search code.

"../../src/pages/Share/ShareRootPage.tsx" "@typescript-eslint/no-unsafe-type-assertion" 1
"../../src/pages/Share/ShareTab.tsx" "@typescript-eslint/no-unsafe-type-assertion" 1
"../../src/pages/ShareCodePage.tsx" "@typescript-eslint/no-unsafe-type-assertion" 2
Expand Down
48 changes: 45 additions & 3 deletions src/pages/Search/SearchTransactionsChangeReport.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, {useMemo} from 'react';
import React, {useEffect, useMemo} from 'react';
// eslint-disable-next-line no-restricted-imports
import {InteractionManager} from 'react-native';
import type {OnyxCollection} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import {usePersonalDetails, useSession} from '@components/OnyxListItemProvider';
import {useSearchSelectionActions, useSearchSelectionContext} from '@components/Search/SearchContext';
import {useSearchResultsContext, useSearchSelectionActions, useSearchSelectionContext} from '@components/Search/SearchContext';
import type {ListItem} from '@components/SelectionList/types';
import useConditionalCreateEmptyReportConfirmation from '@hooks/useConditionalCreateEmptyReportConfirmation';
import useHasPerDiemTransactions from '@hooks/useHasPerDiemTransactions';
Expand All @@ -21,7 +22,7 @@ import IOURequestEditReportCommon from '@pages/iou/request/step/IOURequestEditRe
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES, {DYNAMIC_ROUTES} from '@src/ROUTES';
import type {PersonalDetails, Transaction} from '@src/types/onyx';
import type {PersonalDetails, Report, Transaction} from '@src/types/onyx';

type TransactionGroupListItem = ListItem & {
/** reportID of the report */
Expand All @@ -31,6 +32,7 @@ type TransactionGroupListItem = ListItem & {
function SearchTransactionsChangeReport() {
const {selectedTransactions} = useSearchSelectionContext();
const {clearSelectedTransactions} = useSearchSelectionActions();
const {currentSearchResults} = useSearchResultsContext();
const selectedTransactionsKeys = useMemo(() => Object.keys(selectedTransactions), [selectedTransactions]);
const transactions = useMemo(
() =>
Expand Down Expand Up @@ -97,6 +99,46 @@ function SearchTransactionsChangeReport() {
return report?.ownerAccountID;
}, [selectedTransactions, selectedTransactionsKeys, allReports]);
const targetOwnerPersonalDetails = useMemo(() => getPersonalDetailsForAccountID(targetOwnerAccountID, personalDetails) as PersonalDetails, [personalDetails, targetOwnerAccountID]);

useEffect(() => {
const snapshotData = currentSearchResults?.data;
if (!snapshotData) {
return;
}

const onyxUpdates: Array<{
onyxMethod: typeof Onyx.METHOD.MERGE;
key: `${typeof ONYXKEYS.COLLECTION.REPORT}${string}`;
value: Report;
}> = [];

for (const key of Object.keys(snapshotData)) {
if (!key.startsWith(ONYXKEYS.COLLECTION.REPORT) || key.startsWith(ONYXKEYS.COLLECTION.REPORT_ACTIONS) || key.startsWith(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS)) {
Comment thread
karan-1302 marked this conversation as resolved.
Resolved
continue;
}

const typedKey = key as `${typeof ONYXKEYS.COLLECTION.REPORT}${string}`;
if (allReports?.[typedKey]) {
continue;
}

const report = snapshotData[typedKey];
if (report) {
onyxUpdates.push({
onyxMethod: Onyx.METHOD.MERGE,
key: typedKey,
value: report,
});
}
}

if (onyxUpdates.length > 0) {
Onyx.update(onyxUpdates);
}
// Hydration should only run once on mount using the initial snapshot data
// eslint-disable-next-line react-hooks/exhaustive-deps
Comment thread
karan-1302 marked this conversation as resolved.
Resolved
}, []);

const createReportForPolicy = (shouldDismissEmptyReportsConfirmation?: boolean) => {
const optimisticReport = createNewReport(
targetOwnerPersonalDetails,
Expand Down
Loading