cogsbox-state 0.5.455 → 0.5.456
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/dist/CogsState.d.ts +7 -5
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.jsx +1206 -1196
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +40 -14
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -38,6 +38,7 @@ import { Operation } from 'fast-json-patch';
|
|
|
38
38
|
import { useInView } from 'react-intersection-observer';
|
|
39
39
|
import * as z3 from 'zod/v3';
|
|
40
40
|
import * as z4 from 'zod/v4';
|
|
41
|
+
import z from 'zod';
|
|
41
42
|
|
|
42
43
|
type Prettify<T> = T extends any ? { [K in keyof T]: T[K] } : never;
|
|
43
44
|
|
|
@@ -560,31 +561,35 @@ type CogsApi<
|
|
|
560
561
|
useCogsState: UseCogsStateHook<T, apiParams>;
|
|
561
562
|
setCogsOptions: SetCogsOptionsFunc<T>;
|
|
562
563
|
};
|
|
563
|
-
|
|
564
|
-
// Minimal change - just add a second parameter to detect sync schema
|
|
565
564
|
export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
566
565
|
initialState: State,
|
|
567
566
|
opt?: {
|
|
568
567
|
formElements?: FormsElementsType<State>;
|
|
569
568
|
validation?: ValidationOptionsType;
|
|
570
|
-
// Add this flag to indicate it's from sync schema
|
|
571
569
|
__fromSyncSchema?: boolean;
|
|
572
570
|
__syncNotifications?: Record<string, Function>;
|
|
571
|
+
__apiParamsMap?: Record<string, any>; // Add this
|
|
573
572
|
}
|
|
574
573
|
) => {
|
|
575
|
-
// Keep ALL your existing code exactly the same
|
|
576
574
|
let newInitialState = initialState;
|
|
577
575
|
|
|
578
576
|
const [statePart, initialOptionsPart] =
|
|
579
577
|
transformStateFunc<State>(newInitialState);
|
|
580
578
|
|
|
581
|
-
//
|
|
579
|
+
// Store notifications if provided
|
|
582
580
|
if (opt?.__fromSyncSchema && opt?.__syncNotifications) {
|
|
583
581
|
getGlobalStore
|
|
584
582
|
.getState()
|
|
585
583
|
.setInitialStateOptions('__notifications', opt.__syncNotifications);
|
|
586
584
|
}
|
|
587
585
|
|
|
586
|
+
// Store apiParams map if provided
|
|
587
|
+
if (opt?.__fromSyncSchema && opt?.__apiParamsMap) {
|
|
588
|
+
getGlobalStore
|
|
589
|
+
.getState()
|
|
590
|
+
.setInitialStateOptions('__apiParamsMap', opt.__apiParamsMap);
|
|
591
|
+
}
|
|
592
|
+
|
|
588
593
|
// ... rest of your existing createCogsState code unchanged ...
|
|
589
594
|
|
|
590
595
|
Object.keys(statePart).forEach((key) => {
|
|
@@ -637,14 +642,20 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
|
637
642
|
stateKey: StateKey,
|
|
638
643
|
options?: Prettify<OptionsType<(typeof statePart)[StateKey]>>
|
|
639
644
|
) => {
|
|
640
|
-
// ... your existing useCogsState implementation ...
|
|
641
645
|
const [componentId] = useState(options?.componentId ?? uuidv4());
|
|
646
|
+
const apiParamsSchema = opt?.__apiParamsMap?.[stateKey as string];
|
|
647
|
+
|
|
648
|
+
// Merge apiParams into options
|
|
649
|
+
const enhancedOptions = {
|
|
650
|
+
...options,
|
|
651
|
+
apiParamsSchema, // Add the schema here
|
|
652
|
+
} as any;
|
|
653
|
+
|
|
642
654
|
setOptions({
|
|
643
655
|
stateKey,
|
|
644
|
-
options:
|
|
656
|
+
options: enhancedOptions,
|
|
645
657
|
initialOptionsPart,
|
|
646
658
|
});
|
|
647
|
-
|
|
648
659
|
const thiState =
|
|
649
660
|
getGlobalStore.getState().getShadowValue(stateKey as string) ||
|
|
650
661
|
statePart[stateKey as string];
|
|
@@ -683,12 +694,14 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
|
683
694
|
|
|
684
695
|
return { useCogsState, setCogsOptions } as CogsApi<State>;
|
|
685
696
|
};
|
|
697
|
+
|
|
686
698
|
export function createCogsStateFromSync<
|
|
687
699
|
TSyncSchema extends {
|
|
688
700
|
schemas: Record<
|
|
689
701
|
string,
|
|
690
702
|
{
|
|
691
703
|
schemas: { defaultValues: any };
|
|
704
|
+
apiParamsSchema?: any; // This contains the zod schema for params
|
|
692
705
|
[key: string]: any;
|
|
693
706
|
}
|
|
694
707
|
>;
|
|
@@ -701,21 +714,32 @@ export function createCogsStateFromSync<
|
|
|
701
714
|
[K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['schemas']['defaultValues'];
|
|
702
715
|
},
|
|
703
716
|
{
|
|
704
|
-
[K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
}]
|
|
717
|
+
[K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['apiParamsSchema'] extends z.ZodObject<any>
|
|
718
|
+
? z.infer<TSyncSchema['schemas'][K]['apiParamsSchema']>
|
|
719
|
+
: never;
|
|
720
|
+
}[keyof TSyncSchema['schemas']]
|
|
708
721
|
> {
|
|
709
722
|
const schemas = syncSchema.schemas;
|
|
710
723
|
const initialState: any = {};
|
|
724
|
+
const apiParamsMap: any = {};
|
|
711
725
|
|
|
712
|
-
// Extract defaultValues from each entry
|
|
726
|
+
// Extract defaultValues AND apiParams from each entry
|
|
713
727
|
for (const key in schemas) {
|
|
714
728
|
const entry = schemas[key];
|
|
715
729
|
initialState[key] = entry?.schemas?.defaultValues || {};
|
|
730
|
+
|
|
731
|
+
// Store the apiParamsSchema for each key
|
|
732
|
+
if (entry?.apiParamsSchema) {
|
|
733
|
+
apiParamsMap[key] = entry.apiParamsSchema;
|
|
734
|
+
}
|
|
716
735
|
}
|
|
717
736
|
|
|
718
|
-
|
|
737
|
+
// Pass the sync schema metadata to createCogsState
|
|
738
|
+
return createCogsState(initialState, {
|
|
739
|
+
__fromSyncSchema: true,
|
|
740
|
+
__syncNotifications: syncSchema.notifications,
|
|
741
|
+
__apiParamsMap: apiParamsMap, // Pass the apiParams schemas
|
|
742
|
+
}) as any;
|
|
719
743
|
}
|
|
720
744
|
|
|
721
745
|
const {
|
|
@@ -1044,10 +1068,12 @@ export function useCogsStateFn<TStateObject extends unknown>(
|
|
|
1044
1068
|
syncUpdate,
|
|
1045
1069
|
dependencies,
|
|
1046
1070
|
serverState,
|
|
1071
|
+
apiParamsSchema,
|
|
1047
1072
|
}: {
|
|
1048
1073
|
stateKey?: string;
|
|
1049
1074
|
componentId?: string;
|
|
1050
1075
|
defaultState?: TStateObject;
|
|
1076
|
+
apiParamsSchema?: z.ZodObject<any>; // Add this type
|
|
1051
1077
|
} & OptionsType<TStateObject> = {}
|
|
1052
1078
|
) {
|
|
1053
1079
|
const [reactiveForce, forceUpdate] = useState({}); //this is the key to reactivity
|