/** * useStorage — Typed persistence wrapper around uni.getStorageSync/setStorageSync */ export function useStorage() { /** * Load a value from storage with a fallback default. * Returns fallback if key doesn't exist or JSON parsing fails. */ function loadValue(key: string, fallback: T): T { try { const raw = uni.getStorageSync(key) if (raw === '' || raw === undefined || raw === null) { return fallback } const parsed = JSON.parse(raw) as T return parsed !== null ? parsed : fallback } catch (_e) { console.warn(`[useStorage] Failed to load key "${key}", using fallback`) return fallback } } /** * Save a value to storage as JSON. */ function saveValue(key: string, value: T): void { try { const raw = JSON.stringify(value) uni.setStorageSync(key, raw) } catch (e) { console.error(`[useStorage] Failed to save key "${key}":`, e) } } /** * Remove a key from storage. */ function removeValue(key: string): void { try { uni.removeStorageSync(key) } catch (e) { console.error(`[useStorage] Failed to remove key "${key}":`, e) } } return { loadValue, saveValue, removeValue, } }