cogsbox-state 0.5.3
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/README.md +386 -0
- package/dist/CogsState.d.ts +279 -0
- package/dist/CogsState.jsx +608 -0
- package/dist/CogsState.jsx.map +1 -0
- package/dist/CogsStateClient.d.ts +12 -0
- package/dist/CogsStateClient.jsx +11 -0
- package/dist/CogsStateClient.jsx.map +1 -0
- package/dist/TRPCValidationLink.d.ts +4 -0
- package/dist/_virtual/_commonjsHelpers.js +7 -0
- package/dist/_virtual/_commonjsHelpers.js.map +1 -0
- package/dist/_virtual/index.js +5 -0
- package/dist/_virtual/index.js.map +1 -0
- package/dist/_virtual/jsx-runtime.jsx +5 -0
- package/dist/_virtual/jsx-runtime.jsx.map +1 -0
- package/dist/_virtual/react-jsx-runtime.jsx +5 -0
- package/dist/_virtual/react-jsx-runtime.jsx.map +1 -0
- package/dist/_virtual/react-jsx-runtime.production.jsx +5 -0
- package/dist/_virtual/react-jsx-runtime.production.jsx.map +1 -0
- package/dist/_virtual/react.js +5 -0
- package/dist/_virtual/react.js.map +1 -0
- package/dist/_virtual/react.production.js +5 -0
- package/dist/_virtual/react.production.js.map +1 -0
- package/dist/examples/index.d.ts +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/node_modules/react/cjs/react-jsx-runtime.jsx +605 -0
- package/dist/node_modules/react/cjs/react-jsx-runtime.jsx.map +1 -0
- package/dist/node_modules/react/cjs/react-jsx-runtime.production.jsx +29 -0
- package/dist/node_modules/react/cjs/react-jsx-runtime.production.jsx.map +1 -0
- package/dist/node_modules/react/cjs/react.js +1274 -0
- package/dist/node_modules/react/cjs/react.js.map +1 -0
- package/dist/node_modules/react/cjs/react.production.js +209 -0
- package/dist/node_modules/react/cjs/react.production.js.map +1 -0
- package/dist/node_modules/react/index.js +12 -0
- package/dist/node_modules/react/index.js.map +1 -0
- package/dist/node_modules/react/jsx-runtime.jsx +9 -0
- package/dist/node_modules/react/jsx-runtime.jsx.map +1 -0
- package/dist/node_modules/uuid/dist/esm-browser/native.js +7 -0
- package/dist/node_modules/uuid/dist/esm-browser/native.js.map +1 -0
- package/dist/node_modules/uuid/dist/esm-browser/rng.js +11 -0
- package/dist/node_modules/uuid/dist/esm-browser/rng.js.map +1 -0
- package/dist/node_modules/uuid/dist/esm-browser/stringify.js +10 -0
- package/dist/node_modules/uuid/dist/esm-browser/stringify.js.map +1 -0
- package/dist/node_modules/uuid/dist/esm-browser/v4.js +14 -0
- package/dist/node_modules/uuid/dist/esm-browser/v4.js.map +1 -0
- package/dist/node_modules/zustand/esm/react.js +20 -0
- package/dist/node_modules/zustand/esm/react.js.map +1 -0
- package/dist/node_modules/zustand/esm/vanilla.js +15 -0
- package/dist/node_modules/zustand/esm/vanilla.js.map +1 -0
- package/dist/store.d.ts +103 -0
- package/dist/store.js +225 -0
- package/dist/store.js.map +1 -0
- package/dist/updaterFunctions.d.ts +29 -0
- package/dist/updaterFunctions.jsx +177 -0
- package/dist/updaterFunctions.jsx.map +1 -0
- package/dist/utility.d.ts +19 -0
- package/dist/utility.js +189 -0
- package/dist/utility.js.map +1 -0
- package/package.json +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
# Cobgsbox State
|
|
2
|
+
|
|
3
|
+
> ⚠️ **Warning**: This package is currently a work in progress and not ready for production use. The API is unstable and subject to breaking changes. Please do not use in production environments.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Cobgsbox State is a state management library that provides a fluent interface for managing complex state in React applications.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install cogsbox-state
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- 🎯 **Simplified Deep Updates**
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
// Instead of setState(prev => ({ ...prev, deep: { ...prev.deep, value: newValue }}))
|
|
21
|
+
updater.deep.value.update(newValue);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
- 🔄 **Fluent Array Operations**
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
// Find, filter, and update in one chain
|
|
28
|
+
updater.items.findWith("id", "123").status.update("complete");
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
- 📝 **Smart Form Handling**
|
|
32
|
+
|
|
33
|
+
- Automatic debouncing
|
|
34
|
+
- Validation integration
|
|
35
|
+
- Form state synchronization
|
|
36
|
+
|
|
37
|
+
- 🔍 **Powerful Array Methods**
|
|
38
|
+
|
|
39
|
+
- Filter with state access
|
|
40
|
+
- Flatten nested arrays
|
|
41
|
+
- Unique insertions
|
|
42
|
+
- Map with updaters
|
|
43
|
+
|
|
44
|
+
- 🔄 **State Sync Features**
|
|
45
|
+
|
|
46
|
+
- Server synchronization
|
|
47
|
+
- Local storage persistence
|
|
48
|
+
- Optimistic updates
|
|
49
|
+
|
|
50
|
+
- 🛠 **Developer Experience**
|
|
51
|
+
|
|
52
|
+
- Full TypeScript support
|
|
53
|
+
- Path autocompletion
|
|
54
|
+
- Runtime type checking
|
|
55
|
+
|
|
56
|
+
- ⚡ **Performance**
|
|
57
|
+
- Granular updates
|
|
58
|
+
- Automatic optimization
|
|
59
|
+
- Path-based subscriptions
|
|
60
|
+
|
|
61
|
+
## Initialization
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
// Define your state shape
|
|
65
|
+
interface Product {
|
|
66
|
+
id: string;
|
|
67
|
+
name: string;
|
|
68
|
+
price: number;
|
|
69
|
+
categories: string[];
|
|
70
|
+
variants: {
|
|
71
|
+
id: string;
|
|
72
|
+
color: string;
|
|
73
|
+
size: string;
|
|
74
|
+
stock: number;
|
|
75
|
+
}[];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface CartItem {
|
|
79
|
+
productId: string;
|
|
80
|
+
variantId: string;
|
|
81
|
+
quantity: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const InitialState = {
|
|
85
|
+
catalog: {
|
|
86
|
+
products: [] as Product[],
|
|
87
|
+
categories: [] as string[],
|
|
88
|
+
filters: {
|
|
89
|
+
priceRange: { min: 0, max: 100 },
|
|
90
|
+
selectedCategories: [] as string[],
|
|
91
|
+
search: "",
|
|
92
|
+
},
|
|
93
|
+
sortBy: "price_asc" as "price_asc" | "price_desc" | "name",
|
|
94
|
+
},
|
|
95
|
+
cart: {
|
|
96
|
+
items: [] as CartItem[],
|
|
97
|
+
couponCode: "",
|
|
98
|
+
shipping: {
|
|
99
|
+
address: "",
|
|
100
|
+
method: "standard" as "standard" | "express",
|
|
101
|
+
cost: 0,
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
ui: {
|
|
105
|
+
sidebarOpen: false,
|
|
106
|
+
activeProductId: null as string | null,
|
|
107
|
+
notifications: [] as {
|
|
108
|
+
id: string;
|
|
109
|
+
message: string;
|
|
110
|
+
type: "success" | "error";
|
|
111
|
+
}[],
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// Create state hook
|
|
116
|
+
export const { useCogsState } = createCogsState(InitialState);
|
|
117
|
+
|
|
118
|
+
// Use in component
|
|
119
|
+
const [state, updater] = useCogsState("catalog");
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Updater API
|
|
123
|
+
|
|
124
|
+
### Basic Updates
|
|
125
|
+
|
|
126
|
+
#### `.update()`
|
|
127
|
+
|
|
128
|
+
Updates state value directly.
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
// Direct update
|
|
132
|
+
updater.catalog.filters.search.update("blue shoes");
|
|
133
|
+
|
|
134
|
+
// Functional update
|
|
135
|
+
updater.cart.items[0].quantity.update((prev) => prev + 1);
|
|
136
|
+
|
|
137
|
+
// Deep update
|
|
138
|
+
updater.catalog.products
|
|
139
|
+
.findWith("id", "123")
|
|
140
|
+
.variants[0].stock.update((prev) => prev - 1);
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
#### `.get()`
|
|
144
|
+
|
|
145
|
+
Gets current state value.
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
const searchTerm = updater.catalog.filters.search.get();
|
|
149
|
+
const cartTotal = updater.cart.items
|
|
150
|
+
.get()
|
|
151
|
+
.reduce((sum, item) => sum + item.quantity, 0);
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Array Operations
|
|
155
|
+
|
|
156
|
+
#### `.insert()`
|
|
157
|
+
|
|
158
|
+
Inserts an item into an array.
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
// Add product
|
|
162
|
+
updater.catalog.products.insert({
|
|
163
|
+
id: "123",
|
|
164
|
+
name: "Running Shoes",
|
|
165
|
+
price: 99.99,
|
|
166
|
+
categories: ["shoes", "sports"],
|
|
167
|
+
variants: [],
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Add to cart
|
|
171
|
+
updater.cart.items.insert((prev) => ({
|
|
172
|
+
productId: "123",
|
|
173
|
+
variantId: "v1",
|
|
174
|
+
quantity: 1,
|
|
175
|
+
}));
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
#### `.uniqueInsert()`
|
|
179
|
+
|
|
180
|
+
Inserts only if item doesn't exist (checks deep equality).
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
// Add category if not exists
|
|
184
|
+
updater.catalog.categories.uniqueInsert("sports");
|
|
185
|
+
|
|
186
|
+
// Add notification with unique ID
|
|
187
|
+
updater.ui.notifications.uniqueInsert(
|
|
188
|
+
{
|
|
189
|
+
id: generateId(),
|
|
190
|
+
message: "Item added to cart",
|
|
191
|
+
type: "success",
|
|
192
|
+
},
|
|
193
|
+
["id"],
|
|
194
|
+
);
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
#### `.cut()`
|
|
198
|
+
|
|
199
|
+
Removes an item from an array.
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
// Remove from cart
|
|
203
|
+
updater.cart.items.cut(index);
|
|
204
|
+
|
|
205
|
+
// Remove notification
|
|
206
|
+
updater.ui.notifications.findWith("id", "notif-123").cut();
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
#### `.findWith()`
|
|
210
|
+
|
|
211
|
+
Finds an item in array by property comparison.
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
// Find and update product
|
|
215
|
+
updater.catalog.products.findWith("id", 123).price.update(99.99);
|
|
216
|
+
|
|
217
|
+
// Find and update cart item
|
|
218
|
+
updater.cart.items
|
|
219
|
+
.findWith("productId", 123)
|
|
220
|
+
.quantity.update((prev) => prev + 1);
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
#### `.index()`
|
|
224
|
+
|
|
225
|
+
Gets item at specific index with updater methods.
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
// Update first cart item
|
|
229
|
+
updater.cart.items.index(0).quantity.update(2);
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
#### `.stateEach()`
|
|
233
|
+
|
|
234
|
+
Maps over array with access to item updater.
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
// Apply discount to all products
|
|
238
|
+
updater.catalog.products.stateEach((product, productUpdater) => {
|
|
239
|
+
productUpdater.price.update((price) => price * 0.9);
|
|
240
|
+
});
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
#### `.stateFilter()`
|
|
244
|
+
|
|
245
|
+
Creates filtered view of array with updater methods.
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
// Get low stock variants
|
|
249
|
+
const lowStock = updater.catalog.products.stateFilter((product) =>
|
|
250
|
+
product.variants.some((v) => v.stock < 5),
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
// Update prices for filtered products
|
|
254
|
+
lowStock.stateEach((product, productUpdater) => {
|
|
255
|
+
productUpdater.price.update((price) => price * 0.8);
|
|
256
|
+
});
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
#### `.stateFlattenOn()`
|
|
260
|
+
|
|
261
|
+
Flattens nested arrays by property.
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
// Get all variants across products
|
|
265
|
+
const allVariants = updater.catalog.products.stateFlattenOn("variants");
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Form Integration
|
|
269
|
+
|
|
270
|
+
#### `.formElement()`
|
|
271
|
+
|
|
272
|
+
Creates form control with validation.
|
|
273
|
+
|
|
274
|
+
```typescript
|
|
275
|
+
updater.cart.shipping.address.formElement("shipping", ({ inputProps }) => (
|
|
276
|
+
<input {...inputProps} placeholder="Shipping address" />
|
|
277
|
+
), {
|
|
278
|
+
validation: {
|
|
279
|
+
message: "Address is required",
|
|
280
|
+
onChange: { clear: ["shipping.address"] }
|
|
281
|
+
},
|
|
282
|
+
debounceTime: 300
|
|
283
|
+
})
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### Menu & Selection API
|
|
287
|
+
|
|
288
|
+
#### `.setSelected()`
|
|
289
|
+
|
|
290
|
+
Marks an item as selected in a list.
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
// Select product
|
|
294
|
+
updater.catalog.products[0].setSelected(true);
|
|
295
|
+
|
|
296
|
+
// Select category
|
|
297
|
+
updater.catalog.categories.findWith("name", "sports").setSelected(true);
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
#### `.getSelected()`
|
|
301
|
+
|
|
302
|
+
Gets currently selected item from a list.
|
|
303
|
+
|
|
304
|
+
```typescript
|
|
305
|
+
const selectedProduct = updater.catalog.products.getSelected();
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
[Rest of documentation remains the same...]
|
|
309
|
+
|
|
310
|
+
## Examples
|
|
311
|
+
|
|
312
|
+
### Product Catalog Management
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
function ProductList() {
|
|
316
|
+
const [state, updater] = useCogsState("catalog", {
|
|
317
|
+
serverSync: {
|
|
318
|
+
syncKey: "products",
|
|
319
|
+
syncFunction: async ({ state }) => {
|
|
320
|
+
await api.updateProducts(state.products)
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
const filteredProducts = updater.products
|
|
326
|
+
.stateFilter(product =>
|
|
327
|
+
product.price >= state.filters.priceRange.min &&
|
|
328
|
+
product.price <= state.filters.priceRange.max &&
|
|
329
|
+
(state.filters.selectedCategories.length === 0 ||
|
|
330
|
+
product.categories.some(c => state.filters.selectedCategories.includes(c)))
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
return (
|
|
334
|
+
<div>
|
|
335
|
+
{filteredProducts.stateEach((product, productUpdater) => (
|
|
336
|
+
<ProductCard
|
|
337
|
+
key={product.id}
|
|
338
|
+
product={product}
|
|
339
|
+
onUpdateStock={(variantId, newStock) =>
|
|
340
|
+
productUpdater
|
|
341
|
+
.variants
|
|
342
|
+
.findWith("id", variantId)
|
|
343
|
+
.stock
|
|
344
|
+
.update(newStock)
|
|
345
|
+
}
|
|
346
|
+
/>
|
|
347
|
+
))}
|
|
348
|
+
</div>
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### Shopping Cart Management
|
|
354
|
+
|
|
355
|
+
```typescript
|
|
356
|
+
function CartManager() {
|
|
357
|
+
const [state, updater] = useCogsState("cart", {
|
|
358
|
+
localStorage: {
|
|
359
|
+
key: "shopping-cart"
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
const addToCart = (product: Product, variantId: string) => {
|
|
364
|
+
updater.items.uniqueInsert({
|
|
365
|
+
productId: product.id,
|
|
366
|
+
variantId,
|
|
367
|
+
quantity: 1
|
|
368
|
+
}, ['productId', 'variantId']);
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
return (
|
|
372
|
+
<div>
|
|
373
|
+
{updater.items.stateEach((item, itemUpdater) => (
|
|
374
|
+
<CartItem
|
|
375
|
+
key={item.productId}
|
|
376
|
+
item={item}
|
|
377
|
+
onUpdateQuantity={qty => itemUpdater.quantity.update(qty)}
|
|
378
|
+
onRemove={() => itemUpdater.cut()}
|
|
379
|
+
/>
|
|
380
|
+
))}
|
|
381
|
+
</div>
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
[Rest of TypeScript section remains the same...]
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { GenericObject } from './utility.js';
|
|
2
|
+
import { UseMutationResult } from '@tanstack/react-query';
|
|
3
|
+
import { ZodObject, ZodRawShape } from 'zod';
|
|
4
|
+
|
|
5
|
+
type Prettify<T> = {
|
|
6
|
+
[K in keyof T]: T[K];
|
|
7
|
+
} & {};
|
|
8
|
+
export type ServerSyncStatus = {
|
|
9
|
+
isFresh: boolean;
|
|
10
|
+
isFreshTime: number;
|
|
11
|
+
isStale: boolean;
|
|
12
|
+
isStaleTime: number;
|
|
13
|
+
isSyncing: boolean;
|
|
14
|
+
isSyncingTime: number;
|
|
15
|
+
};
|
|
16
|
+
export type SyncInfo = {
|
|
17
|
+
timeStamp: number;
|
|
18
|
+
userId: number;
|
|
19
|
+
};
|
|
20
|
+
export type FormElementParmas<T> = {
|
|
21
|
+
get: () => T;
|
|
22
|
+
set: UpdateType<T>;
|
|
23
|
+
syncStatus: (SyncInfo & {
|
|
24
|
+
date: Date;
|
|
25
|
+
}) | null;
|
|
26
|
+
path: string[];
|
|
27
|
+
validationErrors: () => string[];
|
|
28
|
+
inputProps: {
|
|
29
|
+
value?: T;
|
|
30
|
+
onChange?: (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
export type StateKeys = string;
|
|
34
|
+
type findWithFuncType<U> = (thisKey: keyof U, thisValue: U[keyof U]) => EndType<U> & {
|
|
35
|
+
upsert: UpdateType<U>;
|
|
36
|
+
} & StateObject<U>;
|
|
37
|
+
export type PushArgs<U> = (update: Prettify<U> | ((prevState: NonNullable<Prettify<U>>[]) => NonNullable<Prettify<U>>), opts?: UpdateOpts) => void;
|
|
38
|
+
export interface GlobalState<T, K extends keyof T = keyof T> {
|
|
39
|
+
getKeyState: () => T;
|
|
40
|
+
setState: (newState: T | ((previousState: T) => T)) => void;
|
|
41
|
+
key: K;
|
|
42
|
+
}
|
|
43
|
+
type CutFunctionType = (index?: number, options?: {
|
|
44
|
+
waitForSync?: boolean;
|
|
45
|
+
}) => void;
|
|
46
|
+
export type InferArrayElement<T> = T extends (infer U)[] ? U : never;
|
|
47
|
+
export type ArrayEndType<TShape extends unknown> = {
|
|
48
|
+
findWith: findWithFuncType<InferArrayElement<TShape>>;
|
|
49
|
+
index: (index: number) => StateObject<InferArrayElement<TShape>> & {
|
|
50
|
+
insert: PushArgs<InferArrayElement<TShape>>;
|
|
51
|
+
cut: CutFunctionType;
|
|
52
|
+
_index: number;
|
|
53
|
+
} & EndType<InferArrayElement<TShape>>;
|
|
54
|
+
insert: PushArgs<InferArrayElement<TShape>>;
|
|
55
|
+
cut: CutFunctionType;
|
|
56
|
+
stateEach: (callbackfn: (value: InferArrayElement<TShape>, setter: StateObject<InferArrayElement<TShape>>, index: number, array: TShape, arraySetter: StateObject<TShape>) => void) => any;
|
|
57
|
+
stateFlattenOn: <K extends keyof InferArrayElement<TShape>>(field: K) => StateObject<InferArrayElement<InferArrayElement<TShape>[K]>[]>;
|
|
58
|
+
uniqueInsert: (payload: UpdateArg<InferArrayElement<TShape>>, fields?: (keyof InferArrayElement<TShape>)[]) => void;
|
|
59
|
+
stateFilter: (callbackfn: (value: InferArrayElement<TShape>, index: number) => void) => ArrayEndType<TShape>;
|
|
60
|
+
getSelected: () => StateObject<InferArrayElement<TShape>> | undefined;
|
|
61
|
+
} & EndType<TShape>;
|
|
62
|
+
export type UpdateType<T> = (payload: UpdateArg<Prettify<T>>, opts?: UpdateOpts) => void;
|
|
63
|
+
export type FormOptsType = {
|
|
64
|
+
key?: string;
|
|
65
|
+
validation?: {
|
|
66
|
+
message?: string;
|
|
67
|
+
stretch?: boolean;
|
|
68
|
+
props?: GenericObject;
|
|
69
|
+
disable?: boolean;
|
|
70
|
+
};
|
|
71
|
+
formElements?: boolean;
|
|
72
|
+
debounceTime?: number;
|
|
73
|
+
stateServerDifferences?: string[][];
|
|
74
|
+
};
|
|
75
|
+
export type FormControl<T> = (obj: FormElementParmas<T>) => JSX.Element;
|
|
76
|
+
export type UpdateArg<S> = S | ((prevState: S) => S);
|
|
77
|
+
export type UpdateOpts = {
|
|
78
|
+
timelineLabel?: string;
|
|
79
|
+
timeLineMessage?: string;
|
|
80
|
+
validate?: boolean;
|
|
81
|
+
};
|
|
82
|
+
export type ObjectEndType<T> = EndType<T> & {
|
|
83
|
+
[K in keyof T]-?: ObjectEndType<T[K]>;
|
|
84
|
+
} & {
|
|
85
|
+
stateObject: (callbackfn: (value: T, setter: StateObject<T>) => void) => any;
|
|
86
|
+
delete: () => void;
|
|
87
|
+
};
|
|
88
|
+
export type EndType<T> = {
|
|
89
|
+
update: UpdateType<T>;
|
|
90
|
+
_path: string[];
|
|
91
|
+
_stateKey: string;
|
|
92
|
+
formElement: (validationKey: string, control: FormControl<T>, opts?: FormOptsType) => JSX.Element;
|
|
93
|
+
get: () => T;
|
|
94
|
+
$get: () => T;
|
|
95
|
+
_status: "fresh" | "stale" | "synced";
|
|
96
|
+
showValidationErrors: (ctx: string) => string[];
|
|
97
|
+
setValidation: (ctx: string) => void;
|
|
98
|
+
removeValidation: (ctx: string) => void;
|
|
99
|
+
ignoreFields: (fields: string[]) => StateObject<T>;
|
|
100
|
+
_selected: boolean;
|
|
101
|
+
setSelected: (value: boolean) => void;
|
|
102
|
+
validationWrapper: ({ children, hideMessage, }: {
|
|
103
|
+
children: React.ReactNode;
|
|
104
|
+
hideMessage?: boolean;
|
|
105
|
+
}) => JSX.Element;
|
|
106
|
+
lastSynced?: SyncInfo;
|
|
107
|
+
};
|
|
108
|
+
export type StateObject<T> = (T extends any[] ? ArrayEndType<T> : T extends Record<string, unknown> | object ? {
|
|
109
|
+
[K in keyof T]-?: StateObject<T[K]>;
|
|
110
|
+
} & ObjectEndType<T> : T extends string | number | boolean | null ? T : never) & EndType<T> & {
|
|
111
|
+
_componentId: string | null;
|
|
112
|
+
_initialState: T;
|
|
113
|
+
updateInitialState: (newState: T | null) => {
|
|
114
|
+
fetchId: (field: keyof T) => string | number;
|
|
115
|
+
};
|
|
116
|
+
_isLoading: boolean;
|
|
117
|
+
_serverState: T;
|
|
118
|
+
revertToInitialState: (obj?: {
|
|
119
|
+
validationKey?: string;
|
|
120
|
+
}) => void;
|
|
121
|
+
middleware: (middles: ({ updateLog, update, }: {
|
|
122
|
+
updateLog: UpdateTypeDetail[] | undefined;
|
|
123
|
+
update: UpdateTypeDetail;
|
|
124
|
+
}) => void) => void;
|
|
125
|
+
_isServerSynced: () => boolean;
|
|
126
|
+
getLocalStorage: (key: string) => LocalStorageData<T> | null;
|
|
127
|
+
};
|
|
128
|
+
export type CogsUpdate<T extends unknown> = UpdateType<T>;
|
|
129
|
+
export type EffectiveSetState<TStateObject> = (newStateOrFunction: TStateObject | ((prevState: TStateObject) => TStateObject), path: string[], updateObj: {
|
|
130
|
+
updateType: "update" | "insert" | "cut";
|
|
131
|
+
}, validationKey?: string, opts?: UpdateOpts) => void;
|
|
132
|
+
export type UpdateTypeDetail = {
|
|
133
|
+
timeStamp: number;
|
|
134
|
+
stateKey: string;
|
|
135
|
+
updateType: "update" | "insert" | "cut";
|
|
136
|
+
path: string[];
|
|
137
|
+
status: "new" | "sent" | "synced";
|
|
138
|
+
oldValue: any;
|
|
139
|
+
newValue: any;
|
|
140
|
+
userId?: number;
|
|
141
|
+
};
|
|
142
|
+
export type ActionsType<T> = {
|
|
143
|
+
type: "onChange";
|
|
144
|
+
action: ({ state, actionType }: {
|
|
145
|
+
state: T;
|
|
146
|
+
actionType: string;
|
|
147
|
+
}) => void;
|
|
148
|
+
debounce?: number;
|
|
149
|
+
}[];
|
|
150
|
+
type ArrayToObject<T extends string[]> = Record<T[number], string>;
|
|
151
|
+
type CookieType<T> = {
|
|
152
|
+
timeStamp: number;
|
|
153
|
+
value: T;
|
|
154
|
+
cookieName: string;
|
|
155
|
+
OnUnMountCookie?: Boolean;
|
|
156
|
+
};
|
|
157
|
+
export type CogsCookiesType<T extends string[] = string[]> = CookieType<ArrayToObject<T>>;
|
|
158
|
+
export type OptionsType<T extends unknown = unknown> = {
|
|
159
|
+
serverSync?: ServerSyncType<T>;
|
|
160
|
+
validationKey?: string;
|
|
161
|
+
enableServerState?: boolean;
|
|
162
|
+
middleware?: ({ updateLog, update, }: {
|
|
163
|
+
updateLog: UpdateTypeDetail[] | undefined;
|
|
164
|
+
update: UpdateTypeDetail;
|
|
165
|
+
}) => void;
|
|
166
|
+
zodSchema?: ZodObject<ZodRawShape>;
|
|
167
|
+
modifyState?: (state: T) => T;
|
|
168
|
+
localStorage?: {
|
|
169
|
+
key: string | ((state: T) => string);
|
|
170
|
+
};
|
|
171
|
+
formElements?: FormsElementsType;
|
|
172
|
+
enabledSync?: (state: T) => boolean;
|
|
173
|
+
reactiveDeps?: (state: T) => any[] | true;
|
|
174
|
+
syncUpdate?: Partial<UpdateTypeDetail>;
|
|
175
|
+
initState?: {
|
|
176
|
+
localStorageKey?: string;
|
|
177
|
+
ctx?: Record<string, any>;
|
|
178
|
+
initialState: T;
|
|
179
|
+
dependencies?: any[];
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
export type ServerSyncType<T> = {
|
|
183
|
+
testKey?: string;
|
|
184
|
+
syncKey: (({ state }: {
|
|
185
|
+
state: T;
|
|
186
|
+
}) => string) | string;
|
|
187
|
+
syncFunction: ({ state }: {
|
|
188
|
+
state: T;
|
|
189
|
+
}) => void;
|
|
190
|
+
debounce?: number;
|
|
191
|
+
mutation: UseMutationResult<any, unknown, any, unknown>;
|
|
192
|
+
snapshot?: {
|
|
193
|
+
name: (({ state }: {
|
|
194
|
+
state: T;
|
|
195
|
+
}) => string) | string;
|
|
196
|
+
stateKeys: StateKeys[];
|
|
197
|
+
currentUrl: string;
|
|
198
|
+
currentParams?: URLSearchParams;
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
export type SyncActionsType<T> = {
|
|
202
|
+
syncKey: string;
|
|
203
|
+
rollBackState?: T;
|
|
204
|
+
actionTimeStamp: number;
|
|
205
|
+
retryCount?: number;
|
|
206
|
+
status: "success" | "waiting" | "rolledBack" | "error" | "cancelled" | "failed";
|
|
207
|
+
snapshot?: {
|
|
208
|
+
name: string;
|
|
209
|
+
stateKeys: StateKeys[];
|
|
210
|
+
currentUrl: string;
|
|
211
|
+
currentParams?: URLSearchParams;
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
export type ValidationWrapperOptions<T extends unknown = unknown> = {
|
|
215
|
+
children: React.ReactNode;
|
|
216
|
+
active: boolean;
|
|
217
|
+
stretch?: boolean;
|
|
218
|
+
path: string[];
|
|
219
|
+
message?: string;
|
|
220
|
+
data?: T;
|
|
221
|
+
key?: string;
|
|
222
|
+
};
|
|
223
|
+
export type SyncRenderOptions<T extends unknown = unknown> = {
|
|
224
|
+
children: React.ReactNode;
|
|
225
|
+
time: number;
|
|
226
|
+
data?: T;
|
|
227
|
+
key?: string;
|
|
228
|
+
};
|
|
229
|
+
type FormsElementsType<T extends unknown = unknown> = {
|
|
230
|
+
validation?: (options: ValidationWrapperOptions<T>) => React.ReactNode;
|
|
231
|
+
syncRender?: (options: SyncRenderOptions<T>) => React.ReactNode;
|
|
232
|
+
};
|
|
233
|
+
export type InitialStateInnerType<T extends unknown = unknown> = {
|
|
234
|
+
initialState: T;
|
|
235
|
+
} & OptionsType<T>;
|
|
236
|
+
export type InitialStateType<T> = {
|
|
237
|
+
[key: string]: InitialStateInnerType<T>;
|
|
238
|
+
};
|
|
239
|
+
export type FunctionsToPassDownType = {
|
|
240
|
+
getValidationErrors: (pathArray: string) => string[];
|
|
241
|
+
removeValidationError: (path: string) => void;
|
|
242
|
+
};
|
|
243
|
+
export type AllStateTypes<T extends unknown> = Record<string, T>;
|
|
244
|
+
export type CogsInitialState<T> = {
|
|
245
|
+
initialState: T;
|
|
246
|
+
formElements?: FormsElementsType<T>;
|
|
247
|
+
};
|
|
248
|
+
export type TransformedStateType<T> = {
|
|
249
|
+
[P in keyof T]: T[P] extends CogsInitialState<infer U> ? U : T[P];
|
|
250
|
+
};
|
|
251
|
+
export declare function addStateOptions<T extends unknown>(initialState: T, { formElements, zodSchema }: OptionsType<T>): T;
|
|
252
|
+
export declare const createCogsState: <State extends Record<string, unknown>>(initialState: State) => {
|
|
253
|
+
useCogsState: <StateKey extends keyof State>(stateKey: StateKey, options?: OptionsType<TransformedStateType<State>[StateKey]>) => readonly [TransformedStateType<State>[StateKey], StateObject<TransformedStateType<State>[StateKey]>];
|
|
254
|
+
setCogsOptions: <StateKey extends keyof State>(stateKey: StateKey, options: OptionsType<TransformedStateType<State>[StateKey]>) => void;
|
|
255
|
+
};
|
|
256
|
+
type LocalStorageData<T> = {
|
|
257
|
+
state: T;
|
|
258
|
+
lastUpdated: number;
|
|
259
|
+
lastSyncedWithServer?: number;
|
|
260
|
+
baseServerState?: T;
|
|
261
|
+
};
|
|
262
|
+
export declare function useCogsStateFn<TStateObject extends unknown>(stateObject: TStateObject, { stateKey, serverSync, zodSchema, localStorage, formElements, middleware, reactiveDeps, componentId, initState, syncUpdate, }?: {
|
|
263
|
+
stateKey?: string;
|
|
264
|
+
componentId?: string;
|
|
265
|
+
} & OptionsType<TStateObject>): [TStateObject, StateObject<TStateObject>];
|
|
266
|
+
export declare function $cogsSignal(proxy: {
|
|
267
|
+
_path: string[];
|
|
268
|
+
_stateKey: string;
|
|
269
|
+
}): import('react').FunctionComponentElement<{
|
|
270
|
+
proxy: {
|
|
271
|
+
_path: string[];
|
|
272
|
+
_stateKey: string;
|
|
273
|
+
};
|
|
274
|
+
}>;
|
|
275
|
+
export declare function $cogsSignalStore(proxy: {
|
|
276
|
+
_path: string[];
|
|
277
|
+
_stateKey: string;
|
|
278
|
+
}): import('react').ReactSVGElement;
|
|
279
|
+
export {};
|