|
1 | | -import React, { createContext, useContext, useState, useEffect } from 'react'; |
| 1 | +import React, { createContext, useContext } from 'react'; |
| 2 | +import usePersistentState from '../hooks/usePersistentState'; |
| 3 | +import { |
| 4 | + KEY_IS_ANIMATION_ENABLED, |
| 5 | + KEY_SHOW_ANIMATIONS_HOMEPAGE, |
| 6 | + KEY_SHOW_ANIMATIONS_INNER_PAGES, |
| 7 | +} from '../utils/LocalStorageManager'; |
2 | 8 |
|
3 | 9 | const AnimationContext = createContext(); |
4 | 10 |
|
5 | 11 | export const AnimationProvider = ({ children }) => { |
6 | | - // Initialize state from localStorage, or default to true |
7 | | - const [isAnimationEnabled, setIsAnimationEnabled] = useState(() => { |
8 | | - try { |
9 | | - const storedValue = localStorage.getItem('isAnimationEnabled'); |
10 | | - return storedValue ? JSON.parse(storedValue) : true; |
11 | | - } catch (error) { |
12 | | - console.error( |
13 | | - "Error reading 'isAnimationEnabled' from localStorage", |
14 | | - error, |
15 | | - ); |
16 | | - return true; // Default to true if localStorage is not accessible |
17 | | - } |
18 | | - }); |
19 | | - |
20 | | - const [showAnimationsHomepage, setShowAnimationsHomepage] = useState(() => { |
21 | | - try { |
22 | | - const storedValue = localStorage.getItem('showAnimationsHomepage'); |
23 | | - return storedValue ? JSON.parse(storedValue) : true; // Default to true |
24 | | - } catch (error) { |
25 | | - console.error( |
26 | | - "Error reading 'showAnimationsHomepage' from localStorage", |
27 | | - error, |
28 | | - ); |
29 | | - return true; // Default to true if localStorage is not accessible |
30 | | - } |
31 | | - }); |
32 | | - |
33 | | - const [showAnimationsInnerPages, setShowAnimationsInnerPages] = useState( |
34 | | - () => { |
35 | | - try { |
36 | | - const storedValue = localStorage.getItem('showAnimationsInnerPages'); |
37 | | - return storedValue ? JSON.parse(storedValue) : false; // Default to false |
38 | | - } catch (error) { |
39 | | - console.error( |
40 | | - "Error reading 'showAnimationsInnerPages' from localStorage", |
41 | | - error, |
42 | | - ); |
43 | | - return false; // Default to false if localStorage is not accessible |
44 | | - } |
45 | | - }, |
| 12 | + const [isAnimationEnabled, setIsAnimationEnabled] = usePersistentState( |
| 13 | + KEY_IS_ANIMATION_ENABLED, |
| 14 | + true, |
46 | 15 | ); |
47 | 16 |
|
48 | | - // Update localStorage whenever isAnimationEnabled changes |
49 | | - useEffect(() => { |
50 | | - try { |
51 | | - localStorage.setItem( |
52 | | - 'isAnimationEnabled', |
53 | | - JSON.stringify(isAnimationEnabled), |
54 | | - ); |
55 | | - } catch (error) { |
56 | | - console.error( |
57 | | - "Error writing 'isAnimationEnabled' to localStorage", |
58 | | - error, |
59 | | - ); |
60 | | - } |
61 | | - }, [isAnimationEnabled]); |
62 | | - |
63 | | - // Update localStorage whenever showAnimationsHomepage changes |
64 | | - useEffect(() => { |
65 | | - try { |
66 | | - localStorage.setItem( |
67 | | - 'showAnimationsHomepage', |
68 | | - JSON.stringify(showAnimationsHomepage), |
69 | | - ); |
70 | | - } catch (error) { |
71 | | - console.error( |
72 | | - "Error writing 'showAnimationsHomepage' to localStorage", |
73 | | - error, |
74 | | - ); |
75 | | - } |
76 | | - }, [showAnimationsHomepage]); |
| 17 | + const [showAnimationsHomepage, setShowAnimationsHomepage] = |
| 18 | + usePersistentState(KEY_SHOW_ANIMATIONS_HOMEPAGE, true); |
77 | 19 |
|
78 | | - // Update localStorage whenever showAnimationsInnerPages changes |
79 | | - useEffect(() => { |
80 | | - try { |
81 | | - localStorage.setItem( |
82 | | - 'showAnimationsInnerPages', |
83 | | - JSON.stringify(showAnimationsInnerPages), |
84 | | - ); |
85 | | - } catch (error) { |
86 | | - console.error( |
87 | | - "Error writing 'showAnimationsInnerPages' to localStorage", |
88 | | - error, |
89 | | - ); |
90 | | - } |
91 | | - }, [showAnimationsInnerPages]); |
| 20 | + const [showAnimationsInnerPages, setShowAnimationsInnerPages] = |
| 21 | + usePersistentState(KEY_SHOW_ANIMATIONS_INNER_PAGES, false); |
92 | 22 |
|
93 | 23 | const toggleAnimation = () => { |
94 | 24 | setIsAnimationEnabled((prev) => !prev); |
|
0 commit comments