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
33 changes: 33 additions & 0 deletions src/libs/markAllPolicyReportsAsRead.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// eslint-disable-next-line you-dont-need-lodash-underscore/each
import Onyx from 'react-native-onyx';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Report} from '@src/types/onyx';
import * as ReportActionFile from './actions/Report';
import * as ReportUtils from './ReportUtils';

export default function markAllPolicyReportsAsRead(policyID: string) {
const connectionID = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (allReports) => {
if (!allReports) {
return;
}

let delay = 0;
Object.keys(allReports).forEach((key: string) => {
const report: Report | null | undefined = allReports[key];
if (report?.policyID !== policyID || !ReportUtils.isUnread(report)) {
return;
}

setTimeout(() => {
Comment thread
marcaaron marked this conversation as resolved.
ReportActionFile.readNewestAction(report?.reportID);
}, delay);

delay += 1000;
Comment on lines +24 to +28

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👋 @marcaaron or @rlinoz Do you recall why we are using a timeout here? We are looking to add similar functionality to a keyboard shortcut and wanted to reuse same logic but the timeout is a problem as it will delay even optimistic actions.

Is this done to prevent server flooding or something else?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also, here is a slack thread for moving this logic to BE instead https://expensify.slack.com/archives/C01GTK53T8Q/p1746861626079909

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.

Yes, it's just to avoid hammering the server with requests since some users who requested this feature had thousands of reports. Though, I think we probably have solve that problem by now.

});
Onyx.disconnect(connectionID);
},
});
}
5 changes: 5 additions & 0 deletions src/setup/addUtilsToWindow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Onyx from 'react-native-onyx';
import * as Environment from '@libs/Environment/Environment';
import markAllPolicyReportsAsRead from '@libs/markAllPolicyReportsAsRead';
import * as Session from '@userActions/Session';

/**
Expand Down Expand Up @@ -44,5 +45,9 @@ export default function addUtilsToWindow() {
};

window.setSupportToken = Session.setSupportAuthToken;

// Workaround to give employees the ability to mark reports as read via the JS console
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).markAllPolicyReportsAsRead = markAllPolicyReportsAsRead;
});
}