-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalStorageManager.js
More file actions
63 lines (55 loc) · 1.86 KB
/
LocalStorageManager.js
File metadata and controls
63 lines (55 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* LocalStorageManager.js
*
* A centralized module for managing localStorage keys and interactions.
* This prevents key name collisions and standardizes getting/setting values.
*/
// --- Key Definitions ---
// Animation Settings
export const KEY_IS_ANIMATION_ENABLED = 'isAnimationEnabled';
export const KEY_SHOW_ANIMATIONS_HOMEPAGE = 'showAnimationsHomepage';
export const KEY_SHOW_ANIMATIONS_INNER_PAGES = 'showAnimationsInnerPages';
// Sidebar Section States
export const KEY_SIDEBAR_STATE = 'sidebar_state';
export const KEY_APPS_COLLAPSED_CATEGORIES = 'apps_collapsedCategories';
// Homepage Order Settings
export const KEY_HOMEPAGE_SECTION_ORDER = 'homepage-section-order';
// --- Utility Functions ---
/**
* Safely gets and parses a value from localStorage.
* @param {string} key The localStorage key.
* @param {*} defaultValue The default value to return if the key doesn't exist or an error occurs.
* @returns {*} The parsed value or the default value.
*/
export const get = (key, defaultValue) => {
try {
const storedValue = localStorage.getItem(key);
return storedValue ? JSON.parse(storedValue) : defaultValue;
} catch (error) {
console.error(`Error reading '${key}' from localStorage`, error);
return defaultValue;
}
};
/**
* Safely sets a value in localStorage.
* @param {string} key The localStorage key.
* @param {*} value The value to be stringified and stored.
*/
export const set = (key, value) => {
try {
localStorage.setItem(key, JSON.stringify(value));
} catch (error) {
console.error(`Error writing '${key}' to localStorage`, error);
}
};
/**
* Safely removes a value from localStorage.
* @param {string} key The localStorage key to remove.
*/
export const remove = (key) => {
try {
localStorage.removeItem(key);
} catch (error) {
console.error(`Error removing '${key}' from localStorage`, error);
}
};