|
1 | 1 | import cloneDeep from 'lodash/cloneDeep';
|
2 |
| -import type {OnyxKey, OnyxMergeCollectionInput, OnyxValue} from 'react-native-onyx'; |
| 2 | +import type {OnyxCollection, OnyxEntry, OnyxKey} from 'react-native-onyx'; |
3 | 3 | import Onyx from 'react-native-onyx';
|
4 |
| -import type {UnknownRecord, ValueOf} from 'type-fest'; |
| 4 | +import type {UnknownRecord} from 'type-fest'; |
5 | 5 | import {KEYS_TO_PRESERVE} from '@libs/actions/App';
|
6 |
| -import type {OnyxCollectionKey, OnyxCollectionValuesMapping, OnyxValues} from '@src/ONYXKEYS'; |
| 6 | +import type {OnyxCollectionValuesMapping, OnyxValues} from '@src/ONYXKEYS'; |
7 | 7 | import ONYXKEYS from '@src/ONYXKEYS';
|
8 | 8 |
|
9 | 9 | // List of Onyx keys from the .txt file we want to keep for the local override
|
@@ -55,42 +55,41 @@ function cleanAndTransformState<T>(state: string): T {
|
55 | 55 | }
|
56 | 56 |
|
57 | 57 | const processStateImport = (transformedState: OnyxValues) => {
|
58 |
| - const collectionKeys = new Set(Object.values(ONYXKEYS.COLLECTION)); |
59 |
| - const collectionsMap = new Map<keyof OnyxCollectionValuesMapping, ValueOf<OnyxValues>>(); |
60 |
| - const regularState: Partial<OnyxValues> = {}; |
| 58 | + const collectionKeys = [...new Set(Object.values(ONYXKEYS.COLLECTION))]; |
| 59 | + const collectionsMap = new Map<keyof OnyxCollectionValuesMapping, OnyxCollection<OnyxKey>>(); |
| 60 | + const regularState: Partial<Record<OnyxKey, OnyxEntry<OnyxKey>>> = {}; |
61 | 61 |
|
62 | 62 | Object.entries(transformedState).forEach(([key, value]) => {
|
63 |
| - const baseKey = key.split('_').at(0); |
64 |
| - const collectionKey = `${baseKey}_` as OnyxCollectionKey; |
65 |
| - |
66 |
| - if (collectionKeys.has(collectionKey)) { |
| 63 | + const collectionKey = collectionKeys.find((cKey) => key.startsWith(cKey)); |
| 64 | + if (collectionKey) { |
67 | 65 | if (!collectionsMap.has(collectionKey)) {
|
68 | 66 | collectionsMap.set(collectionKey, {});
|
69 | 67 | }
|
| 68 | + |
70 | 69 | const collection = collectionsMap.get(collectionKey);
|
71 | 70 | if (!collection) {
|
72 | 71 | return;
|
73 | 72 | }
|
74 | 73 |
|
75 |
| - collection[key] = value; |
| 74 | + collection[key] = value as OnyxEntry<OnyxKey>; |
76 | 75 | } else {
|
77 |
| - regularState[key] = value; |
| 76 | + regularState[key as OnyxKey] = value as OnyxEntry<OnyxKey>; |
78 | 77 | }
|
79 | 78 | });
|
80 | 79 |
|
81 | 80 | return Onyx.clear(KEYS_TO_PRESERVE)
|
82 | 81 | .then(() => {
|
83 | 82 | const collectionPromises = Array.from(collectionsMap.entries()).map(([baseKey, items]) => {
|
84 |
| - return Onyx.setCollection(baseKey, items); |
| 83 | + return items ? Onyx.setCollection(baseKey, items) : Promise.resolve(); |
85 | 84 | });
|
86 | 85 | return Promise.all(collectionPromises);
|
87 | 86 | })
|
88 | 87 | .then(() => {
|
89 | 88 | if (Object.keys(regularState).length > 0) {
|
90 |
| - return Onyx.multiSet(regularState); |
| 89 | + return Onyx.multiSet(regularState as Partial<OnyxValues>); |
91 | 90 | }
|
92 | 91 | return Promise.resolve();
|
93 | 92 | });
|
94 | 93 | };
|
95 | 94 |
|
96 |
| -export {transformNumericKeysToArray, cleanAndTransformState, processStateImport}; |
| 95 | +export {cleanAndTransformState, processStateImport, transformNumericKeysToArray}; |
0 commit comments