{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-local-storage",
  "type": "registry:hook",
  "title": "useLocalStorage",
  "description": "React hook for local and session storage state.",
  "files": [
    {
      "path": "packages/react/src/hooks/use-local-storage.ts",
      "type": "registry:hook",
      "target": "hooks/use-local-storage.ts",
      "content": "import { useState, useCallback } from 'react';\n\n/**\n * 存储键的类型\n */\ntype StorageKey = string;\n\n/**\n * 使用本地存储的 hook\n * @param key - 存储键名\n * @param defaultValue - 默认值\n * @returns [值, 设置值的函数]\n */\nexport function useLocalStorage<T>(key: StorageKey, defaultValue: T | (() => T) = '' as T): [T, (value: T | ((prev: T) => T)) => void] {\n\tconst [storedValue, setStoredValue] = useState<T>(() => {\n\t\tif (typeof window === 'undefined') {\n\t\t\treturn typeof defaultValue === 'function' ? (defaultValue as () => T)() : defaultValue;\n\t\t}\n\n\t\ttry {\n\t\t\tconst item = window.localStorage.getItem(key);\n\t\t\tif (item === null) {\n\t\t\t\treturn typeof defaultValue === 'function' ? (defaultValue as () => T)() : defaultValue;\n\t\t\t}\n\t\t\treturn JSON.parse(item);\n\t\t} catch (error) {\n\t\t\tconsole.warn(`Error reading localStorage key \"${key}\":`, error);\n\t\t\treturn typeof defaultValue === 'function' ? (defaultValue as () => T)() : defaultValue;\n\t\t}\n\t});\n\n\tconst setValue = useCallback(\n\t\t(value: T | ((prev: T) => T)) => {\n\t\t\ttry {\n\t\t\t\tconst valueToStore = value instanceof Function ? value(storedValue) : value;\n\t\t\t\tsetStoredValue(valueToStore);\n\t\t\t\tif (typeof window !== 'undefined') {\n\t\t\t\t\twindow.localStorage.setItem(key, JSON.stringify(valueToStore));\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tconsole.warn(`Error setting localStorage key \"${key}\":`, error);\n\t\t\t}\n\t\t},\n\t\t[key, storedValue],\n\t);\n\n\treturn [storedValue, setValue];\n}\n\n/**\n * 使用会话存储的 hook\n */\nexport function useSessionStorage<T>(key: StorageKey, defaultValue: T | (() => T) = '' as T): [T, (value: T | ((prev: T) => T)) => void] {\n\tconst [storedValue, setStoredValue] = useState<T>(() => {\n\t\tif (typeof window === 'undefined') {\n\t\t\treturn typeof defaultValue === 'function' ? (defaultValue as () => T)() : defaultValue;\n\t\t}\n\n\t\ttry {\n\t\t\tconst item = window.sessionStorage.getItem(key);\n\t\t\tif (item === null) {\n\t\t\t\treturn typeof defaultValue === 'function' ? (defaultValue as () => T)() : defaultValue;\n\t\t\t}\n\t\t\treturn JSON.parse(item);\n\t\t} catch (error) {\n\t\t\tconsole.warn(`Error reading sessionStorage key \"${key}\":`, error);\n\t\t\treturn typeof defaultValue === 'function' ? (defaultValue as () => T)() : defaultValue;\n\t\t}\n\t});\n\n\tconst setValue = useCallback(\n\t\t(value: T | ((prev: T) => T)) => {\n\t\t\ttry {\n\t\t\t\tconst valueToStore = value instanceof Function ? value(storedValue) : value;\n\t\t\t\tsetStoredValue(valueToStore);\n\t\t\t\tif (typeof window !== 'undefined') {\n\t\t\t\t\twindow.sessionStorage.setItem(key, JSON.stringify(valueToStore));\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tconsole.warn(`Error setting sessionStorage key \"${key}\":`, error);\n\t\t\t}\n\t\t},\n\t\t[key, storedValue],\n\t);\n\n\treturn [storedValue, setValue];\n}\n"
    }
  ]
}
