Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
69cc9f2
Use newest Onyx changes from Onyx store-based PR 2
fabioh8010 Jun 12, 2026
eb942cb
Merge remote-tracking branch 'origin/main' into feature/onyx-store-pr-2
fabioh8010 Jun 15, 2026
7beffbd
Replace removed Onyx sourceValue with a structural-sharing delta
fabioh8010 Jun 15, 2026
34c8b8a
Merge branch 'feature/onyx-store-poc-baseline' into feature/onyx-stor…
fabioh8010 Jun 22, 2026
b6fce1a
Merge branch 'main' into feature/onyx-store-pr-2
fabioh8010 Jun 22, 2026
64a15f8
Merge branch 'feature/onyx-store-poc-baseline' into feature/onyx-stor…
fabioh8010 Jun 22, 2026
35e1cc2
Merge branch 'feature/onyx-store-poc-baseline' into feature/onyx-stor…
fabioh8010 Jun 24, 2026
51bbeb7
Merge branch 'feature/onyx-store-poc-baseline' into feature/onyx-stor…
fabioh8010 Jun 30, 2026
076914f
Rebuild VISIBLE_REPORT_ACTIONS per report so deletions take effect
fabioh8010 Jun 30, 2026
bb9143e
Add tests for useCollectionDelta/getCollectionDelta
fabioh8010 Jun 30, 2026
39922ea
Cleanup
fabioh8010 Jun 30, 2026
05fe128
Cleanup
fabioh8010 Jun 30, 2026
f02c0e7
Merge branch 'main' into feature/onyx-store-pr-2
fabioh8010 Jul 23, 2026
aef6a90
Restore comment
fabioh8010 Jul 23, 2026
96788cb
Format files
fabioh8010 Jul 23, 2026
4ccad87
Add test for visibility recompute on a session change
fabioh8010 Jul 23, 2026
8c62d6e
Remove useMemo
fabioh8010 Jul 23, 2026
bb13756
Merge remote-tracking branch 'origin/main' into feature/onyx-store-pr-2
fabioh8010 Jul 24, 2026
ed1e14e
Merge remote-tracking branch 'origin/main' into feature/onyx-store-pr-2
fabioh8010 Jul 24, 2026
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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
"react-native-nitro-fetch": "1.5.0",
"react-native-nitro-modules": "0.35.9",
"react-native-nitro-sqlite": "9.6.0",
"react-native-onyx": "3.0.89",
"react-native-onyx": "3.0.90",
"react-native-pager-view": "8.0.0",
"react-native-pdf": "7.0.2",
"react-native-permissions": "^5.4.0",
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useCardFeeds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {CardFeeds, CardFeedsStatusByDomainID, CombinedCardFeed, CombinedCardFeeds, CompanyCardFeedWithDomainID} from '@src/types/onyx';

import type {OnyxCollection, ResultMetadata} from 'react-native-onyx';
import type {ResultMetadata} from 'react-native-onyx';

import useFeedKeysWithAssignedCards from './useFeedKeysWithAssignedCards';
import useOnyx from './useOnyx';
Expand All @@ -26,7 +26,7 @@ import useWorkspaceAccountID from './useWorkspaceAccountID';
* 4. Card feed status by domain ID.
* 5. Workspace account ID for the policy.
*/
const useCardFeeds = (policyID: string | undefined): [CombinedCardFeeds | undefined, ResultMetadata<OnyxCollection<CardFeeds>>, CardFeeds | undefined, CardFeedsStatusByDomainID, number] => {
const useCardFeeds = (policyID: string | undefined): [CombinedCardFeeds | undefined, ResultMetadata, CardFeeds | undefined, CardFeedsStatusByDomainID, number] => {
const workspaceAccountID = useWorkspaceAccountID(policyID);
const [allFeeds, allFeedsResult] = useOnyx(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER);
const feedKeysWithCards = useFeedKeysWithAssignedCards();
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useCardsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {ResultMetadata} from 'react-native-onyx';
import useOnyx from './useOnyx';

/* Custom hook that retrieves a list of company cards for the given selected feed. */
const useCardsList = (selectedFeed: CardFeedWithDomainID | undefined): [WorkspaceCardsList | undefined, ResultMetadata<WorkspaceCardsList>] => {
const useCardsList = (selectedFeed: CardFeedWithDomainID | undefined): [WorkspaceCardsList | undefined, ResultMetadata] => {
const splitFeedName = splitCardFeedWithDomainID(selectedFeed);

const [cardsList, cardsListMetadata] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${splitFeedName?.domainID}_${splitFeedName?.feedName}`, {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useCardsLists.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const filterInactiveCardsInCollection = (cardsLists: OnyxCollection<WorkspaceCar
};

/* Custom hook that retrieves all cards lists (excluding inactive cards from each list). */
const useCardsLists = (): [OnyxCollection<WorkspaceCardsList>, ResultMetadata<OnyxCollection<WorkspaceCardsList>>] => {
const useCardsLists = (): [OnyxCollection<WorkspaceCardsList>, ResultMetadata] => {
const [cardsLists, cardsListsMetadata] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}`, {
selector: filterInactiveCardsInCollection,
});
Expand Down
17 changes: 17 additions & 0 deletions src/hooks/useCollectionDelta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import getCollectionDelta from '@libs/getCollectionDelta';

import type {OnyxCollection} from 'react-native-onyx';

import usePrevious from './usePrevious';

/**
* Given the latest collection value from `useOnyx`, returns the subset of members that changed since
* the previous render (added, changed, or removed), or `undefined` when nothing changed. Because Onyx
* structurally shares unchanged members, the underlying diff is a cheap reference-equality scan.
*/
function useCollectionDelta<TValue>(value: OnyxCollection<TValue>): OnyxCollection<TValue> | undefined {
const previous = usePrevious(value);
return getCollectionDelta(value, previous);
}

export default useCollectionDelta;
12 changes: 6 additions & 6 deletions src/hooks/useCompanyCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import {filterAmexDirectParentCard, getCompanyCardFeed, getCompanyFeeds, getSele

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {CardFeeds, CardList} from '@src/types/onyx';
import type {CardList} from '@src/types/onyx';
import type Card from '@src/types/onyx/Card';
import type {AssignableCardsList, WorkspaceCardsList} from '@src/types/onyx/Card';
import type {AssignableCardsList} from '@src/types/onyx/Card';
import type {CardFeedsStatusByDomainID, CombinedCardFeeds, CompanyCardFeedWithDomainID, CompanyCardFeedWithNumber, CompanyFeeds} from '@src/types/onyx/CardFeeds';
import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue';

import type {OnyxCollection, OnyxEntry, ResultMetadata} from 'react-native-onyx';
import type {ResultMetadata} from 'react-native-onyx';

import type {CombinedCardFeed} from './useCardFeeds';

Expand Down Expand Up @@ -45,9 +45,9 @@ type UseCompanyCardsResult = Partial<{
isFeedAdded: boolean;

onyxMetadata: {
cardListMetadata: ResultMetadata<WorkspaceCardsList>;
allCardFeedsMetadata: ResultMetadata<OnyxCollection<CardFeeds>>;
lastSelectedFeedMetadata: ResultMetadata<OnyxEntry<CompanyCardFeedWithDomainID>>;
cardListMetadata: ResultMetadata;
allCardFeedsMetadata: ResultMetadata;
lastSelectedFeedMetadata: ResultMetadata;
};
};

Expand Down
31 changes: 13 additions & 18 deletions src/hooks/useSidebarOrderedReports.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type * as OnyxTypes from '@src/types/onyx';

import type {OnyxEntry} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';

import React, {createContext, useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react';

import useCollectionDelta from './useCollectionDelta';
import {useCurrentReportIDState} from './useCurrentReportID';
import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails';
import useLocalize from './useLocalize';
import useMappedPolicies from './useMappedPolicies';
import useNetwork from './useNetwork';
import useOnyx from './useOnyx';
import usePrevious from './usePrevious';
import useReportAttributes from './useReportAttributes';
import useResponsiveLayout from './useResponsiveLayout';

type PartialPolicyForSidebar = Pick<OnyxTypes.Policy, 'type' | 'name' | 'avatarURL' | 'employeeList'>;

type SidebarOrderedReportsContextProviderProps = {
children: React.ReactNode;
currentReportIDForTests?: string;
Expand Down Expand Up @@ -67,14 +64,6 @@ const SidebarOrderedReportsActionsContext = createContext<SidebarOrderedReportsA
setStickyReportID: () => {},
});

const policyMapper = (policy: OnyxEntry<OnyxTypes.Policy>): PartialPolicyForSidebar =>
(policy && {
type: policy.type,
name: policy.name,
avatarURL: policy.avatarURL,
employeeList: policy.employeeList,
}) as PartialPolicyForSidebar;

// This file does not compile with React Compiler (render-time ref cache below keeps referential
// stability), so the manual useMemo/useCallback in this provider are load-bearing and must stay.
function SidebarOrderedReportsContextProvider({
Expand All @@ -93,12 +82,18 @@ function SidebarOrderedReportsContextProvider({
const [priorityMode = CONST.PRIORITY_MODE.DEFAULT] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE);
const [inboxTab = CONST.INBOX_TAB.ALL] = useOnyx(ONYXKEYS.NVP_INBOX_TAB);
const activeTab = inboxTab ?? CONST.INBOX_TAB.ALL;
const [chatReports, {sourceValue: reportUpdates}] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const [, {sourceValue: policiesUpdates}] = useMappedPolicies(policyMapper);
const [transactions, {sourceValue: transactionsUpdates}] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION);
const [transactionViolations, {sourceValue: transactionViolationsUpdates}] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
const [reportNameValuePairs, {sourceValue: reportNameValuePairsUpdates}] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS);
const [reportsDrafts, {sourceValue: reportsDraftsUpdates}] = useOnyx(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT);
const [chatReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const reportUpdates = useCollectionDelta(chatReports);
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);

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.

The old useMappedPolicies(policyMapper) only tracked type, name, avatarURL, and employeeList.

Could we use a selector here?

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 mapper actually never "tracked" only these properties: this consumer destructured [, {sourceValue: policiesUpdates}], discarding the mapped value, and useMappedPolicies maps after the useOnyx(COLLECTION.POLICY) subscription (no selector), so sourceValue was the raw changed policy members — and the code only uses their keys by doing Object.keys(policiesUpdates). So on main an LHN recompute already fired on any policy change (keyed by policyID), and useCollectionDelta(allPolicies) preserves that exact behavior (only the changed keys are consumed), so there's no regression — it's actually probably cheaper since it drops the unused inline map.

const policiesUpdates = useCollectionDelta(allPolicies);
const [transactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION);
const transactionsUpdates = useCollectionDelta(transactions);
const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
const transactionViolationsUpdates = useCollectionDelta(transactionViolations);
const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS);
const reportNameValuePairsUpdates = useCollectionDelta(reportNameValuePairs);
const [reportsDrafts] = useOnyx(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT);
const reportsDraftsUpdates = useCollectionDelta(reportsDrafts);
const [betas] = useOnyx(ONYXKEYS.BETAS);
const [conciergeReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID);
const reportAttributes = useReportAttributes();
Expand Down
2 changes: 1 addition & 1 deletion src/types/utils/isLoadingOnyxValue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {ResultMetadata} from 'react-native-onyx';

function isLoadingOnyxValue(...results: Array<ResultMetadata<unknown>>): boolean {
function isLoadingOnyxValue(...results: ResultMetadata[]): boolean {
return results.some((result) => result.status === 'loading');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const THREAD_REPORT_ID = '10002';
const PARENT_ACTION_ID = '9001';
const IOU_TRANSACTION_ID = '3106135972713435169';

const loadedMetadata: ResultMetadata<ReportActions> = {status: 'loaded'};
const loadedMetadata: ResultMetadata = {status: 'loaded'};

function asReportActionsOnyxResult(reportActions: ReportActions | undefined): UseOnyxResult<ReportActions> {
return [reportActions, loadedMetadata];
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/hooks/useTransactionThreadReportID.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function makeSentMoneyPayAction(extra: Partial<ReportAction> = {}): ReportAction
} as ReportAction;
}

const loadedReportMetadata: ResultMetadata<Report> = {status: 'loaded'};
const loadedReportMetadata: ResultMetadata = {status: 'loaded'};

function asReportOnyxResult(report: Report | undefined): UseOnyxResult<Report> {
return [report, loadedReportMetadata];
Expand Down
62 changes: 62 additions & 0 deletions tests/unit/useCollectionDeltaTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {renderHook} from '@testing-library/react-native';

import useCollectionDelta from '@hooks/useCollectionDelta';

import type {OnyxCollection} from 'react-native-onyx';

type Item = {v: number};

function renderUseCollectionDelta(initialProps: OnyxCollection<Item>) {
return renderHook((value: OnyxCollection<Item>) => useCollectionDelta(value), {initialProps});
}

describe('useCollectionDelta', () => {
it('should return undefined on the first render (nothing to diff against)', () => {
const {result} = renderUseCollectionDelta({a: {v: 1}});
expect(result.current).toBeUndefined();
});

it('should return the members added since the previous render', () => {
const a: Item = {v: 1};
const b: Item = {v: 2};
const {result, rerender} = renderUseCollectionDelta({a});

rerender({a, b});
expect(result.current).toStrictEqual({b});
});

it('should return a changed member with its current value', () => {
const a: Item = {v: 1};
const aNext: Item = {v: 2};
const {result, rerender} = renderUseCollectionDelta({a});

rerender({a: aNext});
expect(result.current).toStrictEqual({a: aNext});
});

it('should return a removed member as undefined', () => {
const a: Item = {v: 1};
const b: Item = {v: 2};
const {result, rerender} = renderUseCollectionDelta({a, b});

rerender({a});
expect(result.current).toStrictEqual({b: undefined});
});

it('should return undefined when the value reference is unchanged between renders', () => {
const collection: OnyxCollection<Item> = {a: {v: 1}};
const {result, rerender} = renderUseCollectionDelta(collection);

rerender(collection);
expect(result.current).toBeUndefined();
});

it('should return undefined when members keep their references across renders (structural sharing)', () => {
const a: Item = {v: 1};
const {result, rerender} = renderUseCollectionDelta({a});

// New container object, same member reference.
rerender({a});
expect(result.current).toBeUndefined();
});
});
67 changes: 67 additions & 0 deletions tests/unit/useReportActionsVisibilityTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {ReactNode} from 'react';

import Onyx from 'react-native-onyx';

import getOnyxValue from '../utils/getOnyxValue';
import {getFakeReportAction} from '../utils/ReportTestUtils';
import * as TestHelper from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
Expand All @@ -37,6 +38,16 @@ function buildActions(...reportActionIDs: string[]): ReportActions {
return Object.fromEntries(reportActionIDs.map((id) => [id, buildVisibleComment(id)]));
}

/** Build an ADD_COMMENT action whispered to specific accounts (visible only to those accounts). */
function buildWhisperedComment(reportActionID: string, whisperedTo: number[]) {
return getFakeReportAction(1, {
reportActionID,
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
message: [{html: 'hey', isDeletedParentAction: false, isEdited: false, text: 'test', type: 'COMMENT', whisperedTo}],
originalMessage: {whisperedTo},
});
}

function setReportActions(reportID: string, actions: ReportActions) {
return Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, actions);
}
Expand Down Expand Up @@ -129,3 +140,59 @@ describe('useReportActionsVisibility scopes VISIBLE_REPORT_ACTIONS per report',
unmount();
});
});

describe('VISIBLE_REPORT_ACTIONS reflects deleted report actions', () => {
beforeAll(() => {
Onyx.init({keys: ONYXKEYS});
initOnyxDerivedValues();
});

beforeEach(async () => {
await Onyx.clear();
TestHelper.signInWithTestUser(1, 'test@test.com');
await waitForBatchedUpdates();
});

it('recomputes visibility on a session change (account switch) even when no report action changed', async () => {
// Given report A holds a whisper targeted to account 1, and account 1 is signed in.
// (signInWithTestUser in beforeEach signs in accountID 1.)
await setReportActions(REPORT_A, {whisper: buildWhisperedComment('whisper', [1])});
await waitForBatchedUpdates();

// The whisper is visible to its target (account 1).
const visibleAsTarget = await getOnyxValue(ONYXKEYS.DERIVED.VISIBLE_REPORT_ACTIONS);
expect(visibleAsTarget?.[REPORT_A]?.whisper).toBe(true);

// When the session switches to a different user (account 2) without touching the report's actions
await act(async () => {
await Onyx.merge(ONYXKEYS.SESSION, {accountID: 2});
await waitForBatchedUpdates();
});

// Then the derived value is recomputed against the new user: the whisper now targets "others" and is hidden.
// This only flips if the SESSION delta forces a recompute despite REPORT_ACTIONS being unchanged.
const visibleAsOther = await getOnyxValue(ONYXKEYS.DERIVED.VISIBLE_REPORT_ACTIONS);
expect(visibleAsOther?.[REPORT_A]?.whisper).toBe(false);
});

it('drops a deleted action from the derived value via the incremental delta path', async () => {
// Given a report with two visible actions (so the derived value has a cached entry to update incrementally)
await setReportActions(REPORT_A, buildActions('a1', 'a2'));
await waitForBatchedUpdates();

const visibleBefore = await getOnyxValue(ONYXKEYS.DERIVED.VISIBLE_REPORT_ACTIONS);
expect(Object.keys(visibleBefore?.[REPORT_A] ?? {}).sort()).toEqual(['a1', 'a2']);

// When one action is deleted (a null tombstone merge, exactly how report action deletion works)
await act(async () => {
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_A}`, {a1: null});
await waitForBatchedUpdates();
});

// Then the deleted action is gone from the derived visibility map (not left stale).
// The member-level delta no longer carries the per-action `null`, so this only passes
// because the changed report is rebuilt from its full current action set.
const visibleAfter = await getOnyxValue(ONYXKEYS.DERIVED.VISIBLE_REPORT_ACTIONS);
expect(Object.keys(visibleAfter?.[REPORT_A] ?? {})).toEqual(['a2']);
});
});
Loading