cogsbox-state 0.5.282 → 0.5.284

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/store.ts CHANGED
@@ -102,7 +102,11 @@ export type CogsGlobalState = {
102
102
  shadowStateStore: { [key: string]: any };
103
103
  initializeShadowState: (key: string, initialState: any) => void;
104
104
  updateShadowAtPath: (key: string, path: string[], newValue: any) => void;
105
- insertShadowArrayElement: (key: string, arrayPath: string[]) => void;
105
+ insertShadowArrayElement: (
106
+ key: string,
107
+ arrayPath: string[],
108
+ newItem: any
109
+ ) => void;
106
110
  removeShadowArrayElement: (
107
111
  key: string,
108
112
  arrayPath: string[],
@@ -325,17 +329,41 @@ export const getGlobalStore = create<CogsGlobalState>((set, get) => ({
325
329
  });
326
330
  },
327
331
 
328
- insertShadowArrayElement: (key: string, arrayPath: string[]) => {
332
+ insertShadowArrayElement: (
333
+ key: string,
334
+ arrayPath: string[],
335
+ newItem: any
336
+ ) => {
329
337
  set((state) => {
330
338
  const newShadow = { ...state.shadowStateStore };
331
- let current = newShadow[key];
339
+ if (!newShadow[key]) return state;
340
+
341
+ newShadow[key] = JSON.parse(JSON.stringify(newShadow[key]));
342
+
343
+ let current: any = newShadow[key];
332
344
 
333
345
  for (const segment of arrayPath) {
334
- current = current?.[segment];
346
+ current = current[segment];
347
+ if (!current) return state;
335
348
  }
336
349
 
337
350
  if (Array.isArray(current)) {
338
- current.push({});
351
+ // Create shadow structure based on the actual new item
352
+ const createShadowStructure = (obj: any): any => {
353
+ if (Array.isArray(obj)) {
354
+ return obj.map((item) => createShadowStructure(item));
355
+ }
356
+ if (typeof obj === "object" && obj !== null) {
357
+ const shadow: any = {};
358
+ for (const k in obj) {
359
+ shadow[k] = createShadowStructure(obj[k]);
360
+ }
361
+ return shadow;
362
+ }
363
+ return {}; // Leaf nodes get empty object for metadata
364
+ };
365
+
366
+ current.push(createShadowStructure(newItem));
339
367
  }
340
368
 
341
369
  return { shadowStateStore: newShadow };