From 598dda8f9b3c3eeada9e3f95224234420f5d63eb Mon Sep 17 00:00:00 2001 From: Hubert Sosinski Date: Wed, 18 Nov 2026 14:19:09 +0100 Subject: [PATCH] Build the reportID to name map once per report collection --- src/hooks/useReportIDToNameMap.ts | 57 +++++++++++- tests/unit/useReportIDToNameMapTest.tsx | 116 +++++++++++++++++++++++- 2 files changed, 167 insertions(+), 6 deletions(-) diff --git a/src/hooks/useReportIDToNameMap.ts b/src/hooks/useReportIDToNameMap.ts index 7b67e429d9d5..7e08ab1c5b06 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 './useOnyxState'; /** Mapping from reportID to report name. */ type ReportIDToNameMap = Record; @@ -72,10 +72,59 @@ function buildReportIDToNameMap(reports: OnyxCollection | undefined, rep return map; } -/** Returns a map of reportID to report name built from COLLECTION.REPORT. */ +/** True when a report was added or removed, or a report name differs between the two collections. */ +function hasReportNameChanged(previousReports: NonNullable>, reports: NonNullable>): boolean { + for (const [key, report] of Object.entries(reports)) { + const previousReport = previousReports[key]; + if (report === previousReport) { + continue; + } + if (!report !== !previousReport || report?.reportName !== previousReport?.reportName) { + return true; + } + } + for (const [key, previousReport] of Object.entries(previousReports)) { + if (previousReport && !reports[key]) { + return true; + } + } + return false; +} + +const EMPTY_REPORT_ID_TO_NAME_MAP: ReportIDToNameMap = {}; + +// Keyed by the collection, not held as one last value, because the same selector runs with live reports in most places and +// with the reports of a Search snapshot inside Search. A single slot would be evicted on every switch between the two. +const reportIDToNameMapByReports = new WeakMap>, ReportIDToNameMap>(); + +/** + * Returns the map for a report collection, shared by every consumer that reads the same collection. Most report writes + * (a new message, a read marker) change no name, so the map of the previous collection is reused and consumers do not re-render. + */ +function selectReportIDToNameMap(reports: OnyxCollection | undefined, previousReports: OnyxCollection | undefined): ReportIDToNameMap { + if (!reports) { + return EMPTY_REPORT_ID_TO_NAME_MAP; + } + const cachedMap = reportIDToNameMapByReports.get(reports); + if (cachedMap) { + return cachedMap; + } + + const previousMap = previousReports && reportIDToNameMapByReports.get(previousReports); + const map = previousReports && previousMap && !hasReportNameChanged(previousReports, reports) ? previousMap : buildReportIDToNameMap(reports); + reportIDToNameMapByReports.set(reports, map); + return map; +} + +function isSameReportIDToNameMap(map: ReportIDToNameMap, otherMap: ReportIDToNameMap): boolean { + return map === otherMap; +} + +/** Returns a map of reportID to report name built from COLLECTION.REPORT. The map keeps its reference until a report is added or removed, or a name changes. */ function useReportIDToNameMap(): ReportIDToNameMap { - const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT); - return buildReportIDToNameMap(reports); + return useOnyxState((state, previousState) => selectReportIDToNameMap(state[ONYXKEYS.COLLECTION.REPORT], previousState?.[ONYXKEYS.COLLECTION.REPORT]), { + selectorEquality: isSameReportIDToNameMap, + }); } export default useReportIDToNameMap; diff --git a/tests/unit/useReportIDToNameMapTest.tsx b/tests/unit/useReportIDToNameMapTest.tsx index 0f8a2ea8cccc..24720da4668e 100644 --- a/tests/unit/useReportIDToNameMapTest.tsx +++ b/tests/unit/useReportIDToNameMapTest.tsx @@ -1,12 +1,17 @@ import {act, renderHook} from '@testing-library/react-native'; +import {SearchQueryContext} from '@components/Search/SearchContextDefinitions'; +import {SearchScopeProvider} from '@components/Search/SearchScopeProvider'; + import useReportIDToNameMap, {buildMentionedRoomPlaceholderMap, buildReportIDToNameMap, getMentionedReportIDs, getReportIDToNameMapForRoomsInMarkdown} from '@hooks/useReportIDToNameMap'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Report} from '@src/types/onyx'; +import type ChildrenProps from '@src/types/utils/ChildrenProps'; import type {OnyxCollection} from 'react-native-onyx'; +import React, {use, useMemo} from 'react'; import Onyx from 'react-native-onyx'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; @@ -19,8 +24,24 @@ function createReport(reportID: string, overrides: Partial = {}): Report } as Report; } -const renderReportIDToNameMap = async () => { - const hook = renderHook(() => useReportIDToNameMap()); +const REPORT_ID = '1'; +const REPORT_KEY = `${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}` as const; +const SEARCH_HASH = 42; +const SNAPSHOT_KEY = `${ONYXKEYS.COLLECTION.SNAPSHOT}${SEARCH_HASH}` as const; + +function SearchScreen({children}: ChildrenProps) { + const queryContext = use(SearchQueryContext); + const queryContextWithHash = useMemo(() => ({...queryContext, currentSearchHash: SEARCH_HASH}), [queryContext]); + + return ( + + {children} + + ); +} + +const renderReportIDToNameMap = async (wrapper?: React.ComponentType) => { + const hook = renderHook(() => useReportIDToNameMap(), {wrapper}); await act(async () => { await waitForBatchedUpdates(); }); @@ -91,6 +112,97 @@ 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).toEqual({[REPORT_ID]: 'First'}); + }); + + it('should give two consumers of the same reports the same map, before and after a name change', async () => { + // Given two mounted consumers of the live reports + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}1`, createReport('1', {reportName: 'Old Name'})); + const {result: firstConsumer} = await renderReportIDToNameMap(); + const {result: secondConsumer} = await renderReportIDToNameMap(); + const mapBeforeRename = firstConsumer.current; + + // When a report is renamed + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}1`, {reportName: 'New Name'}); + await waitForBatchedUpdates(); + }); + + // Then both consumers share one map each time, because the map is built once per collection and not once per consumer + expect(secondConsumer.current).toBe(firstConsumer.current); + expect(firstConsumer.current).toEqual({[REPORT_ID]: 'New Name'}); + expect(mapBeforeRename).toEqual({[REPORT_ID]: 'Old Name'}); + }); + + it('should build the map from the snapshot reports inside Search', async () => { + // Given two live reports and a Search snapshot that holds one of them under another name + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}1`, createReport('1', {reportName: 'Live name'})); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}2`, createReport('2', {reportName: 'Live only'})); + await Onyx.merge(SNAPSHOT_KEY, {data: {[REPORT_KEY]: createReport('1', {reportName: 'Snapshot name'})}}); + + // When the map is read on a Search screen + const {result} = await renderReportIDToNameMap(SearchScreen); + + // Then it holds the snapshot report only, the same reports useOnyx returns on Search + expect(result.current).toEqual({[REPORT_ID]: 'Snapshot name'}); + }); + + it('should keep the live map and the Search map apart when either source changes', async () => { + // Given one consumer on a Search screen and one outside it, each with its own map + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}1`, createReport('1', {reportName: 'Live name'})); + await Onyx.merge(SNAPSHOT_KEY, {data: {[REPORT_KEY]: createReport('1', {reportName: 'Snapshot name'})}}); + const {result: searchConsumer} = await renderReportIDToNameMap(SearchScreen); + const {result: liveConsumer} = await renderReportIDToNameMap(); + const searchMap = searchConsumer.current; + const liveMap = liveConsumer.current; + + // When the live report and then the snapshot report each get a write that changes no name + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}1`, {lastMessageText: 'hello'}); + await waitForBatchedUpdates(); + }); + await act(async () => { + await Onyx.merge(SNAPSHOT_KEY, {data: {[REPORT_KEY]: {lastMessageText: 'hello'}}}); + await waitForBatchedUpdates(); + }); + + // Then both consumers keep the map they had, because each source compares against its own previous reports and not the other's + expect(liveConsumer.current).toBe(liveMap); + expect(searchConsumer.current).toBe(searchMap); + expect(liveMap).toEqual({[REPORT_ID]: 'Live name'}); + expect(searchMap).toEqual({[REPORT_ID]: 'Snapshot name'}); + }); }); describe('getReportIDToNameMapForRoomsInMarkdown', () => {