UNPKG

@shaaz1000/rn-storage

Version:

A comprehensive storage solution for React Native with encryption, caching, and offline sync

73 lines 2.31 kB
"use strict"; // src/hooks/useEncryptedStorage.ts var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useEncryptedStorage = void 0; const react_1 = require("react"); const storageManager_1 = __importDefault(require("../core/storageManager")); function useEncryptedStorage(key, initialValue) { const [value, setValue] = (0, react_1.useState)(initialValue); const [loading, setLoading] = (0, react_1.useState)(true); const [error, setError] = (0, react_1.useState)(null); const storage = storageManager_1.default.getInstance(); // Load encrypted value (0, react_1.useEffect)(() => { const loadValue = async () => { try { setLoading(true); const storedValue = await storage.getItem(key, true); // Always decrypt if (storedValue !== null) { setValue(storedValue); } } catch (err) { setError(err); } finally { setLoading(false); } }; loadValue(); }, [key]); // Update encrypted value const setEncryptedValue = (0, react_1.useCallback)(async (newValue) => { try { setLoading(true); await storage.setItem(key, newValue, true); // Always encrypt setValue(newValue); setError(null); } catch (err) { setError(err); } finally { setLoading(false); } }, [key]); // Remove encrypted value const removeValue = (0, react_1.useCallback)(async () => { try { setLoading(true); await storage.removeItem(key); setValue(initialValue); setError(null); } catch (err) { setError(err); } finally { setLoading(false); } }, [key, initialValue]); return { value, setValue: setEncryptedValue, remove: removeValue, loading, error, }; } exports.useEncryptedStorage = useEncryptedStorage; //# sourceMappingURL=useEncryptedStorage.js.map