@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.esm.js CHANGED
@@ -18,6 +18,8 @@ import { useVirtualizer } from '@tanstack/react-virtual';
18
18
  import { HelmetProvider, Helmet } from 'react-helmet-async';
19
19
  import { Trigger, Content, List as List$1, Root } from '@radix-ui/react-tabs';
20
20
  import { gzipSync, gunzipSync } from 'fflate';
21
+ import { z } from 'zod';
22
+ import superjson from 'superjson';
21
23
 
22
24
  const cvaIcon = cvaMerge(["aspect-square", "inline-grid", "relative", "shrink-0"], {
23
25
  variants: {
@@ -8238,179 +8240,615 @@ const useCustomEncoding = () => {
8238
8240
  };
8239
8241
 
8240
8242
  /**
8241
- * The usePrevious hook is a useful tool for tracking the previous value of a variable in a functional component. This can be particularly handy in scenarios where it is necessary to compare the current value with the previous one, such as triggering actions or rendering based on changes.
8243
+ * Runs a sequential migration pipeline on the provided data, applying
8244
+ * each migration whose version is in the range (fromVersion, toVersion].
8242
8245
  *
8243
- * @example
8244
- * const [color, setColor] = React.useState(getRandomColor());
8245
- const previousColor = usePrevious(color);
8246
+ * Throws if the filtered migrations have non-contiguous versions (gaps).
8246
8247
  */
8247
- const usePrevious = (value) => {
8248
- const ref = useRef(undefined);
8249
- useEffect(() => {
8250
- ref.current = value;
8251
- }, [value]);
8252
- const wrapper = useMemo(() => ({
8253
- get previous() {
8254
- return ref.current;
8255
- },
8256
- }), []);
8257
- return wrapper.previous;
8248
+ const runMigrations = ({ data, fromVersion, toVersion, migrations, }) => {
8249
+ if (toVersion <= fromVersion) {
8250
+ return data;
8251
+ }
8252
+ const applicable = migrations
8253
+ .toSorted((a, b) => a.version - b.version)
8254
+ .filter(m => m.version > fromVersion && m.version <= toVersion);
8255
+ if (applicable.length === 0) {
8256
+ return data;
8257
+ }
8258
+ for (let i = 1; i < applicable.length; i++) {
8259
+ const prev = applicable[i - 1];
8260
+ const curr = applicable[i];
8261
+ if (prev && curr && curr.version !== prev.version + 1) {
8262
+ throw new Error(`Migration gap detected: found version ${prev.version} and ${curr.version} but no migration for version ${prev.version + 1}.`);
8263
+ }
8264
+ }
8265
+ return applicable.reduce((acc, migration) => migration.migrate(acc), data);
8258
8266
  };
8259
8267
 
8260
8268
  /**
8261
- * Validates the state object using a Zod schema.
8269
+ * Recursively builds a salvaging schema from a ZodObject by injecting `.catch(defaultValue[key])`
8270
+ * for each field. This means invalid field values fall back to defaultState values rather than
8271
+ * failing the entire parse.
8262
8272
  *
8263
- * @template TState - The type of the state.
8264
- * @param {ValidateStateOptions<TState>} params - The parameters for the validateState function.
8273
+ * Unwraps ZodOptional, ZodNullable, and ZodDefault to reach the inner schema.
8274
+ * ZodCatch is intentionally NOT unwrapped consumer-defined catches take precedence.
8275
+ * Non-ZodObject schemas (unions, discriminated unions, refinements, etc.) are returned unchanged
8276
+ * so they behave as atomic units.
8265
8277
  */
8266
- const validateState = ({ state, schema, onValidationFailed, onValidationSuccessful, defaultState, }) => {
8278
+ function buildSalvagingSchema(schema, defaultValue) {
8279
+ if (schema instanceof z.ZodOptional) {
8280
+ return buildSalvagingSchema(schema.unwrap(), defaultValue).optional();
8281
+ }
8282
+ if (schema instanceof z.ZodNullable) {
8283
+ return buildSalvagingSchema(schema.unwrap(), defaultValue).nullable();
8284
+ }
8285
+ if (schema instanceof z.ZodDefault) {
8286
+ return buildSalvagingSchema(schema._def.innerType, defaultValue);
8287
+ }
8288
+ if (!(schema instanceof z.ZodObject)) {
8289
+ return schema;
8290
+ }
8291
+ const defaultRecordResult = z.record(z.string(), z.unknown()).safeParse(defaultValue);
8292
+ if (!defaultRecordResult.success) {
8293
+ return schema;
8294
+ }
8295
+ const defaultRecord = defaultRecordResult.data;
8296
+ const salvagingShape = {};
8297
+ for (const key of Object.keys(schema.shape)) {
8298
+ const fieldSchema = schema.shape[key];
8299
+ if (fieldSchema === undefined)
8300
+ continue;
8301
+ const fieldDefault = defaultRecord[key];
8302
+ salvagingShape[key] = buildSalvagingSchema(fieldSchema, fieldDefault).catch(fieldDefault);
8303
+ }
8304
+ return z.object(salvagingShape);
8305
+ }
8306
+ /**
8307
+ * Attempts to salvage partial state from raw data when full schema validation has failed.
8308
+ *
8309
+ * For ZodObject schemas, rebuilds the schema with per-field `.catch(defaultState[key])` fallbacks
8310
+ * and reparses. Valid field values from the stored data are kept; invalid fields fall back to their
8311
+ * corresponding values in `defaultState`. Works recursively for nested ZodObject fields.
8312
+ *
8313
+ * Non-object schemas (discriminated unions, unions with `.refine()`, etc.) cannot be partially
8314
+ * salvaged and return `null`.
8315
+ *
8316
+ * Consumer-defined `.catch()` wrappers take precedence over the injected salvage catches, since
8317
+ * Zod processes catches inside-out.
8318
+ *
8319
+ * Note: when every field is invalid, the salvaged result will be deeply equal to `defaultState`.
8320
+ * Callers should compare the result against `defaultState` to distinguish a genuine partial
8321
+ * salvage from a total loss where no stored data survived.
8322
+ *
8323
+ * @template TState - The type of the stored state.
8324
+ * @param schema - Zod schema for validation.
8325
+ * @param rawData - The raw deserialized value that failed validation.
8326
+ * @param defaultState - The authoritative fallback value; used as per-field catch defaults.
8327
+ * @returns {TState | null} The salvaged state, or `null` if salvaging is not possible or also fails.
8328
+ */
8329
+ const salvageState = (schema, rawData, defaultState) => {
8267
8330
  try {
8268
- const parsedState = schema.parse(state);
8269
- onValidationSuccessful?.(parsedState);
8270
- return parsedState;
8331
+ const salvagingSchema = buildSalvagingSchema(schema, defaultState);
8332
+ const intermediate = salvagingSchema.safeParse(rawData);
8333
+ if (!intermediate.success) {
8334
+ return null;
8335
+ }
8336
+ // Re-validate through the original schema to ensure correctness and get the proper TState type.
8337
+ // By construction this should always succeed: each field value is either the valid stored value
8338
+ // or defaultState[key], both of which satisfy the original schema.
8339
+ const result = schema.safeParse(intermediate.data);
8340
+ return result.success ? result.data : null;
8271
8341
  }
8272
- catch (error) {
8273
- // eslint-disable-next-line no-console
8274
- console.error("Failed to parse and validate the state from local storage.", error);
8275
- onValidationFailed?.(error);
8276
- return defaultState;
8342
+ catch {
8343
+ return null;
8277
8344
  }
8278
8345
  };
8279
8346
 
8280
8347
  /**
8281
- * Initializes the state from local storage, parsing and validating it if a Zod schema is provided.
8348
+ * Internal envelope used to tag superjson-serialized data in web storage.
8349
+ *
8350
+ * `__serializer` is a **reserved internal key** — consumer state objects must
8351
+ * not include it as a top-level key. The double-underscore prefix is a
8352
+ * deliberate signal that this is a private implementation detail.
8282
8353
  *
8283
- * @template TState - The type of the state stored in local storage.
8284
- * @param {Omit<LocalStorageParams<TState>, "state">} params - The parameters for initializing the local storage state.
8285
- * @returns {TState} - The initialized state.
8354
+ * A runtime warning is emitted (via `writeToStorage`) if reserved keys are
8355
+ * detected in the value being written.
8286
8356
  */
8287
- const initLocalStorageState = ({ key, defaultState, schema, }) => {
8288
- const localStorageItem = localStorage.getItem(key);
8289
- if (!localStorageItem) {
8357
+ const taggedSuperjsonEnvelopeSchema = z.object({
8358
+ __serializer: z.literal("superjson"),
8359
+ json: z.custom(),
8360
+ meta: z.custom().optional(),
8361
+ });
8362
+ const storageSerializer = {
8363
+ serialize: (value) => {
8364
+ const serialized = superjson.serialize(value);
8365
+ return JSON.stringify({ __serializer: "superjson", ...serialized });
8366
+ },
8367
+ deserialize: (value) => {
8368
+ const parsed = JSON.parse(value);
8369
+ const result = taggedSuperjsonEnvelopeSchema.safeParse(parsed);
8370
+ if (result.success) {
8371
+ return superjson.deserialize({ json: result.data.json, meta: result.data.meta });
8372
+ }
8373
+ return parsed;
8374
+ },
8375
+ };
8376
+
8377
+ /**
8378
+ * Internal envelope that pairs stored data with a schema version number.
8379
+ *
8380
+ * `__version` and `__data` are **reserved internal keys** — consumer state
8381
+ * objects must not include them as top-level keys. The double-underscore
8382
+ * prefix is a deliberate signal that these are private implementation details.
8383
+ *
8384
+ * A runtime warning is emitted (via `writeToStorage`) if reserved keys are
8385
+ * detected in the value being written.
8386
+ */
8387
+ const storageVersionEnvelopeSchema = z
8388
+ .object({
8389
+ __version: z.number(),
8390
+ __data: z.unknown(),
8391
+ })
8392
+ .refine((x) => "__data" in x, {
8393
+ message: "__data is required",
8394
+ });
8395
+ /** Wraps data and version into a versioned storage envelope. */
8396
+ const createStorageVersionEnvelope = (data, version) => ({
8397
+ __version: version,
8398
+ __data: data,
8399
+ });
8400
+
8401
+ /**
8402
+ * Validates raw data against the schema, attempting partial salvage on failure.
8403
+ *
8404
+ * On success: returns the parsed data.
8405
+ * On failure with a salvageable ZodObject schema where at least one stored field
8406
+ * survived: returns the salvaged state and calls `onValidationSalvaged`.
8407
+ * On failure where salvage produces a result identical to `defaultState` (i.e. every
8408
+ * field was invalid and caught to its default): treats this as a total failure and
8409
+ * calls `onValidationFailed` instead of `onValidationSalvaged`.
8410
+ * On failure with no salvage possible: returns `defaultState` and calls `onValidationFailed`.
8411
+ */
8412
+ const validateOrSalvage = ({ rawValue, schema, defaultState, key, onValidationFailed, onValidationSalvaged, }) => {
8413
+ const parseResult = schema.safeParse(rawValue);
8414
+ if (parseResult.success) {
8415
+ return parseResult.data;
8416
+ }
8417
+ const salvaged = salvageState(schema, rawValue, defaultState);
8418
+ if (salvaged !== null && !isEqual(salvaged, defaultState)) {
8419
+ // eslint-disable-next-line no-console
8420
+ console.warn(`Partially invalid data in storage key "${key}" — salvaged valid fields, reset invalid fields to defaults.`, parseResult.error);
8421
+ onValidationSalvaged?.(parseResult.error, salvaged);
8422
+ return salvaged;
8423
+ }
8424
+ // eslint-disable-next-line no-console
8425
+ console.error(`Failed to parse and validate the state from storage key "${key}". Returning default state.`, parseResult.error);
8426
+ onValidationFailed?.(parseResult.error);
8427
+ return defaultState;
8428
+ };
8429
+ /**
8430
+ * Reads and deserializes a value from web storage, validating it against the provided schema.
8431
+ * Falls back to defaultState if the key is missing or data is corrupt.
8432
+ *
8433
+ * On partial validation failure for ZodObject schemas, salvages valid fields rather than
8434
+ * resetting the entire state to `defaultState`. See `salvageState` for salvage mechanics.
8435
+ * If the salvaged result is deeply equal to `defaultState` (i.e. every field was invalid),
8436
+ * the salvage is considered a total loss and `onValidationFailed` is called instead of
8437
+ * `onValidationSalvaged`.
8438
+ *
8439
+ * Deserialization failures (unparseable/corrupt raw data) are routed through
8440
+ * `onValidationFailed` so consumers are always notified when stored data is unusable.
8441
+ *
8442
+ * When `migration` is provided, detects versioned envelopes, runs the
8443
+ * migration pipeline, and validates the migrated result. Non-versioned consumers are
8444
+ * unaffected — the migration path is fully opt-in.
8445
+ *
8446
+ * @template TState - The type of the stored state.
8447
+ * @param params - Storage, key, default state, Zod schema, optional migration config, and optional callbacks.
8448
+ * @param params.storage - The web Storage instance.
8449
+ * @param params.key - The storage key.
8450
+ * @param params.defaultState - Fallback value when no stored data exists or data is corrupt.
8451
+ * @param params.schema - Zod schema to validate the deserialized data.
8452
+ * @param params.migration - Optional migration configuration (version, steps, fromKey).
8453
+ * @param params.onValidationFailed - Called when validation fails and no salvage is possible, or when
8454
+ * deserialization of the raw storage value fails entirely.
8455
+ * @param params.onValidationSalvaged - Called when validation fails but at least one stored field
8456
+ * was recovered (salvaged result differs from defaultState).
8457
+ * @returns {TState} The validated or salvaged state, or defaultState.
8458
+ */
8459
+ const readFromStorage = ({ storage, key, defaultState, schema, migration, onValidationFailed, onValidationSalvaged, }) => {
8460
+ const version = migration?.version;
8461
+ const steps = migration?.steps;
8462
+ const fromKey = migration?.fromKey;
8463
+ let raw = storage.getItem(key);
8464
+ if (raw === null && fromKey !== undefined) {
8465
+ raw = storage.getItem(fromKey);
8466
+ }
8467
+ if (raw === null) {
8290
8468
  return defaultState;
8291
8469
  }
8292
- const localStorageItemJSON = JSON.parse(localStorageItem);
8293
- if (!schema) {
8294
- return localStorageItemJSON;
8470
+ try {
8471
+ const deserialized = storageSerializer.deserialize(raw);
8472
+ if (version === undefined) {
8473
+ return validateOrSalvage({
8474
+ rawValue: deserialized,
8475
+ schema,
8476
+ defaultState,
8477
+ key,
8478
+ onValidationFailed,
8479
+ onValidationSalvaged,
8480
+ });
8481
+ }
8482
+ let storedVersion;
8483
+ let data;
8484
+ const envelopeResult = storageVersionEnvelopeSchema.safeParse(deserialized);
8485
+ if (envelopeResult.success) {
8486
+ storedVersion = envelopeResult.data.__version;
8487
+ data = envelopeResult.data.__data;
8488
+ }
8489
+ else {
8490
+ storedVersion = 0;
8491
+ data = deserialized;
8492
+ }
8493
+ if (steps !== undefined && storedVersion < version) {
8494
+ try {
8495
+ data = runMigrations({ data, fromVersion: storedVersion, toVersion: version, migrations: steps });
8496
+ }
8497
+ catch (migrationError) {
8498
+ // eslint-disable-next-line no-console
8499
+ console.error(`Migration failed for storage key "${key}" (v${storedVersion} → v${version}). Returning default state.`, migrationError);
8500
+ return defaultState;
8501
+ }
8502
+ }
8503
+ return validateOrSalvage({ rawValue: data, schema, defaultState, key, onValidationFailed, onValidationSalvaged });
8504
+ }
8505
+ catch (deserializationError) {
8506
+ // eslint-disable-next-line no-console
8507
+ console.error(`Failed to deserialize storage key "${key}". Returning default state.`, deserializationError);
8508
+ onValidationFailed?.(deserializationError);
8509
+ return defaultState;
8295
8510
  }
8296
- return validateState({ state: localStorageItemJSON, defaultState, schema });
8297
8511
  };
8298
8512
 
8299
8513
  /**
8300
- * Sets a value in the local storage with the specified key.
8301
- * Thin wrapper around localStorage.setItem() that is slightly more type safe
8302
- * Stringifies value automatically.
8303
- * Useful if you for some reason can't use the useLocalStorage hook for React lifecycle reasons
8514
+ * Validates the state using a Zod schema. Returns the parsed state on success,
8515
+ * or defaultState on failure (with error logging and optional callbacks).
8304
8516
  *
8305
- * @template TValue - The type of value to be stored.
8306
- * @param {string} key - The key under which to store the value.
8307
- * @param {TValue} value - The value to store in the local storage.
8517
+ * @template TState - The type of the state.
8518
+ * @param params - The state, schema, defaultState, and optional callbacks.
8519
+ * @param params.state - The raw state to validate.
8520
+ * @param params.schema - The Zod schema for validation.
8521
+ * @param params.defaultState - The fallback value on failure.
8522
+ * @param params.onValidationFailed - Optional error callback.
8523
+ * @param params.onValidationSuccessful - Optional success callback.
8524
+ * @returns {TState} The validated state or defaultState.
8308
8525
  */
8309
- const setLocalStorage = (key, value) => {
8310
- localStorage.setItem(key, JSON.stringify(value));
8526
+ const validateState = ({ state, schema, onValidationFailed, onValidationSuccessful, defaultState, }) => {
8527
+ const result = schema.safeParse(state);
8528
+ if (result.success) {
8529
+ onValidationSuccessful?.(result.data);
8530
+ return result.data;
8531
+ }
8532
+ // eslint-disable-next-line no-console
8533
+ console.error("Failed to parse and validate the state from storage.", result.error);
8534
+ onValidationFailed?.(result.error);
8535
+ return defaultState;
8311
8536
  };
8312
8537
 
8313
8538
  /**
8314
- * Custom hook for synchronizing a state variable with local storage,
8315
- * with optional schema validation and callbacks.
8539
+ * Keys reserved for internal storage envelope metadata.
8540
+ * Consumer state objects must not use these as top-level keys.
8541
+ */
8542
+ const RESERVED_STORAGE_KEYS = ["__data", "__version", "__serializer"];
8543
+ const warnIfReservedKeys = (value) => {
8544
+ if (value === null || typeof value !== "object" || Array.isArray(value))
8545
+ return;
8546
+ for (const key of RESERVED_STORAGE_KEYS) {
8547
+ if (key in value) {
8548
+ // eslint-disable-next-line no-console
8549
+ console.warn(`[useWebStorage] "${key}" is a reserved internal storage key and must not be used in state objects. ` +
8550
+ `It will conflict with the storage serialization envelope.`);
8551
+ }
8552
+ }
8553
+ };
8554
+ /**
8555
+ * Serializes and writes a value to web storage.
8556
+ * When a version is provided, wraps the data in a versioned envelope
8557
+ * before serializing so the migration pipeline can detect it on read.
8316
8558
  *
8317
- * @template TState - The type of the state variable.
8318
- * @param {LocalStorageParams<TState> & LocalStorageCallbacks & { state: TState }} params - The parameters for the useLocalStorageEffect hook.
8319
- * @returns {void}
8559
+ * @param storage - The Storage instance (localStorage / sessionStorage).
8560
+ * @param key - The storage key.
8561
+ * @param value - The value to serialize and store.
8562
+ * @param version - Optional schema version to stamp onto the stored payload.
8320
8563
  */
8321
- const useLocalStorageEffect = ({ key, state, defaultState, schema, onValidationFailed, onValidationSuccessful, }) => {
8322
- const prevState = usePrevious(state);
8323
- const optionsRef = useRef({ schema, defaultState, onValidationFailed, onValidationSuccessful });
8564
+ const writeToStorage = (storage, key, value, version) => {
8565
+ warnIfReservedKeys(value);
8566
+ const payload = version === undefined ? value : createStorageVersionEnvelope(value, version);
8567
+ storage.setItem(key, storageSerializer.serialize(payload));
8568
+ };
8569
+
8570
+ /**
8571
+ * Effect that synchronizes React state to web storage on every state change,
8572
+ * validating through the schema before writing.
8573
+ *
8574
+ * @template TState - The type of the state.
8575
+ * @param params - Storage, key, state, schema, and optional callbacks.
8576
+ * @param params.storage - The web Storage instance.
8577
+ * @param params.key - The storage key.
8578
+ * @param params.state - The current state value.
8579
+ * @param params.defaultState - The fallback value on validation failure.
8580
+ * @param params.schema - Zod schema for validation.
8581
+ * @param params.version - Optional schema version passed to writeToStorage.
8582
+ * @param params.onValidationFailed - Optional error callback.
8583
+ * @param params.onValidationSuccessful - Optional success callback.
8584
+ * @param params.skipWriteRef - Optional ref flag to skip one write cycle (key-change guard).
8585
+ */
8586
+ const useStorageSyncEffect = ({ storage, key, state, defaultState, schema, version, onValidationFailed, onValidationSuccessful, skipWriteRef, }) => {
8587
+ const optionsRef = useRef({ schema, defaultState, onValidationFailed, onValidationSuccessful, version });
8324
8588
  useEffect(() => {
8325
- optionsRef.current = { schema, defaultState, onValidationFailed, onValidationSuccessful };
8326
- }, [schema, defaultState, onValidationFailed, onValidationSuccessful]);
8589
+ optionsRef.current = { schema, defaultState, onValidationFailed, onValidationSuccessful, version };
8590
+ }, [schema, defaultState, onValidationFailed, onValidationSuccessful, version]);
8327
8591
  useEffect(() => {
8328
- if (JSON.stringify(prevState) === JSON.stringify(state)) {
8592
+ if (skipWriteRef?.current) {
8593
+ skipWriteRef.current = false;
8329
8594
  return;
8330
8595
  }
8331
- const { schema: refSchema, defaultState: refDefaultState, onValidationFailed: refOnValidationFailed, onValidationSuccessful: refOnValidationSuccessful, } = optionsRef.current;
8332
- if (refSchema) {
8333
- validateState({
8334
- state,
8335
- schema: refSchema,
8336
- defaultState: refDefaultState,
8337
- onValidationFailed: error => {
8338
- // eslint-disable-next-line no-console
8339
- console.error(`State validation failed. Resetting local storage to default state: ${refDefaultState}.`, error);
8340
- localStorage.removeItem(key);
8341
- refOnValidationFailed?.(error);
8342
- },
8343
- onValidationSuccessful: data => {
8344
- setLocalStorage(key, data);
8345
- refOnValidationSuccessful?.(data);
8346
- },
8347
- });
8348
- }
8349
- else {
8350
- const stringifiedState = JSON.stringify(state);
8351
- localStorage.setItem(key, stringifiedState);
8352
- }
8353
- }, [state, key, prevState]);
8596
+ const { schema: currentSchema, defaultState: currentDefault, onValidationFailed: currentOnFailed, onValidationSuccessful: currentOnSuccess, version: currentVersion, } = optionsRef.current;
8597
+ validateState({
8598
+ state,
8599
+ schema: currentSchema,
8600
+ defaultState: currentDefault,
8601
+ onValidationFailed: error => {
8602
+ // eslint-disable-next-line no-console
8603
+ console.error(`State validation failed for key "${key}". Removing from storage.`, error);
8604
+ storage.removeItem(key);
8605
+ currentOnFailed?.(error);
8606
+ },
8607
+ onValidationSuccessful: data => {
8608
+ writeToStorage(storage, key, data, currentVersion);
8609
+ currentOnSuccess?.(data);
8610
+ },
8611
+ });
8612
+ }, [storage, key, state, skipWriteRef]);
8354
8613
  };
8355
8614
 
8356
8615
  /**
8357
- * Works like a normal useState, but saves to local storage and has optional schema validation.
8616
+ * Internal storage-agnostic hook that backs useLocalStorage and useSessionStorage.
8358
8617
  *
8359
- * @template TState - The type of the value stored in local storage.
8360
- * @param {Omit<LocalStorageParams<TState>, "state"> & LocalStorageCallbacks} options - The options for useLocalStorage.
8361
- * @returns {[TState, Dispatch<SetStateAction<TState>>, () => void]} - A tuple containing the current value, a function to update the value, and a function to remove the value from local storage.
8618
+ * @template TState - The type of the stored state.
8619
+ * @param storage - The web Storage instance.
8620
+ * @param options - Key, defaultState, schema, and optional callbacks.
8621
+ * @param options.key - The storage key.
8622
+ * @param options.defaultState - Fallback value when no stored data exists.
8623
+ * @param options.schema - Zod schema for validation.
8624
+ * @param options.migration - Optional migration configuration (version, steps, fromKey).
8625
+ * @param options.onValidationFailed - Called when validation fails and no salvage is possible.
8626
+ * @param options.onValidationSuccessful - Called when validation succeeds during the write-sync.
8627
+ * @param options.onValidationSalvaged - Called when validation fails but partial state was recovered.
8628
+ * @returns {Array} A tuple of [state, setState, reset].
8362
8629
  */
8363
- const useLocalStorage = ({ key, defaultState, schema, onValidationFailed, onValidationSuccessful, }) => {
8630
+ const useWebStorage = (storage, { key, defaultState, schema, migration, onValidationFailed, onValidationSuccessful, onValidationSalvaged, }) => {
8364
8631
  if (!key) {
8365
- throw new Error("useLocalStorage key must be defined");
8632
+ throw new Error("useWebStorage: key must be a non-empty string");
8366
8633
  }
8367
- const [state, setState] = useState(() => initLocalStorageState({ key, defaultState, schema }));
8368
- const prevKey = usePrevious(key);
8634
+ const [state, setState] = useState(() => readFromStorage({ storage, key, defaultState, schema, migration, onValidationFailed, onValidationSalvaged }));
8635
+ const prevKeyRef = useRef(key);
8369
8636
  const defaultStateRef = useRef(defaultState);
8637
+ const schemaRef = useRef(schema);
8638
+ const migrationRef = useRef(migration);
8639
+ const onValidationFailedRef = useRef(onValidationFailed);
8640
+ const onValidationSalvagedRef = useRef(onValidationSalvaged);
8370
8641
  useEffect(() => {
8371
8642
  defaultStateRef.current = defaultState;
8372
8643
  }, [defaultState]);
8373
- const schemaRef = useRef(schema);
8374
8644
  useEffect(() => {
8375
8645
  schemaRef.current = schema;
8376
8646
  }, [schema]);
8377
8647
  useEffect(() => {
8378
- if (key !== prevKey) {
8379
- setState(initLocalStorageState({ key, defaultState: defaultStateRef.current, schema: schemaRef.current }));
8380
- }
8381
- }, [key, prevKey]);
8382
- const localStorageProps = useMemo(() => ({
8648
+ migrationRef.current = migration;
8649
+ }, [migration]);
8650
+ useEffect(() => {
8651
+ onValidationFailedRef.current = onValidationFailed;
8652
+ }, [onValidationFailed]);
8653
+ useEffect(() => {
8654
+ onValidationSalvagedRef.current = onValidationSalvaged;
8655
+ }, [onValidationSalvaged]);
8656
+ const keyChangedRef = useRef(false);
8657
+ useEffect(() => {
8658
+ if (key !== prevKeyRef.current) {
8659
+ prevKeyRef.current = key;
8660
+ keyChangedRef.current = true;
8661
+ setState(readFromStorage({
8662
+ storage,
8663
+ key,
8664
+ defaultState: defaultStateRef.current,
8665
+ schema: schemaRef.current,
8666
+ migration: migrationRef.current,
8667
+ onValidationFailed: onValidationFailedRef.current,
8668
+ onValidationSalvaged: onValidationSalvagedRef.current,
8669
+ }));
8670
+ }
8671
+ }, [key, storage]);
8672
+ useStorageSyncEffect({
8673
+ storage,
8383
8674
  key,
8384
8675
  state,
8385
8676
  defaultState,
8386
8677
  schema,
8678
+ version: migration?.version,
8387
8679
  onValidationFailed,
8388
8680
  onValidationSuccessful,
8389
- }), [key, state, defaultState, schema, onValidationFailed, onValidationSuccessful]);
8390
- useLocalStorageEffect(localStorageProps);
8681
+ skipWriteRef: keyChangedRef,
8682
+ });
8683
+ useEffect(() => {
8684
+ const fromKey = migration?.fromKey;
8685
+ if (fromKey && storage.getItem(key) !== null) {
8686
+ storage.removeItem(fromKey);
8687
+ }
8688
+ }, [migration?.fromKey, storage, key]);
8391
8689
  const reset = useCallback(() => {
8392
8690
  setState(defaultStateRef.current);
8393
8691
  }, []);
8394
- return useMemo(() => [state, setState, reset], [state, setState, reset]);
8692
+ return useMemo(() => [state, setState, reset], [state, reset]);
8395
8693
  };
8396
8694
 
8397
8695
  /**
8398
- * Works like a normal useReducer, but saves to local storage and has optional schema validation.
8696
+ * Works like useState, but persists to localStorage with Zod schema validation
8697
+ * and superjson serialization (supports Date, Map, Set, BigInt, etc.).
8399
8698
  *
8400
- * @template TState - The type of the state.
8401
- * @template TAction - The type of the action.
8402
- * @param {LocalStorageParams<TState> & LocalStorageCallbacks} params - The parameters for the useLocalStorageReducer function.
8403
- * @returns {[TState, Dispatch<TAction>]} - A tuple containing the state and the dispatch function.
8699
+ * @template TState - The type of the stored state.
8700
+ * @param options - Key, defaultState, schema, and optional callbacks.
8701
+ * @param options.key - The storage key.
8702
+ * @param options.defaultState - Fallback value when no stored data exists.
8703
+ * @param options.schema - Zod schema for validation.
8704
+ * @param options.onValidationFailed - Optional error callback.
8705
+ * @param options.onValidationSuccessful - Optional success callback.
8706
+ * @returns {Array} A tuple of [state, setState, reset].
8404
8707
  */
8405
- const useLocalStorageReducer = ({ key, defaultState, reducer, schema, onValidationFailed, onValidationSuccessful, }) => {
8708
+ const useLocalStorage = (options) => useWebStorage(globalThis.localStorage, options);
8709
+
8710
+ /**
8711
+ * Internal storage-agnostic reducer hook that backs useLocalStorageReducer and useSessionStorageReducer.
8712
+ *
8713
+ * @template TState - The type of the stored state.
8714
+ * @template TAction - The type of the reducer action.
8715
+ * @param storage - The web Storage instance.
8716
+ * @param options - Key, defaultState, schema, reducer, and optional callbacks.
8717
+ * @param options.key - The storage key.
8718
+ * @param options.defaultState - Fallback value when no stored data exists.
8719
+ * @param options.schema - Zod schema for validation.
8720
+ * @param options.migration - Optional migration configuration (version, steps, fromKey).
8721
+ * @param options.reducer - The reducer function.
8722
+ * @param options.onValidationFailed - Called when validation fails and no salvage is possible.
8723
+ * @param options.onValidationSuccessful - Called when validation succeeds during the write-sync.
8724
+ * @param options.onValidationSalvaged - Called when validation fails but partial state was recovered.
8725
+ * @returns {Array} A tuple of [state, dispatch].
8726
+ */
8727
+ const useWebStorageReducer = (storage, { key, defaultState, schema, migration, reducer, onValidationFailed, onValidationSuccessful, onValidationSalvaged, }) => {
8406
8728
  if (!key) {
8407
- throw new Error("useLocalStorage key may not be falsy");
8729
+ throw new Error("useWebStorageReducer: key must be a non-empty string");
8408
8730
  }
8409
- const [state, dispatch] = useReducer(reducer, defaultState, () => initLocalStorageState({ key, defaultState, schema }));
8410
- useLocalStorageEffect({ key, state, defaultState, schema, onValidationFailed, onValidationSuccessful });
8411
- return [state, dispatch];
8731
+ const internalReducer = (prevState, internal) => {
8732
+ switch (internal.kind) {
8733
+ case "reinit":
8734
+ return internal.state;
8735
+ case "user":
8736
+ return reducer(prevState, internal.action);
8737
+ default:
8738
+ return internal;
8739
+ }
8740
+ };
8741
+ const [state, internalDispatch] = useReducer(internalReducer, undefined, () => readFromStorage({ storage, key, defaultState, schema, migration, onValidationFailed, onValidationSalvaged }));
8742
+ const userDispatch = useCallback((action) => internalDispatch({ kind: "user", action }), []);
8743
+ const prevKeyRef = useRef(key);
8744
+ const defaultStateRef = useRef(defaultState);
8745
+ const schemaRef = useRef(schema);
8746
+ const migrationRef = useRef(migration);
8747
+ const onValidationFailedRef = useRef(onValidationFailed);
8748
+ const onValidationSalvagedRef = useRef(onValidationSalvaged);
8749
+ useEffect(() => {
8750
+ defaultStateRef.current = defaultState;
8751
+ }, [defaultState]);
8752
+ useEffect(() => {
8753
+ schemaRef.current = schema;
8754
+ }, [schema]);
8755
+ useEffect(() => {
8756
+ migrationRef.current = migration;
8757
+ }, [migration]);
8758
+ useEffect(() => {
8759
+ onValidationFailedRef.current = onValidationFailed;
8760
+ }, [onValidationFailed]);
8761
+ useEffect(() => {
8762
+ onValidationSalvagedRef.current = onValidationSalvaged;
8763
+ }, [onValidationSalvaged]);
8764
+ const keyChangedRef = useRef(false);
8765
+ useEffect(() => {
8766
+ if (key !== prevKeyRef.current) {
8767
+ prevKeyRef.current = key;
8768
+ keyChangedRef.current = true;
8769
+ internalDispatch({
8770
+ kind: "reinit",
8771
+ state: readFromStorage({
8772
+ storage,
8773
+ key,
8774
+ defaultState: defaultStateRef.current,
8775
+ schema: schemaRef.current,
8776
+ migration: migrationRef.current,
8777
+ onValidationFailed: onValidationFailedRef.current,
8778
+ onValidationSalvaged: onValidationSalvagedRef.current,
8779
+ }),
8780
+ });
8781
+ }
8782
+ }, [key, storage]);
8783
+ useStorageSyncEffect({
8784
+ storage,
8785
+ key,
8786
+ state,
8787
+ defaultState,
8788
+ schema,
8789
+ version: migration?.version,
8790
+ onValidationFailed,
8791
+ onValidationSuccessful,
8792
+ skipWriteRef: keyChangedRef,
8793
+ });
8794
+ useEffect(() => {
8795
+ const fromKey = migration?.fromKey;
8796
+ if (fromKey && storage.getItem(key) !== null) {
8797
+ storage.removeItem(fromKey);
8798
+ }
8799
+ }, [migration?.fromKey, storage, key]);
8800
+ return useMemo(() => [state, userDispatch], [state, userDispatch]);
8412
8801
  };
8413
8802
 
8803
+ /**
8804
+ * Works like useReducer, but persists to localStorage with Zod schema validation
8805
+ * and superjson serialization (supports Date, Map, Set, BigInt, etc.).
8806
+ *
8807
+ * @template TState - The type of the stored state.
8808
+ * @template TAction - The type of the reducer action.
8809
+ * @param options - Key, defaultState, schema, reducer, and optional callbacks.
8810
+ * @param options.key - The storage key.
8811
+ * @param options.defaultState - Fallback value when no stored data exists.
8812
+ * @param options.schema - Zod schema for validation.
8813
+ * @param options.reducer - The reducer function.
8814
+ * @param options.onValidationFailed - Optional error callback.
8815
+ * @param options.onValidationSuccessful - Optional success callback.
8816
+ * @returns {Array} A tuple of [state, dispatch].
8817
+ */
8818
+ const useLocalStorageReducer = (options) => useWebStorageReducer(globalThis.localStorage, options);
8819
+
8820
+ /**
8821
+ * Works like useState, but persists to sessionStorage with Zod schema validation
8822
+ * and superjson serialization (supports Date, Map, Set, BigInt, etc.).
8823
+ *
8824
+ * @template TState - The type of the stored state.
8825
+ * @param options - Key, defaultState, schema, and optional callbacks.
8826
+ * @param options.key - The storage key.
8827
+ * @param options.defaultState - Fallback value when no stored data exists.
8828
+ * @param options.schema - Zod schema for validation.
8829
+ * @param options.onValidationFailed - Optional error callback.
8830
+ * @param options.onValidationSuccessful - Optional success callback.
8831
+ * @returns {Array} A tuple of [state, setState, reset].
8832
+ */
8833
+ const useSessionStorage = (options) => useWebStorage(globalThis.sessionStorage, options);
8834
+
8835
+ /**
8836
+ * Works like useReducer, but persists to sessionStorage with Zod schema validation
8837
+ * and superjson serialization (supports Date, Map, Set, BigInt, etc.).
8838
+ *
8839
+ * @template TState - The type of the stored state.
8840
+ * @template TAction - The type of the reducer action.
8841
+ * @param options - Key, defaultState, schema, reducer, and optional callbacks.
8842
+ * @param options.key - The storage key.
8843
+ * @param options.defaultState - Fallback value when no stored data exists.
8844
+ * @param options.schema - Zod schema for validation.
8845
+ * @param options.reducer - The reducer function.
8846
+ * @param options.onValidationFailed - Optional error callback.
8847
+ * @param options.onValidationSuccessful - Optional success callback.
8848
+ * @returns {Array} A tuple of [state, dispatch].
8849
+ */
8850
+ const useSessionStorageReducer = (options) => useWebStorageReducer(globalThis.sessionStorage, options);
8851
+
8414
8852
  const OVERSCAN = 10;
8415
8853
  const DEFAULT_ROW_HEIGHT = 50;
8416
8854
  /**
@@ -9404,6 +9842,26 @@ const useModifierKey = ({ exclude = [] } = {}) => {
9404
9842
  return isModifierPressed;
9405
9843
  };
9406
9844
 
9845
+ /**
9846
+ * The usePrevious hook is a useful tool for tracking the previous value of a variable in a functional component. This can be particularly handy in scenarios where it is necessary to compare the current value with the previous one, such as triggering actions or rendering based on changes.
9847
+ *
9848
+ * @example
9849
+ * const [color, setColor] = React.useState(getRandomColor());
9850
+ const previousColor = usePrevious(color);
9851
+ */
9852
+ const usePrevious = (value) => {
9853
+ const ref = useRef(undefined);
9854
+ useEffect(() => {
9855
+ ref.current = value;
9856
+ }, [value]);
9857
+ const wrapper = useMemo(() => ({
9858
+ get previous() {
9859
+ return ref.current;
9860
+ },
9861
+ }), []);
9862
+ return wrapper.previous;
9863
+ };
9864
+
9407
9865
  const defaultPageSize = 50;
9408
9866
  /**
9409
9867
  * Custom hook for handling Relay pagination in tables.
@@ -9748,4 +10206,4 @@ const useWindowActivity = ({ onFocus, onBlur, skip = false } = { onBlur: undefin
9748
10206
  return useMemo(() => ({ focused }), [focused]);
9749
10207
  };
9750
10208
 
9751
- export { ActionRenderer, Alert, Badge, Breadcrumb, BreadcrumbContainer, Button, Card, CardBody, CardFooter, CardHeader, Collapse, CompletionStatusIndicator, CopyableText, DEFAULT_SKELETON_PREFERENCE_CARD_PROPS, DetailsList, EmptyState, EmptyValue, ExternalLink, GridAreas, Heading, Highlight, HorizontalOverflowScroller, Icon, IconButton, Indicator, KPI, KPICard, KPICardSkeleton, KPISkeleton, List, ListItem, MenuDivider, MenuItem, MenuList, MoreMenu, Notice, PackageNameStoryComponent, Page, PageContent, PageHeader, PageHeaderKpiMetrics, PageHeaderSecondaryActions, PageHeaderTitle, Pagination, Polygon, Popover, PopoverContent, PopoverTitle, PopoverTrigger, Portal, PreferenceCard, PreferenceCardSkeleton, Prompt, ROLE_CARD, SectionHeader, SegmentedValueBar, Sidebar, SkeletonBlock, SkeletonLabel, SkeletonLines, Spacer, Spinner, StarButton, Tab, TabContent, TabList, Tabs, Tag, Text, ToggleGroup, Tooltip, TrendIndicator, TrendIndicators, ValueBar, ZStack, createGrid, cvaButton, cvaButtonPrefixSuffix, cvaButtonSpinner, cvaButtonSpinnerContainer, cvaClickable, cvaContainerStyles, cvaContentContainer, cvaContentWrapper, cvaDescriptionCard, cvaIconBackground, cvaIconButton, cvaImgStyles, cvaIndicator, cvaIndicatorIcon, cvaIndicatorIconBackground, cvaIndicatorLabel, cvaIndicatorPing, cvaInputContainer, cvaInteractableItem, cvaList, cvaListContainer, cvaListItem$1 as cvaListItem, cvaMenu, cvaMenuItem, cvaMenuItemLabel, cvaMenuItemPrefix, cvaMenuItemStyle, cvaMenuItemSuffix, cvaMenuList, cvaMenuListDivider, cvaMenuListItem, cvaMenuListMultiSelect, cvaPageHeader, cvaPageHeaderContainer, cvaPageHeaderHeading, cvaPreferenceCard, cvaTitleCard, cvaToggleGroup, cvaToggleGroupWithSlidingBackground, cvaToggleItem, cvaToggleItemContent, cvaToggleItemText, cvaZStackContainer, cvaZStackItem, defaultPageSize, docs, getDevicePixelRatio, getValueBarColorByValue, iconColorNames, iconPalette, noPagination, preferenceCardGrid, useBidirectionalScroll, useClickOutside, useContainerBreakpoints, useContinuousTimeout, useCopyToClipboard, useCursorUrlSync, useCustomEncoding, useDebounce, useDevicePixelRatio, useElevatedReducer, useElevatedState, useGeolocation, useGridAreas, useHover, useInfiniteScroll, useIsFirstRender, useIsFullscreen, useIsTextTruncated, useKeyboardShortcut, useList, useListItemHeight, useLocalStorage, useLocalStorageReducer, useMeasure, useMergeRefs, useModifierKey, useOverflowItems, usePopoverContext, usePrevious, usePrompt, useRandomCSSLengths, useRelayPagination, useResize, useScrollBlock, useScrollDetection, useSelfUpdatingRef, useTextSearch, useTimeout, useViewportBreakpoints, useWatch, useWindowActivity };
10209
+ export { ActionRenderer, Alert, Badge, Breadcrumb, BreadcrumbContainer, Button, Card, CardBody, CardFooter, CardHeader, Collapse, CompletionStatusIndicator, CopyableText, DEFAULT_SKELETON_PREFERENCE_CARD_PROPS, DetailsList, EmptyState, EmptyValue, ExternalLink, GridAreas, Heading, Highlight, HorizontalOverflowScroller, Icon, IconButton, Indicator, KPI, KPICard, KPICardSkeleton, KPISkeleton, List, ListItem, MenuDivider, MenuItem, MenuList, MoreMenu, Notice, PackageNameStoryComponent, Page, PageContent, PageHeader, PageHeaderKpiMetrics, PageHeaderSecondaryActions, PageHeaderTitle, Pagination, Polygon, Popover, PopoverContent, PopoverTitle, PopoverTrigger, Portal, PreferenceCard, PreferenceCardSkeleton, Prompt, ROLE_CARD, SectionHeader, SegmentedValueBar, Sidebar, SkeletonBlock, SkeletonLabel, SkeletonLines, Spacer, Spinner, StarButton, Tab, TabContent, TabList, Tabs, Tag, Text, ToggleGroup, Tooltip, TrendIndicator, TrendIndicators, ValueBar, ZStack, createGrid, cvaButton, cvaButtonPrefixSuffix, cvaButtonSpinner, cvaButtonSpinnerContainer, cvaClickable, cvaContainerStyles, cvaContentContainer, cvaContentWrapper, cvaDescriptionCard, cvaIconBackground, cvaIconButton, cvaImgStyles, cvaIndicator, cvaIndicatorIcon, cvaIndicatorIconBackground, cvaIndicatorLabel, cvaIndicatorPing, cvaInputContainer, cvaInteractableItem, cvaList, cvaListContainer, cvaListItem$1 as cvaListItem, cvaMenu, cvaMenuItem, cvaMenuItemLabel, cvaMenuItemPrefix, cvaMenuItemStyle, cvaMenuItemSuffix, cvaMenuList, cvaMenuListDivider, cvaMenuListItem, cvaMenuListMultiSelect, cvaPageHeader, cvaPageHeaderContainer, cvaPageHeaderHeading, cvaPreferenceCard, cvaTitleCard, cvaToggleGroup, cvaToggleGroupWithSlidingBackground, cvaToggleItem, cvaToggleItemContent, cvaToggleItemText, cvaZStackContainer, cvaZStackItem, defaultPageSize, docs, getDevicePixelRatio, getValueBarColorByValue, iconColorNames, iconPalette, noPagination, preferenceCardGrid, useBidirectionalScroll, useClickOutside, useContainerBreakpoints, useContinuousTimeout, useCopyToClipboard, useCursorUrlSync, useCustomEncoding, useDebounce, useDevicePixelRatio, useElevatedReducer, useElevatedState, useGeolocation, useGridAreas, useHover, useInfiniteScroll, useIsFirstRender, useIsFullscreen, useIsTextTruncated, useKeyboardShortcut, useList, useListItemHeight, useLocalStorage, useLocalStorageReducer, useMeasure, useMergeRefs, useModifierKey, useOverflowItems, usePopoverContext, usePrevious, usePrompt, useRandomCSSLengths, useRelayPagination, useResize, useScrollBlock, useScrollDetection, useSelfUpdatingRef, useSessionStorage, useSessionStorageReducer, useTextSearch, useTimeout, useViewportBreakpoints, useWatch, useWindowActivity };