import React, { createContext, useContext, useState, useCallback, useRef, useMemo } from 'react'; export interface SettingsSection { id: string; label: string; } interface SettingsSectionsContextValue { sections: SettingsSection[]; activeSection: string; scrollToSection: (id: string) => void; scrollContainerRef: React.RefObject; setSections: (sections: SettingsSection[]) => void; setActiveSection: (id: string) => void; setScrollToSection: (fn: (id: string) => void) => void; } const SettingsSectionsContext = createContext(null); export function SettingsSectionsProvider({ children }: { children: React.ReactNode }) { const [sections, setSections] = useState([]); const [activeSection, setActiveSection] = useState(''); const [scrollFn, setScrollFn] = useState<((id: string) => void) | null>(null); const scrollContainerRef = useRef(null); const scrollToSection = useCallback((id: string) => { scrollFn?.(id); }, [scrollFn]); // setScrollToSection receives a function, so wrap in updater to avoid // React interpreting it as a state updater function const setScrollToSectionStable = useCallback((fn: (id: string) => void) => { setScrollFn(() => fn); }, []); const value = useMemo(() => ({ sections, activeSection, scrollToSection, scrollContainerRef, setSections, setActiveSection, setScrollToSection: setScrollToSectionStable, }), [sections, activeSection, scrollToSection, scrollContainerRef, setScrollToSectionStable]); return ( {children} ); } export function useSettingsSectionsContext() { return useContext(SettingsSectionsContext); }