@trackunit/react-components 1.18.15 → 1.18.16
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/index.cjs.js +568 -108
- package/index.esm.js +567 -109
- package/package.json +6 -5
- package/src/hooks/localStorage/readFromStorage.d.ts +39 -0
- package/src/hooks/localStorage/runMigrations.d.ts +13 -0
- package/src/hooks/localStorage/salvageState.d.ts +25 -0
- package/src/hooks/localStorage/storageSerializer.d.ts +4 -0
- package/src/hooks/localStorage/storageVersionEnvelope.d.ts +30 -0
- package/src/hooks/localStorage/types.d.ts +28 -24
- package/src/hooks/localStorage/useLocalStorage.d.ts +13 -7
- package/src/hooks/localStorage/useLocalStorageReducer.d.ts +17 -10
- package/src/hooks/localStorage/useSessionStorage.d.ts +16 -0
- package/src/hooks/localStorage/useSessionStorageReducer.d.ts +20 -0
- package/src/hooks/localStorage/useStorageSyncEffect.d.ts +35 -0
- package/src/hooks/localStorage/useWebStorage.d.ts +18 -0
- package/src/hooks/localStorage/useWebStorageReducer.d.ts +22 -0
- package/src/hooks/localStorage/validateState.d.ts +17 -8
- package/src/hooks/localStorage/writeToStorage.d.ts +11 -0
- package/src/index.d.ts +3 -0
- package/src/hooks/localStorage/initLocalStorageState.d.ts +0 -9
- package/src/hooks/localStorage/setLocalStorage.d.ts +0 -11
- package/src/hooks/localStorage/useLocalStorageEffect.d.ts +0 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-components",
|
|
3
|
-
"version": "1.18.
|
|
3
|
+
"version": "1.18.16",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
@@ -14,14 +14,15 @@
|
|
|
14
14
|
"@floating-ui/react": "^0.26.25",
|
|
15
15
|
"string-ts": "^2.0.0",
|
|
16
16
|
"tailwind-merge": "^2.0.0",
|
|
17
|
-
"@trackunit/ui-design-tokens": "1.11.
|
|
18
|
-
"@trackunit/css-class-variance-utilities": "1.11.
|
|
19
|
-
"@trackunit/shared-utils": "1.13.
|
|
20
|
-
"@trackunit/ui-icons": "1.11.
|
|
17
|
+
"@trackunit/ui-design-tokens": "1.11.70",
|
|
18
|
+
"@trackunit/css-class-variance-utilities": "1.11.72",
|
|
19
|
+
"@trackunit/shared-utils": "1.13.72",
|
|
20
|
+
"@trackunit/ui-icons": "1.11.69",
|
|
21
21
|
"@tanstack/react-router": "1.114.29",
|
|
22
22
|
"es-toolkit": "^1.39.10",
|
|
23
23
|
"@tanstack/react-virtual": "3.13.12",
|
|
24
24
|
"fflate": "^0.8.2",
|
|
25
|
+
"superjson": "^2.2.6",
|
|
25
26
|
"zod": "^3.23.8"
|
|
26
27
|
},
|
|
27
28
|
"module": "./index.esm.js",
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import type { MigrationConfig, WebStorageCallbacks } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Reads and deserializes a value from web storage, validating it against the provided schema.
|
|
5
|
+
* Falls back to defaultState if the key is missing or data is corrupt.
|
|
6
|
+
*
|
|
7
|
+
* On partial validation failure for ZodObject schemas, salvages valid fields rather than
|
|
8
|
+
* resetting the entire state to `defaultState`. See `salvageState` for salvage mechanics.
|
|
9
|
+
* If the salvaged result is deeply equal to `defaultState` (i.e. every field was invalid),
|
|
10
|
+
* the salvage is considered a total loss and `onValidationFailed` is called instead of
|
|
11
|
+
* `onValidationSalvaged`.
|
|
12
|
+
*
|
|
13
|
+
* Deserialization failures (unparseable/corrupt raw data) are routed through
|
|
14
|
+
* `onValidationFailed` so consumers are always notified when stored data is unusable.
|
|
15
|
+
*
|
|
16
|
+
* When `migration` is provided, detects versioned envelopes, runs the
|
|
17
|
+
* migration pipeline, and validates the migrated result. Non-versioned consumers are
|
|
18
|
+
* unaffected — the migration path is fully opt-in.
|
|
19
|
+
*
|
|
20
|
+
* @template TState - The type of the stored state.
|
|
21
|
+
* @param params - Storage, key, default state, Zod schema, optional migration config, and optional callbacks.
|
|
22
|
+
* @param params.storage - The web Storage instance.
|
|
23
|
+
* @param params.key - The storage key.
|
|
24
|
+
* @param params.defaultState - Fallback value when no stored data exists or data is corrupt.
|
|
25
|
+
* @param params.schema - Zod schema to validate the deserialized data.
|
|
26
|
+
* @param params.migration - Optional migration configuration (version, steps, fromKey).
|
|
27
|
+
* @param params.onValidationFailed - Called when validation fails and no salvage is possible, or when
|
|
28
|
+
* deserialization of the raw storage value fails entirely.
|
|
29
|
+
* @param params.onValidationSalvaged - Called when validation fails but at least one stored field
|
|
30
|
+
* was recovered (salvaged result differs from defaultState).
|
|
31
|
+
* @returns {TState} The validated or salvaged state, or defaultState.
|
|
32
|
+
*/
|
|
33
|
+
export declare const readFromStorage: <TState>({ storage, key, defaultState, schema, migration, onValidationFailed, onValidationSalvaged, }: {
|
|
34
|
+
readonly storage: Storage;
|
|
35
|
+
readonly key: string;
|
|
36
|
+
readonly defaultState: TState;
|
|
37
|
+
readonly schema: z.ZodType<TState>;
|
|
38
|
+
readonly migration?: MigrationConfig;
|
|
39
|
+
} & Pick<WebStorageCallbacks<TState>, "onValidationFailed" | "onValidationSalvaged">) => TState;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { StorageMigration } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Runs a sequential migration pipeline on the provided data, applying
|
|
4
|
+
* each migration whose version is in the range (fromVersion, toVersion].
|
|
5
|
+
*
|
|
6
|
+
* Throws if the filtered migrations have non-contiguous versions (gaps).
|
|
7
|
+
*/
|
|
8
|
+
export declare const runMigrations: ({ data, fromVersion, toVersion, migrations, }: {
|
|
9
|
+
readonly data: unknown;
|
|
10
|
+
readonly fromVersion: number;
|
|
11
|
+
readonly toVersion: number;
|
|
12
|
+
readonly migrations: ReadonlyArray<StorageMigration>;
|
|
13
|
+
}) => unknown;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* Attempts to salvage partial state from raw data when full schema validation has failed.
|
|
4
|
+
*
|
|
5
|
+
* For ZodObject schemas, rebuilds the schema with per-field `.catch(defaultState[key])` fallbacks
|
|
6
|
+
* and reparses. Valid field values from the stored data are kept; invalid fields fall back to their
|
|
7
|
+
* corresponding values in `defaultState`. Works recursively for nested ZodObject fields.
|
|
8
|
+
*
|
|
9
|
+
* Non-object schemas (discriminated unions, unions with `.refine()`, etc.) cannot be partially
|
|
10
|
+
* salvaged and return `null`.
|
|
11
|
+
*
|
|
12
|
+
* Consumer-defined `.catch()` wrappers take precedence over the injected salvage catches, since
|
|
13
|
+
* Zod processes catches inside-out.
|
|
14
|
+
*
|
|
15
|
+
* Note: when every field is invalid, the salvaged result will be deeply equal to `defaultState`.
|
|
16
|
+
* Callers should compare the result against `defaultState` to distinguish a genuine partial
|
|
17
|
+
* salvage from a total loss where no stored data survived.
|
|
18
|
+
*
|
|
19
|
+
* @template TState - The type of the stored state.
|
|
20
|
+
* @param schema - Zod schema for validation.
|
|
21
|
+
* @param rawData - The raw deserialized value that failed validation.
|
|
22
|
+
* @param defaultState - The authoritative fallback value; used as per-field catch defaults.
|
|
23
|
+
* @returns {TState | null} The salvaged state, or `null` if salvaging is not possible or also fails.
|
|
24
|
+
*/
|
|
25
|
+
export declare const salvageState: <TState>(schema: z.ZodType<TState>, rawData: unknown, defaultState: TState) => TState | null;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* Internal envelope that pairs stored data with a schema version number.
|
|
4
|
+
*
|
|
5
|
+
* `__version` and `__data` are **reserved internal keys** — consumer state
|
|
6
|
+
* objects must not include them as top-level keys. The double-underscore
|
|
7
|
+
* prefix is a deliberate signal that these are private implementation details.
|
|
8
|
+
*
|
|
9
|
+
* A runtime warning is emitted (via `writeToStorage`) if reserved keys are
|
|
10
|
+
* detected in the value being written.
|
|
11
|
+
*/
|
|
12
|
+
export declare const storageVersionEnvelopeSchema: z.ZodEffects<z.ZodObject<{
|
|
13
|
+
__version: z.ZodNumber;
|
|
14
|
+
__data: z.ZodUnknown;
|
|
15
|
+
}, "strip", z.ZodTypeAny, {
|
|
16
|
+
__version: number;
|
|
17
|
+
__data?: unknown;
|
|
18
|
+
}, {
|
|
19
|
+
__version: number;
|
|
20
|
+
__data?: unknown;
|
|
21
|
+
}>, {
|
|
22
|
+
__version: number;
|
|
23
|
+
__data: unknown;
|
|
24
|
+
}, {
|
|
25
|
+
__version: number;
|
|
26
|
+
__data?: unknown;
|
|
27
|
+
}>;
|
|
28
|
+
export type StorageVersionEnvelope = z.infer<typeof storageVersionEnvelopeSchema>;
|
|
29
|
+
/** Wraps data and version into a versioned storage envelope. */
|
|
30
|
+
export declare const createStorageVersionEnvelope: (data: unknown, version: number) => StorageVersionEnvelope;
|
|
@@ -1,27 +1,31 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
export
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
export type StorageMigration = {
|
|
3
|
+
readonly version: number;
|
|
4
|
+
readonly migrate: (data: unknown) => unknown;
|
|
5
|
+
};
|
|
6
|
+
export type MigrationConfig = {
|
|
7
|
+
readonly version: number;
|
|
8
|
+
readonly steps?: ReadonlyArray<StorageMigration>;
|
|
9
|
+
readonly fromKey?: string;
|
|
10
|
+
};
|
|
11
|
+
export type WebStorageOptions<TState> = {
|
|
12
|
+
/** Must be a stable, non-empty string. Passing an empty string throws during render. Changing from non-empty to empty across renders is unsupported and will throw. */
|
|
13
|
+
readonly key: string;
|
|
14
|
+
readonly defaultState: NoInfer<TState>;
|
|
15
|
+
readonly schema: z.ZodType<TState>;
|
|
16
|
+
readonly migration?: MigrationConfig;
|
|
17
|
+
};
|
|
18
|
+
export type WebStorageCallbacks<TState> = {
|
|
19
|
+
readonly onValidationFailed?: (error: unknown) => void;
|
|
20
|
+
readonly onValidationSuccessful?: (data: TState) => void;
|
|
3
21
|
/**
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* The default state value.
|
|
9
|
-
*/
|
|
10
|
-
defaultState: TState;
|
|
11
|
-
/**
|
|
12
|
-
* Optional schema for validating the state.
|
|
13
|
-
*/
|
|
14
|
-
schema?: z.Schema<TState>;
|
|
15
|
-
}
|
|
16
|
-
export interface LocalStorageCallbacks<TState> {
|
|
17
|
-
/**
|
|
18
|
-
* Optional callback function called when validation fails.
|
|
22
|
+
* Called when schema validation fails but partial state was recovered.
|
|
23
|
+
* Valid fields from the stored value are kept; invalid fields fall back to their
|
|
24
|
+
* corresponding values in `defaultState`. Only fires during reads (initialization
|
|
25
|
+
* and key changes) — never during the write-sync path.
|
|
19
26
|
*
|
|
20
|
-
* @param error - The
|
|
21
|
-
|
|
22
|
-
onValidationFailed?: (error: unknown) => void;
|
|
23
|
-
/**
|
|
24
|
-
* Optional callback function called when validation succeeds.
|
|
27
|
+
* @param error - The ZodError describing which fields failed validation.
|
|
28
|
+
* @param salvaged - The recovered state combining valid stored fields and defaultState fallbacks.
|
|
25
29
|
*/
|
|
26
|
-
|
|
27
|
-
}
|
|
30
|
+
readonly onValidationSalvaged?: (error: z.ZodError, salvaged: TState) => void;
|
|
31
|
+
};
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import { Dispatch, SetStateAction } from "react";
|
|
2
|
-
import {
|
|
1
|
+
import type { Dispatch, SetStateAction } from "react";
|
|
2
|
+
import type { WebStorageCallbacks, WebStorageOptions } from "./types";
|
|
3
3
|
/**
|
|
4
|
-
* Works like
|
|
4
|
+
* Works like useState, but persists to localStorage with Zod schema validation
|
|
5
|
+
* and superjson serialization (supports Date, Map, Set, BigInt, etc.).
|
|
5
6
|
*
|
|
6
|
-
* @template TState - The type of the
|
|
7
|
-
* @param
|
|
8
|
-
* @
|
|
7
|
+
* @template TState - The type of the stored state.
|
|
8
|
+
* @param options - Key, defaultState, schema, and optional callbacks.
|
|
9
|
+
* @param options.key - The storage key.
|
|
10
|
+
* @param options.defaultState - Fallback value when no stored data exists.
|
|
11
|
+
* @param options.schema - Zod schema for validation.
|
|
12
|
+
* @param options.onValidationFailed - Optional error callback.
|
|
13
|
+
* @param options.onValidationSuccessful - Optional success callback.
|
|
14
|
+
* @returns {Array} A tuple of [state, setState, reset].
|
|
9
15
|
*/
|
|
10
|
-
export declare const useLocalStorage: <TState>(
|
|
16
|
+
export declare const useLocalStorage: <TState>(options: WebStorageOptions<TState> & WebStorageCallbacks<TState>) => readonly [TState, Dispatch<SetStateAction<TState>>, () => void];
|
|
@@ -1,13 +1,20 @@
|
|
|
1
|
-
import { Dispatch } from "react";
|
|
2
|
-
import {
|
|
1
|
+
import type { Dispatch } from "react";
|
|
2
|
+
import type { WebStorageCallbacks, WebStorageOptions } from "./types";
|
|
3
3
|
/**
|
|
4
|
-
* Works like
|
|
4
|
+
* Works like useReducer, but persists to localStorage with Zod schema validation
|
|
5
|
+
* and superjson serialization (supports Date, Map, Set, BigInt, etc.).
|
|
5
6
|
*
|
|
6
|
-
* @template TState - The type of the state.
|
|
7
|
-
* @template TAction - The type of the action.
|
|
8
|
-
* @param
|
|
9
|
-
* @
|
|
7
|
+
* @template TState - The type of the stored state.
|
|
8
|
+
* @template TAction - The type of the reducer action.
|
|
9
|
+
* @param options - Key, defaultState, schema, reducer, and optional callbacks.
|
|
10
|
+
* @param options.key - The storage key.
|
|
11
|
+
* @param options.defaultState - Fallback value when no stored data exists.
|
|
12
|
+
* @param options.schema - Zod schema for validation.
|
|
13
|
+
* @param options.reducer - The reducer function.
|
|
14
|
+
* @param options.onValidationFailed - Optional error callback.
|
|
15
|
+
* @param options.onValidationSuccessful - Optional success callback.
|
|
16
|
+
* @returns {Array} A tuple of [state, dispatch].
|
|
10
17
|
*/
|
|
11
|
-
export declare const useLocalStorageReducer: <TState, TAction>(
|
|
12
|
-
reducer: (state: TState, action: TAction) => TState;
|
|
13
|
-
}
|
|
18
|
+
export declare const useLocalStorageReducer: <TState, TAction>(options: WebStorageOptions<TState> & WebStorageCallbacks<TState> & {
|
|
19
|
+
readonly reducer: (state: TState, action: TAction) => TState;
|
|
20
|
+
}) => readonly [TState, Dispatch<TAction>];
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Dispatch, SetStateAction } from "react";
|
|
2
|
+
import type { WebStorageCallbacks, WebStorageOptions } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Works like useState, but persists to sessionStorage with Zod schema validation
|
|
5
|
+
* and superjson serialization (supports Date, Map, Set, BigInt, etc.).
|
|
6
|
+
*
|
|
7
|
+
* @template TState - The type of the stored state.
|
|
8
|
+
* @param options - Key, defaultState, schema, and optional callbacks.
|
|
9
|
+
* @param options.key - The storage key.
|
|
10
|
+
* @param options.defaultState - Fallback value when no stored data exists.
|
|
11
|
+
* @param options.schema - Zod schema for validation.
|
|
12
|
+
* @param options.onValidationFailed - Optional error callback.
|
|
13
|
+
* @param options.onValidationSuccessful - Optional success callback.
|
|
14
|
+
* @returns {Array} A tuple of [state, setState, reset].
|
|
15
|
+
*/
|
|
16
|
+
export declare const useSessionStorage: <TState>(options: WebStorageOptions<TState> & WebStorageCallbacks<TState>) => readonly [TState, Dispatch<SetStateAction<TState>>, () => void];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Dispatch } from "react";
|
|
2
|
+
import type { WebStorageCallbacks, WebStorageOptions } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Works like useReducer, but persists to sessionStorage with Zod schema validation
|
|
5
|
+
* and superjson serialization (supports Date, Map, Set, BigInt, etc.).
|
|
6
|
+
*
|
|
7
|
+
* @template TState - The type of the stored state.
|
|
8
|
+
* @template TAction - The type of the reducer action.
|
|
9
|
+
* @param options - Key, defaultState, schema, reducer, and optional callbacks.
|
|
10
|
+
* @param options.key - The storage key.
|
|
11
|
+
* @param options.defaultState - Fallback value when no stored data exists.
|
|
12
|
+
* @param options.schema - Zod schema for validation.
|
|
13
|
+
* @param options.reducer - The reducer function.
|
|
14
|
+
* @param options.onValidationFailed - Optional error callback.
|
|
15
|
+
* @param options.onValidationSuccessful - Optional success callback.
|
|
16
|
+
* @returns {Array} A tuple of [state, dispatch].
|
|
17
|
+
*/
|
|
18
|
+
export declare const useSessionStorageReducer: <TState, TAction>(options: WebStorageOptions<TState> & WebStorageCallbacks<TState> & {
|
|
19
|
+
readonly reducer: (state: TState, action: TAction) => TState;
|
|
20
|
+
}) => readonly [TState, Dispatch<TAction>];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type MutableRefObject } from "react";
|
|
2
|
+
import type { z } from "zod";
|
|
3
|
+
import type { WebStorageCallbacks } from "./types";
|
|
4
|
+
type UseStorageSyncEffectOptions<TState> = {
|
|
5
|
+
readonly storage: Storage;
|
|
6
|
+
readonly key: string;
|
|
7
|
+
readonly state: TState;
|
|
8
|
+
readonly defaultState: TState;
|
|
9
|
+
readonly schema: z.ZodType<TState>;
|
|
10
|
+
readonly version?: number;
|
|
11
|
+
/**
|
|
12
|
+
* When provided, the effect skips the write if this ref is `true`
|
|
13
|
+
* (and resets it to `false` afterward). Used by key-change handlers
|
|
14
|
+
* to prevent writing stale state to a newly switched key.
|
|
15
|
+
*/
|
|
16
|
+
readonly skipWriteRef?: MutableRefObject<boolean>;
|
|
17
|
+
} & WebStorageCallbacks<TState>;
|
|
18
|
+
/**
|
|
19
|
+
* Effect that synchronizes React state to web storage on every state change,
|
|
20
|
+
* validating through the schema before writing.
|
|
21
|
+
*
|
|
22
|
+
* @template TState - The type of the state.
|
|
23
|
+
* @param params - Storage, key, state, schema, and optional callbacks.
|
|
24
|
+
* @param params.storage - The web Storage instance.
|
|
25
|
+
* @param params.key - The storage key.
|
|
26
|
+
* @param params.state - The current state value.
|
|
27
|
+
* @param params.defaultState - The fallback value on validation failure.
|
|
28
|
+
* @param params.schema - Zod schema for validation.
|
|
29
|
+
* @param params.version - Optional schema version passed to writeToStorage.
|
|
30
|
+
* @param params.onValidationFailed - Optional error callback.
|
|
31
|
+
* @param params.onValidationSuccessful - Optional success callback.
|
|
32
|
+
* @param params.skipWriteRef - Optional ref flag to skip one write cycle (key-change guard).
|
|
33
|
+
*/
|
|
34
|
+
export declare const useStorageSyncEffect: <TState>({ storage, key, state, defaultState, schema, version, onValidationFailed, onValidationSuccessful, skipWriteRef, }: UseStorageSyncEffectOptions<TState>) => void;
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Dispatch, type SetStateAction } from "react";
|
|
2
|
+
import type { WebStorageCallbacks, WebStorageOptions } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Internal storage-agnostic hook that backs useLocalStorage and useSessionStorage.
|
|
5
|
+
*
|
|
6
|
+
* @template TState - The type of the stored state.
|
|
7
|
+
* @param storage - The web Storage instance.
|
|
8
|
+
* @param options - Key, defaultState, schema, and optional callbacks.
|
|
9
|
+
* @param options.key - The storage key.
|
|
10
|
+
* @param options.defaultState - Fallback value when no stored data exists.
|
|
11
|
+
* @param options.schema - Zod schema for validation.
|
|
12
|
+
* @param options.migration - Optional migration configuration (version, steps, fromKey).
|
|
13
|
+
* @param options.onValidationFailed - Called when validation fails and no salvage is possible.
|
|
14
|
+
* @param options.onValidationSuccessful - Called when validation succeeds during the write-sync.
|
|
15
|
+
* @param options.onValidationSalvaged - Called when validation fails but partial state was recovered.
|
|
16
|
+
* @returns {Array} A tuple of [state, setState, reset].
|
|
17
|
+
*/
|
|
18
|
+
export declare const useWebStorage: <TState>(storage: Storage, { key, defaultState, schema, migration, onValidationFailed, onValidationSuccessful, onValidationSalvaged, }: WebStorageOptions<TState> & WebStorageCallbacks<TState>) => readonly [TState, Dispatch<SetStateAction<TState>>, () => void];
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type Dispatch } from "react";
|
|
2
|
+
import type { WebStorageCallbacks, WebStorageOptions } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Internal storage-agnostic reducer hook that backs useLocalStorageReducer and useSessionStorageReducer.
|
|
5
|
+
*
|
|
6
|
+
* @template TState - The type of the stored state.
|
|
7
|
+
* @template TAction - The type of the reducer action.
|
|
8
|
+
* @param storage - The web Storage instance.
|
|
9
|
+
* @param options - Key, defaultState, schema, reducer, and optional callbacks.
|
|
10
|
+
* @param options.key - The storage key.
|
|
11
|
+
* @param options.defaultState - Fallback value when no stored data exists.
|
|
12
|
+
* @param options.schema - Zod schema for validation.
|
|
13
|
+
* @param options.migration - Optional migration configuration (version, steps, fromKey).
|
|
14
|
+
* @param options.reducer - The reducer function.
|
|
15
|
+
* @param options.onValidationFailed - Called when validation fails and no salvage is possible.
|
|
16
|
+
* @param options.onValidationSuccessful - Called when validation succeeds during the write-sync.
|
|
17
|
+
* @param options.onValidationSalvaged - Called when validation fails but partial state was recovered.
|
|
18
|
+
* @returns {Array} A tuple of [state, dispatch].
|
|
19
|
+
*/
|
|
20
|
+
export declare const useWebStorageReducer: <TState, TAction>(storage: Storage, { key, defaultState, schema, migration, reducer, onValidationFailed, onValidationSuccessful, onValidationSalvaged, }: WebStorageOptions<TState> & WebStorageCallbacks<TState> & {
|
|
21
|
+
readonly reducer: (state: TState, action: TAction) => TState;
|
|
22
|
+
}) => readonly [TState, Dispatch<TAction>];
|
|
@@ -1,13 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import type { WebStorageCallbacks } from "./types";
|
|
3
|
+
type ValidateStateOptions<TState> = {
|
|
4
|
+
readonly state: unknown;
|
|
5
|
+
readonly defaultState: TState;
|
|
6
|
+
readonly schema: z.ZodType<TState>;
|
|
7
|
+
} & WebStorageCallbacks<TState>;
|
|
6
8
|
/**
|
|
7
|
-
* Validates the state
|
|
9
|
+
* Validates the state using a Zod schema. Returns the parsed state on success,
|
|
10
|
+
* or defaultState on failure (with error logging and optional callbacks).
|
|
8
11
|
*
|
|
9
12
|
* @template TState - The type of the state.
|
|
10
|
-
* @param
|
|
13
|
+
* @param params - The state, schema, defaultState, and optional callbacks.
|
|
14
|
+
* @param params.state - The raw state to validate.
|
|
15
|
+
* @param params.schema - The Zod schema for validation.
|
|
16
|
+
* @param params.defaultState - The fallback value on failure.
|
|
17
|
+
* @param params.onValidationFailed - Optional error callback.
|
|
18
|
+
* @param params.onValidationSuccessful - Optional success callback.
|
|
19
|
+
* @returns {TState} The validated state or defaultState.
|
|
11
20
|
*/
|
|
12
|
-
export declare const validateState: <TState>(
|
|
21
|
+
export declare const validateState: <TState>({ state, schema, onValidationFailed, onValidationSuccessful, defaultState, }: ValidateStateOptions<TState>) => TState;
|
|
13
22
|
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serializes and writes a value to web storage.
|
|
3
|
+
* When a version is provided, wraps the data in a versioned envelope
|
|
4
|
+
* before serializing so the migration pipeline can detect it on read.
|
|
5
|
+
*
|
|
6
|
+
* @param storage - The Storage instance (localStorage / sessionStorage).
|
|
7
|
+
* @param key - The storage key.
|
|
8
|
+
* @param value - The value to serialize and store.
|
|
9
|
+
* @param version - Optional schema version to stamp onto the stored payload.
|
|
10
|
+
*/
|
|
11
|
+
export declare const writeToStorage: (storage: Storage, key: string, value: unknown, version?: number) => void;
|
package/src/index.d.ts
CHANGED
|
@@ -105,8 +105,11 @@ export * from "./components/ValueBar/ValueBarTypes";
|
|
|
105
105
|
export * from "./components/ZStack/ZStack";
|
|
106
106
|
export * from "./components/ZStack/ZStack.variants";
|
|
107
107
|
export * from "./hooks/encoding/useCustomEncoding";
|
|
108
|
+
export * from "./hooks/localStorage/types";
|
|
108
109
|
export * from "./hooks/localStorage/useLocalStorage";
|
|
109
110
|
export * from "./hooks/localStorage/useLocalStorageReducer";
|
|
111
|
+
export * from "./hooks/localStorage/useSessionStorage";
|
|
112
|
+
export * from "./hooks/localStorage/useSessionStorageReducer";
|
|
110
113
|
export * from "./hooks/noPagination";
|
|
111
114
|
export * from "./hooks/useBidirectionalScroll";
|
|
112
115
|
export * from "./hooks/useClickOutside";
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { LocalStorageParams } from "./types";
|
|
2
|
-
/**
|
|
3
|
-
* Initializes the state from local storage, parsing and validating it if a Zod schema is provided.
|
|
4
|
-
*
|
|
5
|
-
* @template TState - The type of the state stored in local storage.
|
|
6
|
-
* @param {Omit<LocalStorageParams<TState>, "state">} params - The parameters for initializing the local storage state.
|
|
7
|
-
* @returns {TState} - The initialized state.
|
|
8
|
-
*/
|
|
9
|
-
export declare const initLocalStorageState: <TState>({ key, defaultState, schema, }: Omit<LocalStorageParams<TState>, "state">) => TState;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sets a value in the local storage with the specified key.
|
|
3
|
-
* Thin wrapper around localStorage.setItem() that is slightly more type safe
|
|
4
|
-
* Stringifies value automatically.
|
|
5
|
-
* Useful if you for some reason can't use the useLocalStorage hook for React lifecycle reasons
|
|
6
|
-
*
|
|
7
|
-
* @template TValue - The type of value to be stored.
|
|
8
|
-
* @param {string} key - The key under which to store the value.
|
|
9
|
-
* @param {TValue} value - The value to store in the local storage.
|
|
10
|
-
*/
|
|
11
|
-
export declare const setLocalStorage: <TValue>(key: string, value: TValue) => void;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { LocalStorageCallbacks, LocalStorageParams } from "./types";
|
|
2
|
-
/**
|
|
3
|
-
* Custom hook for synchronizing a state variable with local storage,
|
|
4
|
-
* with optional schema validation and callbacks.
|
|
5
|
-
*
|
|
6
|
-
* @template TState - The type of the state variable.
|
|
7
|
-
* @param {LocalStorageParams<TState> & LocalStorageCallbacks & { state: TState }} params - The parameters for the useLocalStorageEffect hook.
|
|
8
|
-
* @returns {void}
|
|
9
|
-
*/
|
|
10
|
-
export declare const useLocalStorageEffect: <TState>({ key, state, defaultState, schema, onValidationFailed, onValidationSuccessful, }: LocalStorageParams<TState> & LocalStorageCallbacks<TState> & {
|
|
11
|
-
state: TState;
|
|
12
|
-
}) => void;
|