|
| 1 | +import { |
| 2 | + createContext, |
| 3 | + createRef, |
| 4 | + useContext, |
| 5 | + useMemo, |
| 6 | + useRef, |
| 7 | + useState, |
| 8 | + useLayoutEffect, |
| 9 | + useEffect, |
| 10 | +} from "react"; |
| 11 | +import { |
| 12 | + getItemFromLocalStorage, |
| 13 | + MAINTENANCE_ENDING_BANNER_EXPANDED_KEY, |
| 14 | + setItemToLocalStorage, |
| 15 | +} from "@/utils"; |
| 16 | + |
| 17 | +interface MaintenanceEndingBannerContextType { |
| 18 | + ref: React.RefObject<HTMLDivElement>; |
| 19 | + height: number; |
| 20 | + isExpanded: boolean; |
| 21 | + toggleExpanded: () => void; |
| 22 | +} |
| 23 | + |
| 24 | +const MaintenanceEndingBannerContext = |
| 25 | + createContext<MaintenanceEndingBannerContextType>({ |
| 26 | + ref: createRef<HTMLDivElement>(), |
| 27 | + height: 0, |
| 28 | + isExpanded: true, |
| 29 | + toggleExpanded: () => {}, |
| 30 | + }); |
| 31 | + |
| 32 | +export const MaintenanceEndingBannerProvider = ({ |
| 33 | + children, |
| 34 | +}: React.PropsWithChildren) => { |
| 35 | + const ref = useRef<HTMLDivElement>(null); |
| 36 | + const [height, setHeight] = useState(0); |
| 37 | + const [isExpanded, setIsExpanded] = useState<boolean>(true); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + const storedValue = getItemFromLocalStorage( |
| 41 | + MAINTENANCE_ENDING_BANNER_EXPANDED_KEY, |
| 42 | + ); |
| 43 | + if (storedValue !== null) { |
| 44 | + setIsExpanded(JSON.parse(storedValue)); |
| 45 | + } |
| 46 | + }, []); |
| 47 | + |
| 48 | + const toggleExpanded = () => { |
| 49 | + setItemToLocalStorage(MAINTENANCE_ENDING_BANNER_EXPANDED_KEY, !isExpanded); |
| 50 | + setIsExpanded((prev) => !prev); |
| 51 | + }; |
| 52 | + |
| 53 | + useLayoutEffect(() => { |
| 54 | + let frameId: number | null = null; |
| 55 | + |
| 56 | + const updatePosition = () => { |
| 57 | + if (ref.current) { |
| 58 | + const rect = ref.current.getBoundingClientRect(); |
| 59 | + |
| 60 | + setHeight(rect.height); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + const throttledUpdate = () => { |
| 65 | + // Context skipping update - frame already queued |
| 66 | + if (frameId) return; |
| 67 | + |
| 68 | + frameId = requestAnimationFrame(() => { |
| 69 | + updatePosition(); |
| 70 | + frameId = null; |
| 71 | + }); |
| 72 | + }; |
| 73 | + |
| 74 | + updatePosition(); |
| 75 | + |
| 76 | + window.addEventListener("scroll", throttledUpdate, { passive: true }); |
| 77 | + window.addEventListener("resize", throttledUpdate); |
| 78 | + |
| 79 | + return () => { |
| 80 | + window.removeEventListener("scroll", throttledUpdate); |
| 81 | + window.removeEventListener("resize", throttledUpdate); |
| 82 | + |
| 83 | + if (frameId) { |
| 84 | + cancelAnimationFrame(frameId); |
| 85 | + } |
| 86 | + }; |
| 87 | + }, []); |
| 88 | + |
| 89 | + const value = useMemo( |
| 90 | + () => ({ |
| 91 | + ref, |
| 92 | + height, |
| 93 | + isExpanded, |
| 94 | + toggleExpanded, |
| 95 | + }), |
| 96 | + [ref, height, isExpanded, toggleExpanded], |
| 97 | + ); |
| 98 | + |
| 99 | + return ( |
| 100 | + <MaintenanceEndingBannerContext.Provider value={value}> |
| 101 | + {children} |
| 102 | + </MaintenanceEndingBannerContext.Provider> |
| 103 | + ); |
| 104 | +}; |
| 105 | + |
| 106 | +export const useMaintenanceEndingBannerContext = () => { |
| 107 | + const context = useContext(MaintenanceEndingBannerContext); |
| 108 | + if (!context) { |
| 109 | + throw new Error( |
| 110 | + "useMaintenanceEndingBannerContext must be used within a MaintenanceEndingBannerProvider", |
| 111 | + ); |
| 112 | + } |
| 113 | + return context; |
| 114 | +}; |
0 commit comments