|
| 1 | +/** |
| 2 | + * Session storage utilities for tracking meme engagement actions. |
| 3 | + * Prevents duplicate tracking of downloads and shares within the same browser session. |
| 4 | + */ |
| 5 | + |
| 6 | +export type EngagementAction = 'download' | 'share'; |
| 7 | + |
| 8 | +// Session storage keys for tracking engagement |
| 9 | +const STORAGE_KEYS = { |
| 10 | + DOWNLOADED_MEMES: 'memedb_downloaded_memes', |
| 11 | + SHARED_MEMES: 'memedb_shared_memes', |
| 12 | +} as const; |
| 13 | + |
| 14 | +/** |
| 15 | + * Checks if session storage is available. |
| 16 | + * Returns false in private browsing mode or when storage is disabled. |
| 17 | + */ |
| 18 | +function isSessionStorageAvailable(): boolean { |
| 19 | + try { |
| 20 | + const testKey = '__storage_test__'; |
| 21 | + sessionStorage.setItem(testKey, 'test'); |
| 22 | + sessionStorage.removeItem(testKey); |
| 23 | + return true; |
| 24 | + } catch (e) { |
| 25 | + return false; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Gets the session storage key for a given action type. |
| 31 | + */ |
| 32 | +function getStorageKey(action: EngagementAction): string { |
| 33 | + return action === 'download' |
| 34 | + ? STORAGE_KEYS.DOWNLOADED_MEMES |
| 35 | + : STORAGE_KEYS.SHARED_MEMES; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Retrieves the set of tracked meme IDs for a given action from session storage. |
| 40 | + * Returns an empty set if storage is unavailable or data is corrupted. |
| 41 | + */ |
| 42 | +function getTrackedMemes(action: EngagementAction): Set<string> { |
| 43 | + if (!isSessionStorageAvailable()) { |
| 44 | + return new Set(); |
| 45 | + } |
| 46 | + |
| 47 | + try { |
| 48 | + const key = getStorageKey(action); |
| 49 | + const stored = sessionStorage.getItem(key); |
| 50 | + |
| 51 | + if (!stored) { |
| 52 | + return new Set(); |
| 53 | + } |
| 54 | + |
| 55 | + const parsed = JSON.parse(stored); |
| 56 | + |
| 57 | + // Validate that parsed data is an array |
| 58 | + if (!Array.isArray(parsed)) { |
| 59 | + return new Set(); |
| 60 | + } |
| 61 | + |
| 62 | + return new Set(parsed); |
| 63 | + } catch (e) { |
| 64 | + // Handle JSON parse errors or other exceptions gracefully |
| 65 | + console.warn(`Failed to retrieve tracked memes for ${action}:`, e); |
| 66 | + return new Set(); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Saves the set of tracked meme IDs for a given action to session storage. |
| 72 | + * Fails silently if storage is unavailable. |
| 73 | + */ |
| 74 | +function saveTrackedMemes(action: EngagementAction, memeIds: Set<string>): void { |
| 75 | + if (!isSessionStorageAvailable()) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + const key = getStorageKey(action); |
| 81 | + const array = Array.from(memeIds); |
| 82 | + sessionStorage.setItem(key, JSON.stringify(array)); |
| 83 | + } catch (e) { |
| 84 | + // Handle quota exceeded or other storage errors gracefully |
| 85 | + console.warn(`Failed to save tracked memes for ${action}:`, e); |
| 86 | + |
| 87 | + // If quota exceeded, try to clear old entries and retry |
| 88 | + if (e instanceof DOMException && e.name === 'QuotaExceededError') { |
| 89 | + try { |
| 90 | + const key = getStorageKey(action); |
| 91 | + sessionStorage.removeItem(key); |
| 92 | + sessionStorage.setItem(key, JSON.stringify(Array.from(memeIds))); |
| 93 | + } catch (retryError) { |
| 94 | + // If retry fails, fail silently |
| 95 | + console.warn(`Retry failed for ${action}:`, retryError); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Checks if a meme action has already been tracked in the current session. |
| 103 | + * |
| 104 | + * @param memeId - The UUID of the meme |
| 105 | + * @param action - The engagement action type ('download' or 'share') |
| 106 | + * @returns true if the action has been tracked, false otherwise |
| 107 | + * |
| 108 | + * @example |
| 109 | + * if (!isActionTracked(meme.id, 'download')) { |
| 110 | + * // Track the download |
| 111 | + * } |
| 112 | + */ |
| 113 | +export function isActionTracked(memeId: string, action: EngagementAction): boolean { |
| 114 | + const trackedMemes = getTrackedMemes(action); |
| 115 | + return trackedMemes.has(memeId); |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Marks a meme action as tracked in the current session. |
| 120 | + * This prevents duplicate tracking of the same action within the session. |
| 121 | + * |
| 122 | + * @param memeId - The UUID of the meme |
| 123 | + * @param action - The engagement action type ('download' or 'share') |
| 124 | + * |
| 125 | + * @example |
| 126 | + * markActionTracked(meme.id, 'download'); |
| 127 | + */ |
| 128 | +export function markActionTracked(memeId: string, action: EngagementAction): void { |
| 129 | + const trackedMemes = getTrackedMemes(action); |
| 130 | + trackedMemes.add(memeId); |
| 131 | + saveTrackedMemes(action, trackedMemes); |
| 132 | +} |
0 commit comments