diff --git a/__mocks__/react-native-onyx.ts b/__mocks__/react-native-onyx.ts index 6db22ac1b627..f5b09f2949da 100644 --- a/__mocks__/react-native-onyx.ts +++ b/__mocks__/react-native-onyx.ts @@ -5,7 +5,7 @@ import type {ConnectOptions, OnyxKey} from 'react-native-onyx'; // eslint-disable-next-line no-restricted-imports -import Onyx, {useOnyx} from 'react-native-onyx'; +import Onyx, {useOnyx, useOnyxState} from 'react-native-onyx'; let connectCallbackDelay = 0; function addDelayToConnectCallback(delay: number) { @@ -41,4 +41,4 @@ const reactNativeOnyxMock: ReactNativeOnyxMock = { export default reactNativeOnyxMock; -export {useOnyx}; +export {useOnyx, useOnyxState}; diff --git a/src/hooks/useReportIDToNameMap.ts b/src/hooks/useReportIDToNameMap.ts index e68784112c42..cdb4ac53b0a6 100644 --- a/src/hooks/useReportIDToNameMap.ts +++ b/src/hooks/useReportIDToNameMap.ts @@ -3,7 +3,7 @@ import type {Report} from '@src/types/onyx'; import type {OnyxCollection} from 'react-native-onyx'; -import useOnyx from './useOnyx'; +import {useOnyxState} from 'react-native-onyx'; /** Mapping from reportID to report name. */ type ReportIDToNameMap = Record; @@ -23,6 +23,10 @@ function getReportIDToNameMapForRoomsInMarkdown(reportIDToName: ReportIDToNameMa return map; } +function getMappedName(report: Report): string { + return report.reportName ?? report.reportID; +} + /** Builds a map of reportID to report name from COLLECTION.REPORT. */ function buildReportIDToNameMap(reports: OnyxCollection | undefined): ReportIDToNameMap { const map: ReportIDToNameMap = {}; @@ -30,15 +34,52 @@ function buildReportIDToNameMap(reports: OnyxCollection | undefined): Re if (!report) { continue; } - map[report.reportID] = report.reportName ?? report.reportID; + map[report.reportID] = getMappedName(report); } return map; } -/** Returns a map of reportID to report name built from COLLECTION.REPORT. */ +function hasReportIDToNameChanged(previousReports: OnyxCollection, reports: OnyxCollection): boolean { + for (const [key, report] of Object.entries(reports ?? {})) { + const previousReport = previousReports?.[key]; + if (report === previousReport) { + continue; + } + if (report?.reportID !== previousReport?.reportID || (report && getMappedName(report)) !== (previousReport && getMappedName(previousReport))) { + return true; + } + } + for (const [key, previousReport] of Object.entries(previousReports ?? {})) { + if (previousReport && !reports?.[key]) { + return true; + } + } + return false; +} + +// Shared by every mounted consumer: collection snapshots keep unchanged members reference-equal, so most writes +// (a new message, a read marker) are detected as name-neutral with reference checks and reuse the previous map. +let cachedReports: OnyxCollection | undefined; +let cachedReportIDToName: ReportIDToNameMap = {}; + +function selectReportIDToNameMap(reports: OnyxCollection | undefined): ReportIDToNameMap { + if (reports === cachedReports) { + return cachedReportIDToName; + } + const previousReports = cachedReports; + cachedReports = reports; + if (!previousReports || hasReportIDToNameChanged(previousReports, reports)) { + cachedReportIDToName = buildReportIDToNameMap(reports); + } + return cachedReportIDToName; +} + +/** Returns a map of reportID to report name built from COLLECTION.REPORT, re-rendering only when a name changes. */ function useReportIDToNameMap(): ReportIDToNameMap { - const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT); - return buildReportIDToNameMap(reports); + return useOnyxState((state) => selectReportIDToNameMap(state[ONYXKEYS.COLLECTION.REPORT]), { + dependencies: [ONYXKEYS.COLLECTION.REPORT], + selectorEquality: (a, b) => a === b, + }); } export default useReportIDToNameMap; diff --git a/tests/unit/useReportIDToNameMapTest.tsx b/tests/unit/useReportIDToNameMapTest.tsx index add7583ab3a5..e37ee8b799d3 100644 --- a/tests/unit/useReportIDToNameMapTest.tsx +++ b/tests/unit/useReportIDToNameMapTest.tsx @@ -89,6 +89,40 @@ describe('useReportIDToNameMap', () => { expect(result.current).toBe(firstResult); }); + + it('should keep the same map when a report write does not change any name', async () => { + // Given a mounted map over two reports + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}1`, createReport('1', {reportName: 'First'})); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}2`, createReport('2', {reportName: 'Second'})); + const {result} = await renderReportIDToNameMap(); + const firstResult = result.current; + + // When a report gets a new message, which touches the report but not its name + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}1`, {lastMessageText: 'hello'}); + await waitForBatchedUpdates(); + }); + + // Then consumers keep the same map, so sending a message does not re-render the composer through it + expect(result.current).toBe(firstResult); + }); + + it('should drop a report from the map when it is removed', async () => { + // Given a mounted map over two reports + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}1`, createReport('1', {reportName: 'First'})); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}2`, createReport('2', {reportName: 'Second'})); + const {result} = await renderReportIDToNameMap(); + + // When one report is removed, which changes no remaining name + await act(async () => { + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}2`, null); + await waitForBatchedUpdates(); + }); + + // Then the removed report leaves the map, because removals count as a change + expect(result.current['2']).toBeUndefined(); + expect(result.current['1']).toBe('First'); + }); }); describe('getReportIDToNameMapForRoomsInMarkdown', () => {