Skip to content
Draft
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
4 changes: 2 additions & 2 deletions __mocks__/react-native-onyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -41,4 +41,4 @@ const reactNativeOnyxMock: ReactNativeOnyxMock = {

export default reactNativeOnyxMock;

export {useOnyx};
export {useOnyx, useOnyxState};
51 changes: 46 additions & 5 deletions src/hooks/useReportIDToNameMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;
Expand All @@ -23,22 +23,63 @@ 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<Report> | undefined): ReportIDToNameMap {
const map: ReportIDToNameMap = {};
for (const report of Object.values(reports ?? {})) {
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<Report>, reports: OnyxCollection<Report>): 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<Report> | undefined;
let cachedReportIDToName: ReportIDToNameMap = {};

function selectReportIDToNameMap(reports: OnyxCollection<Report> | 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;
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/useReportIDToNameMapTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading