cogsbox-state 0.5.442 → 0.5.443
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 +24 -18
- package/dist/CogsState.d.ts.map +1 -1
- package/dist/CogsState.jsx +3 -4
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +49 -44
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -34,10 +34,10 @@ import {
|
|
|
34
34
|
type ComponentsType,
|
|
35
35
|
} from './store.js';
|
|
36
36
|
import { useCogsConfig } from './CogsStateClient.js';
|
|
37
|
-
import {
|
|
37
|
+
import { Operation } from 'fast-json-patch';
|
|
38
38
|
import { useInView } from 'react-intersection-observer';
|
|
39
39
|
import * as z3 from 'zod/v3';
|
|
40
|
-
import * as z4 from 'zod/v4';
|
|
40
|
+
import z, * as z4 from 'zod/v4';
|
|
41
41
|
|
|
42
42
|
type Prettify<T> = T extends any ? { [K in keyof T]: T[K] } : never;
|
|
43
43
|
|
|
@@ -374,7 +374,7 @@ type ValidationOptionsType = {
|
|
|
374
374
|
|
|
375
375
|
onBlur?: boolean;
|
|
376
376
|
};
|
|
377
|
-
export type OptionsType<T extends unknown = unknown> = {
|
|
377
|
+
export type OptionsType<T extends unknown = unknown, TApiParams = never> = {
|
|
378
378
|
log?: boolean;
|
|
379
379
|
componentId?: string;
|
|
380
380
|
cogsSync?: (stateObject: StateObject<T>) => SyncApi;
|
|
@@ -425,6 +425,7 @@ export type OptionsType<T extends unknown = unknown> = {
|
|
|
425
425
|
syncUpdate?: Partial<UpdateTypeDetail>;
|
|
426
426
|
|
|
427
427
|
defaultState?: T;
|
|
428
|
+
apiParams?: TApiParams;
|
|
428
429
|
dependencies?: any[];
|
|
429
430
|
};
|
|
430
431
|
|
|
@@ -533,12 +534,36 @@ export function addStateOptions<T extends unknown>(
|
|
|
533
534
|
) {
|
|
534
535
|
return { initialState: initialState, formElements, validation } as T;
|
|
535
536
|
}
|
|
536
|
-
type
|
|
537
|
-
|
|
537
|
+
type CogsSyncSchema = {
|
|
538
|
+
schemas: Record<
|
|
539
|
+
string,
|
|
540
|
+
{
|
|
541
|
+
schemas: { defaultValues: any };
|
|
542
|
+
apiParamsSchema?: z.ZodObject<any, any>;
|
|
543
|
+
[key: string]: any;
|
|
544
|
+
}
|
|
545
|
+
>;
|
|
546
|
+
notifications: Record<string, any>;
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
type UseCogsStateHook<TSchema extends CogsSyncSchema> = <
|
|
550
|
+
// TStateKey is now DIRECTLY a key of the schemas object. No ambiguity.
|
|
551
|
+
TStateKey extends keyof TSchema['schemas'],
|
|
538
552
|
>(
|
|
539
|
-
stateKey:
|
|
540
|
-
options?: Prettify<
|
|
541
|
-
|
|
553
|
+
stateKey: TStateKey,
|
|
554
|
+
options?: Prettify<
|
|
555
|
+
OptionsType<
|
|
556
|
+
// The state slice type is derived directly from the schema.
|
|
557
|
+
TSchema['schemas'][TStateKey]['schemas']['defaultValues'],
|
|
558
|
+
// The API params type is also derived directly and safely.
|
|
559
|
+
TSchema['schemas'][TStateKey] extends {
|
|
560
|
+
apiParamsSchema: z.ZodObject<any, any>;
|
|
561
|
+
}
|
|
562
|
+
? z.infer<TSchema['schemas'][TStateKey]['apiParamsSchema']>
|
|
563
|
+
: never
|
|
564
|
+
>
|
|
565
|
+
>
|
|
566
|
+
) => StateObject<TSchema['schemas'][TStateKey]['schemas']['defaultValues']>;
|
|
542
567
|
|
|
543
568
|
// Define the type for the options setter using the Transformed state
|
|
544
569
|
type SetCogsOptionsFunc<T extends Record<string, any>> = <
|
|
@@ -549,9 +574,9 @@ type SetCogsOptionsFunc<T extends Record<string, any>> = <
|
|
|
549
574
|
) => void;
|
|
550
575
|
|
|
551
576
|
// Define the final API object shape
|
|
552
|
-
type CogsApi<
|
|
553
|
-
useCogsState: UseCogsStateHook<
|
|
554
|
-
setCogsOptions: SetCogsOptionsFunc<
|
|
577
|
+
type CogsApi<TSchema extends CogsSyncSchema> = {
|
|
578
|
+
useCogsState: UseCogsStateHook<TSchema>;
|
|
579
|
+
setCogsOptions: SetCogsOptionsFunc<TSchema>;
|
|
555
580
|
};
|
|
556
581
|
|
|
557
582
|
// Minimal change - just add a second parameter to detect sync schema
|
|
@@ -674,45 +699,25 @@ export const createCogsState = <State extends Record<StateKeys, unknown>>(
|
|
|
674
699
|
notifyComponents(stateKey as string);
|
|
675
700
|
}
|
|
676
701
|
|
|
677
|
-
return { useCogsState, setCogsOptions }
|
|
702
|
+
return { useCogsState, setCogsOptions };
|
|
678
703
|
};
|
|
679
|
-
export function createCogsStateFromSync<
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
notifications: Record<string, any>;
|
|
689
|
-
},
|
|
690
|
-
>(
|
|
691
|
-
syncSchema: TSyncSchema
|
|
692
|
-
): CogsApi<{
|
|
693
|
-
[K in keyof TSyncSchema['schemas']]: TSyncSchema['schemas'][K]['schemas']['defaultValues'];
|
|
694
|
-
}> {
|
|
704
|
+
export function createCogsStateFromSync<TSchema extends CogsSyncSchema>(
|
|
705
|
+
syncSchema: TSchema
|
|
706
|
+
): CogsApi<TSchema> {
|
|
707
|
+
if (syncSchema.notifications) {
|
|
708
|
+
getGlobalStore
|
|
709
|
+
.getState()
|
|
710
|
+
.setInitialStateOptions('__notifications', syncSchema.notifications);
|
|
711
|
+
}
|
|
712
|
+
|
|
695
713
|
const schemas = syncSchema.schemas;
|
|
696
714
|
const initialState: any = {};
|
|
697
|
-
|
|
698
|
-
// Extract defaultValues from each entry
|
|
699
715
|
for (const key in schemas) {
|
|
700
|
-
|
|
701
|
-
initialState[key] = entry?.schemas?.defaultValues || {};
|
|
716
|
+
initialState[key] = schemas[key]?.schemas?.defaultValues || {};
|
|
702
717
|
}
|
|
703
718
|
|
|
704
|
-
//
|
|
705
|
-
|
|
706
|
-
// const currentOptions =
|
|
707
|
-
// getGlobalStore.getState().getInitialOptions(key) || {};
|
|
708
|
-
// getGlobalStore.getState().setInitialStateOptions(key, {
|
|
709
|
-
// ...currentOptions,
|
|
710
|
-
// syncSchema: schemas[key],
|
|
711
|
-
// notificationChannels: syncSchema.notifications,
|
|
712
|
-
// });
|
|
713
|
-
// }
|
|
714
|
-
|
|
715
|
-
return createCogsState(initialState);
|
|
719
|
+
// The cast is fine because the exported function signature is what matters.
|
|
720
|
+
return createCogsState(initialState) as any;
|
|
716
721
|
}
|
|
717
722
|
|
|
718
723
|
const {
|