Skip to content
Closed
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
49 changes: 29 additions & 20 deletions src/hooks/useOnyx.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import isEmpty from 'lodash/isEmpty';
import isPlainObject from 'lodash/isPlainObject';
import {use, useMemo} from 'react';
import type {DependencyList} from 'react';
// eslint-disable-next-line no-restricted-imports
Expand Down Expand Up @@ -58,35 +60,42 @@
}

const useOnyxOptions = options as UseOnyxOptions<OnyxKey, OnyxValue<OnyxKey>> | undefined;
const {selector: selectorProp, ...optionsWithoutSelector} = useOnyxOptions ?? {};
const {selector, ...optionsWithoutSelector} = useOnyxOptions ?? {};

// Determine if we should use snapshot data based on search state and key
const shouldUseSnapshot = isOnSearch && !!currentSearchHash && isSnapshotCompatibleKey;
// Original Onyx
const originalOnyxOptions: UseOnyxOptions<OnyxKey, OnyxValue<OnyxKey>> = {...optionsWithoutSelector, selector, allowDynamicKey: true};
const originalResult = originalUseOnyx(key, originalOnyxOptions, dependencies);

// Create selector function that handles both regular and snapshot data
const selector = useMemo(() => {
if (!selectorProp || !shouldUseSnapshot) {
return selectorProp;
// Snapshot Onyx
const shouldUseSnapshot = isOnSearch && !!currentSearchHash && isSnapshotCompatibleKey;
const snapshotSelector = useMemo(() => {
if (!selector) {
return (data: OnyxValue<OnyxKey> | undefined) => getKeyData(data as SearchResults, key) as OnyxValue<OnyxKey>;
}

return (data: OnyxValue<OnyxKey> | undefined) => selectorProp(getKeyData(data as SearchResults, key));
}, [selectorProp, shouldUseSnapshot, key]);
return (data: OnyxValue<OnyxKey> | undefined) => selector(getKeyData(data as SearchResults, key));
}, [selector, key]);
const snapshotOnyxOptions: UseOnyxOptions<OnyxKey, OnyxValue<OnyxKey>> = {...optionsWithoutSelector, selector: snapshotSelector, allowDynamicKey: true};
const snapshotKey = `${ONYXKEYS.COLLECTION.SNAPSHOT}${currentSearchHash}` as OnyxKey;
const snapshotResult = shouldUseSnapshot ? originalUseOnyx(snapshotKey, snapshotOnyxOptions, dependencies) : [undefined, {status: 'loaded'}];

Check warning on line 80 in src/hooks/useOnyx.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

The 'snapshotResult' conditional could make the dependencies of useMemo Hook (at line 98) change on every render. Move it inside the useMemo callback. Alternatively, wrap the initialization of 'snapshotResult' in its own useMemo() Hook

Check warning on line 80 in src/hooks/useOnyx.ts

View workflow job for this annotation

GitHub Actions / ESLint check

The 'snapshotResult' conditional could make the dependencies of useMemo Hook (at line 98) change on every render. Move it inside the useMemo callback. Alternatively, wrap the initialization of 'snapshotResult' in its own useMemo() Hook

const onyxOptions: UseOnyxOptions<OnyxKey, OnyxValue<OnyxKey>> = {...optionsWithoutSelector, selector, allowDynamicKey: true};
const snapshotKey = shouldUseSnapshot ? (`${ONYXKEYS.COLLECTION.SNAPSHOT}${currentSearchHash}` as OnyxKey) : key;
const result = useMemo((): UseOnyxResult<TReturnValue> => {
// Merge snapshot data with live data if possible
if (shouldUseSnapshot) {
if (isPlainObject(originalResult[0]) && isPlainObject(snapshotResult[0])) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return [{...(snapshotResult[0] as Record<string, any>), ...(originalResult[0] as Record<string, any>)}, originalResult[1]] as UseOnyxResult<TReturnValue>;
}

const originalResult = originalUseOnyx(snapshotKey, onyxOptions, dependencies);
if (isEmpty(snapshotResult[0])) {
return originalResult as UseOnyxResult<TReturnValue>;
}

// Extract and memoize the specific key data from snapshot if in search mode
const result = useMemo((): UseOnyxResult<TReturnValue> => {
// if it has selector, we don't need to use snapshot here
if (!shouldUseSnapshot || selector) {
return originalResult as UseOnyxResult<TReturnValue>;
return snapshotResult as UseOnyxResult<TReturnValue>;
}

const keyData = getKeyData(originalResult[0] as SearchResults, key);
return [keyData, originalResult[1]] as UseOnyxResult<TReturnValue>;
}, [shouldUseSnapshot, originalResult, key, selector]);
return originalResult as UseOnyxResult<TReturnValue>;
}, [shouldUseSnapshot, originalResult, snapshotResult]);

return result;
};
Expand Down
8 changes: 7 additions & 1 deletion tests/perf-test/ModifiedExpenseMessage.perf-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
beforeAll(() =>
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
}),
);

Expand Down
8 changes: 7 additions & 1 deletion tests/perf-test/ReportActionCompose.perf-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ jest.mock('@src/libs/actions/EmojiPickerAction', () => {
beforeAll(() =>
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
}),
);

Expand Down
8 changes: 7 additions & 1 deletion tests/perf-test/ReportActionsList.perf-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ jest.mock('@src/components/ConfirmedRoute.tsx');
beforeAll(() =>
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
}),
);

Expand Down
8 changes: 7 additions & 1 deletion tests/perf-test/ReportActionsUtils.perf-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ describe('ReportActionsUtils', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});

Onyx.multiSet({
Expand Down
8 changes: 7 additions & 1 deletion tests/perf-test/ReportUtils.perf-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ describe('ReportUtils', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});
});

Expand Down
8 changes: 7 additions & 1 deletion tests/perf-test/SidebarLinks.perf-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ describe('SidebarLinks', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});
});

Expand Down
8 changes: 7 additions & 1 deletion tests/perf-test/SidebarUtils.perf-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ describe('SidebarUtils', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});

Onyx.multiSet({
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/ComposerTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ describe('Composer', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});
});

Expand Down
8 changes: 7 additions & 1 deletion tests/ui/DebugReportActionsTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ describe('DebugReportActions', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});
act(() => {
Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, CONST.LOCALES.EN);
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/LHNItemsPresence.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ describe('SidebarLinksData', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});
initOnyxDerivedValues();
});
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/PureReportActionItemTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ describe('PureReportActionItem', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});
jest.spyOn(NativeNavigation, 'useRoute').mockReturnValue({key: '', name: ''});
jest.spyOn(ReportActionUtils, 'getIOUActionForReportID').mockImplementation(getIOUActionForReportID);
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/ReportActionComposeTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ describe('ReportActionCompose Integration Tests', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});
});

Expand Down
8 changes: 7 additions & 1 deletion tests/ui/ReportActionItemMessageEditTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ describe('ReportActionCompose Integration Tests', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});
});

Expand Down
8 changes: 7 additions & 1 deletion tests/ui/ReportAttachments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ describe('ReportAttachments', () => {
[ONYXKEYS.SESSION]: {accountID: TEST_USER_ACCOUNT_ID, email: TEST_USER_LOGIN},
[ONYXKEYS.PERSONAL_DETAILS_LIST]: {[TEST_USER_ACCOUNT_ID]: {accountID: TEST_USER_ACCOUNT_ID, login: TEST_USER_LOGIN}},
},
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});
});
beforeEach(async () => {
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/ReportDetailsPageTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ describe('ReportDetailsPage', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});
});

Expand Down
8 changes: 7 additions & 1 deletion tests/ui/ReportListItemHeaderTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ describe('ReportListItemHeader', () => {
beforeAll(() =>
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
}),
);

Expand Down
8 changes: 7 additions & 1 deletion tests/ui/TransactionItemRowRBRTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ describe('TransactionItemRowRBR', () => {
beforeAll(async () => {
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});
await Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, CONST.LOCALES.DEFAULT);
});
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/WorkspaceCategoriesTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ describe('WorkspaceCategories', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,13 @@ describe('IOURequestStepConfirmationPageTest', () => {
jest.clearAllMocks();
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
});
});

Expand Down
8 changes: 7 additions & 1 deletion tests/unit/PersistedRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ const request: Request = {
beforeAll(() =>
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
}),
);

Expand Down
8 changes: 7 additions & 1 deletion tests/unit/ReportActionItemSingleTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ describe('ReportActionItemSingle', () => {
beforeAll(() =>
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
}),
);

Expand Down
8 changes: 7 additions & 1 deletion tests/unit/ReportActionsUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ describe('ReportActionsUtils', () => {
beforeAll(() =>
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
evictableKeys: [
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
ONYXKEYS.COLLECTION.SNAPSHOT,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES,
ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS,
],
}),
);

Expand Down
Loading
Loading