Skip to content

Commit a0a25ee

Browse files
committed
Fix types and logic
1 parent d0b53df commit a0a25ee

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/components/ImportOnyxState/utils.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
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';
33
import Onyx from 'react-native-onyx';
4-
import type {UnknownRecord, ValueOf} from 'type-fest';
4+
import type {UnknownRecord} from 'type-fest';
55
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';
77
import ONYXKEYS from '@src/ONYXKEYS';
88

99
// 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 {
5555
}
5656

5757
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>>> = {};
6161

6262
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) {
6765
if (!collectionsMap.has(collectionKey)) {
6866
collectionsMap.set(collectionKey, {});
6967
}
68+
7069
const collection = collectionsMap.get(collectionKey);
7170
if (!collection) {
7271
return;
7372
}
7473

75-
collection[key] = value;
74+
collection[key] = value as OnyxEntry<OnyxKey>;
7675
} else {
77-
regularState[key] = value;
76+
regularState[key as OnyxKey] = value as OnyxEntry<OnyxKey>;
7877
}
7978
});
8079

8180
return Onyx.clear(KEYS_TO_PRESERVE)
8281
.then(() => {
8382
const collectionPromises = Array.from(collectionsMap.entries()).map(([baseKey, items]) => {
84-
return Onyx.setCollection(baseKey, items);
83+
return items ? Onyx.setCollection(baseKey, items) : Promise.resolve();
8584
});
8685
return Promise.all(collectionPromises);
8786
})
8887
.then(() => {
8988
if (Object.keys(regularState).length > 0) {
90-
return Onyx.multiSet(regularState);
89+
return Onyx.multiSet(regularState as Partial<OnyxValues>);
9190
}
9291
return Promise.resolve();
9392
});
9493
};
9594

96-
export {transformNumericKeysToArray, cleanAndTransformState, processStateImport};
95+
export {cleanAndTransformState, processStateImport, transformNumericKeysToArray};

0 commit comments

Comments
 (0)