storage-adapter.js
1 /** 2 * storage-adapter.js 3 * 4 * Polyfills window.storage so VIILegio.jsx works identically on: 5 * - Expo Web → uses the real window.storage (artifact storage API) 6 * - iOS/Android → uses AsyncStorage 7 * 8 * Import this file ONCE at the top of App.js before anything else. 9 */ 10 11 import { Platform } from "react-native"; 12 13 // Only patch on native — web already has window.storage from the artifact host 14 if (Platform.OS !== "web") { 15 const AsyncStorage = require("@react-native-async-storage/async-storage").default; 16 17 const nativeStorage = { 18 async get(key) { 19 try { 20 const value = await AsyncStorage.getItem(key); 21 if (value === null) throw new Error("Key not found"); 22 return { key, value, shared: false }; 23 } catch (e) { 24 throw e; 25 } 26 }, 27 28 async set(key, value, shared = false) { 29 try { 30 const serialized = typeof value === "string" ? value : JSON.stringify(value); 31 await AsyncStorage.setItem(key, serialized); 32 return { key, value: serialized, shared }; 33 } catch (e) { 34 return null; 35 } 36 }, 37 38 async delete(key) { 39 try { 40 await AsyncStorage.removeItem(key); 41 return { key, deleted: true, shared: false }; 42 } catch { 43 return null; 44 } 45 }, 46 47 async list(prefix = "", shared = false) { 48 try { 49 const allKeys = await AsyncStorage.getAllKeys(); 50 const keys = prefix 51 ? allKeys.filter((k) => k.startsWith(prefix)) 52 : allKeys; 53 return { keys, prefix, shared }; 54 } catch { 55 return { keys: [], prefix, shared }; 56 } 57 }, 58 }; 59 60 // Inject into global so window.storage calls in VIILegio work unchanged 61 if (typeof global !== "undefined") { 62 global.window = global.window || {}; 63 global.window.storage = nativeStorage; 64 } 65 }