/** * @fileoverview Local Storage Service for Rantii * @author retoor * @description Manages persistent storage of user data and settings * @keywords storage, localStorage, persistence, settings */ const STORAGE_PREFIX = 'rantii_'; class StorageService { constructor() { this.prefix = STORAGE_PREFIX; } key(name) { return `${this.prefix}${name}`; } set(name, value) { try { const serialized = JSON.stringify(value); localStorage.setItem(this.key(name), serialized); return true; } catch (error) { return false; } } get(name, defaultValue = null) { try { const item = localStorage.getItem(this.key(name)); if (item === null) { return defaultValue; } return JSON.parse(item); } catch (error) { return defaultValue; } } remove(name) { try { localStorage.removeItem(this.key(name)); return true; } catch (error) { return false; } } clear() { try { const keys = Object.keys(localStorage).filter(k => k.startsWith(this.prefix)); keys.forEach(key => localStorage.removeItem(key)); return true; } catch (error) { return false; } } has(name) { return localStorage.getItem(this.key(name)) !== null; } getAuth() { return this.get('auth', null); } setAuth(authData) { return this.set('auth', authData); } clearAuth() { return this.remove('auth'); } getTheme() { return this.get('theme', 'dark'); } setTheme(theme) { return this.set('theme', theme); } getSettings() { return this.get('settings', { theme: 'dark', fontSize: 'medium', notifications: true }); } setSettings(settings) { return this.set('settings', settings); } updateSettings(partial) { const current = this.getSettings(); return this.setSettings({ ...current, ...partial }); } getRecentSearches() { return this.get('recent_searches', []); } addRecentSearch(term) { const searches = this.getRecentSearches(); const filtered = searches.filter(s => s !== term); filtered.unshift(term); return this.set('recent_searches', filtered.slice(0, 10)); } getDraftRant() { return this.get('draft_rant', ''); } setDraftRant(text) { return this.set('draft_rant', text); } clearDraftRant() { return this.remove('draft_rant'); } getDraftComment(rantId) { return this.get(`draft_comment_${rantId}`, ''); } setDraftComment(rantId, text) { return this.set(`draft_comment_${rantId}`, text); } clearDraftComment(rantId) { return this.remove(`draft_comment_${rantId}`); } getCachedProfile(userId) { return this.get(`profile_cache_${userId}`, null); } setCachedProfile(userId, profile) { return this.set(`profile_cache_${userId}`, { data: profile, timestamp: Date.now() }); } isCacheValid(cacheEntry, maxAge = 300000) { if (!cacheEntry || !cacheEntry.timestamp) { return false; } return Date.now() - cacheEntry.timestamp < maxAge; } } export { StorageService, STORAGE_PREFIX };