@@ -12,7 +12,6 @@ import * as PerformanceUtils from './PerformanceUtils';
1212import * as Str from './Str' ;
1313import unstable_batchedUpdates from './batch' ;
1414import Storage from './storage' ;
15- import * as CollectionKeyUtils from './CollectionKeyUtils' ;
1615import type {
1716 CollectionKey ,
1817 CollectionKeyBase ,
@@ -154,9 +153,6 @@ function initStoreValues(keys: DeepRecord<string, OnyxKey>, initialKeyStates: Pa
154153 return acc ;
155154 } , new Set < OnyxKey > ( ) ) ;
156155
157- // Initialize the shared collection key utility
158- CollectionKeyUtils . setCollectionKeys ( onyxCollectionKeySet ) ;
159-
160156 // Set our default key states to use when initializing and clearing Onyx data
161157 defaultKeyStates = initialKeyStates ;
162158
@@ -462,11 +458,11 @@ function getCollectionKeys(): Set<OnyxKey> {
462458 * is associated with a collection of keys.
463459 */
464460function isCollectionKey ( key : OnyxKey ) : key is CollectionKeyBase {
465- return CollectionKeyUtils . isCollectionKey ( key ) ;
461+ return onyxCollectionKeySet . has ( key ) ;
466462}
467463
468464function isCollectionMemberKey < TCollectionKey extends CollectionKeyBase > ( collectionKey : TCollectionKey , key : string ) : key is `${TCollectionKey } ${string } ` {
469- return CollectionKeyUtils . isCollectionMemberKey ( collectionKey , key ) ;
465+ return key . startsWith ( collectionKey ) && key . length > collectionKey . length ;
470466}
471467
472468/**
@@ -497,7 +493,7 @@ function splitCollectionMemberKey<TKey extends CollectionKey, CollectionKeyType
497493 * or if the provided key is a collection member key (in case our configured key is a "collection key")
498494 */
499495function isKeyMatch ( configKey : OnyxKey , key : OnyxKey ) : boolean {
500- return CollectionKeyUtils . isKeyMatch ( configKey , key ) ;
496+ return isCollectionKey ( configKey ) ? Str . startsWith ( key , configKey ) : configKey === key ;
501497}
502498
503499/**
@@ -513,7 +509,24 @@ function isKeyMatch(configKey: OnyxKey, key: OnyxKey): boolean {
513509 * @returns The plain collection key or throws an Error if the key is not a collection one.
514510 */
515511function getCollectionKey ( key : CollectionKey ) : string {
516- return CollectionKeyUtils . getCollectionKey ( key ) ;
512+ // Start by finding the position of the last underscore in the string
513+ let lastUnderscoreIndex = key . lastIndexOf ( '_' ) ;
514+
515+ // Iterate backwards to find the longest key that ends with '_'
516+ while ( lastUnderscoreIndex > 0 ) {
517+ const possibleKey = key . slice ( 0 , lastUnderscoreIndex + 1 ) ;
518+
519+ // Check if the substring is a key in the Set
520+ if ( isCollectionKey ( possibleKey ) ) {
521+ // Return the matching key and the rest of the string
522+ return possibleKey ;
523+ }
524+
525+ // Move to the next underscore to check smaller possible keys
526+ lastUnderscoreIndex = key . lastIndexOf ( '_' , lastUnderscoreIndex - 1 ) ;
527+ }
528+
529+ throw new Error ( `Invalid '${ key } ' key provided, only collection keys are allowed.` ) ;
517530}
518531
519532/**
0 commit comments