|
| 1 | +import { log } from '../utilities/log'; |
| 2 | +import { setKeyToLocalStore, getKeyFromLocalStore, clearKeyFromLocalStore } from '../utilities/local-storage'; |
| 3 | + |
| 4 | +const customAttributesLocalStoreName = "gist.web.customAttributes"; |
| 5 | +const defaultExpiryInDays = 30; |
| 6 | + |
| 7 | +// Internal map to store custom attributes in memory |
| 8 | +let customAttributesMap = new Map(); |
| 9 | + |
| 10 | +function loadCustomAttributesFromStorage() { |
| 11 | + const storedAttributes = getKeyFromLocalStore(customAttributesLocalStoreName); |
| 12 | + if (storedAttributes) { |
| 13 | + try { |
| 14 | + customAttributesMap = new Map(storedAttributes); |
| 15 | + } catch { |
| 16 | + customAttributesMap = new Map(); |
| 17 | + } |
| 18 | + } else { |
| 19 | + customAttributesMap = new Map(); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function saveCustomAttributesToStorage() { |
| 24 | + const attributesArray = Array.from(customAttributesMap.entries()); |
| 25 | + const expiryDate = new Date(); |
| 26 | + expiryDate.setDate(expiryDate.getDate() + defaultExpiryInDays); |
| 27 | + |
| 28 | + setKeyToLocalStore(customAttributesLocalStoreName, attributesArray, expiryDate); |
| 29 | + log(`Saved ${customAttributesMap.size} custom attributes to storage with TTL of ${defaultExpiryInDays} days`); |
| 30 | +} |
| 31 | + |
| 32 | +loadCustomAttributesFromStorage(); |
| 33 | + |
| 34 | +export function setCustomAttribute(key, value) { |
| 35 | + if (!key || typeof key !== 'string') { |
| 36 | + log(`Invalid key for custom attribute: ${key}`); |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + customAttributesMap.set(key, value); |
| 41 | + saveCustomAttributesToStorage(); |
| 42 | + log(`Set custom attribute "${key}" to "${value}"`); |
| 43 | + return true; |
| 44 | +} |
| 45 | + |
| 46 | +export function getCustomAttribute(key) { |
| 47 | + if (!key || typeof key !== 'string') { |
| 48 | + log(`Invalid key for custom attribute: ${key}`); |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + return customAttributesMap.get(key) || null; |
| 53 | +} |
| 54 | + |
| 55 | +export function getAllCustomAttributes() { |
| 56 | + return new Map(customAttributesMap); |
| 57 | +} |
| 58 | + |
| 59 | +export function clearCustomAttributes() { |
| 60 | + customAttributesMap.clear(); |
| 61 | + clearKeyFromLocalStore(customAttributesLocalStoreName); |
| 62 | + log(`Cleared all custom attributes`); |
| 63 | +} |
| 64 | + |
| 65 | +export function removeCustomAttribute(key) { |
| 66 | + if (!key || typeof key !== 'string') { |
| 67 | + log(`Invalid key for custom attribute: ${key}`); |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + const existed = customAttributesMap.has(key); |
| 72 | + customAttributesMap.delete(key); |
| 73 | + |
| 74 | + if (customAttributesMap.size > 0) { |
| 75 | + saveCustomAttributesToStorage(); |
| 76 | + } else { |
| 77 | + clearKeyFromLocalStore(customAttributesLocalStoreName); |
| 78 | + } |
| 79 | + |
| 80 | + log(`Removed custom attribute "${key}"`); |
| 81 | + return existed; |
| 82 | +} |
0 commit comments