import type {UseStorageValueOptions, UseStorageValueResult} from '../useStorageValue/index.js'; import {useStorageValue} from '../useStorageValue/index.js'; import {isBrowser, noop} from '../util/const.js'; let IS_SESSION_STORAGE_AVAILABLE: boolean; try { IS_SESSION_STORAGE_AVAILABLE = isBrowser && Boolean(globalThis.sessionStorage); } catch { IS_SESSION_STORAGE_AVAILABLE = false; } type UseSessionStorageValue = < Type, Default extends Type = Type, Initialize extends boolean | undefined = boolean | undefined, >( key: string, options?: UseStorageValueOptions, ) => UseStorageValueResult; /** * Manages a single sessionStorage key. */ export const useSessionStorageValue: UseSessionStorageValue = IS_SESSION_STORAGE_AVAILABLE ? (key, options) => useStorageValue(sessionStorage, key, options) : ( _key: string, _options?: UseStorageValueOptions, ): UseStorageValueResult => { if (isBrowser && process.env.NODE_ENV === 'development') { console.warn('SessionStorage is not available in this environment'); } // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion return {value: undefined as Type, set: noop, remove: noop, fetch: noop}; };