Spaces:
Sleeping
Sleeping
Download src/utils/optionsContext.jsx from randome-games/proxy: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/spaces/randome-games/proxy/resolve/main/src/utils/optionsContext.jsx
- Command line
-
hf download hf://spaces/randome-games/proxy/src/utils/optionsContext.jsx
-
curl -L -o optionsContext.jsx https://huggingface.co/spaces/randome-games/proxy/resolve/main/src/utils/optionsContext.jsx
1.28 kB
| import { createContext, useContext, useState, useEffect, useCallback, useMemo } from 'react'; | |
| const OptionsContext = createContext(); | |
| const getStoredOptions = () => { | |
| try { | |
| return JSON.parse(localStorage.getItem('options') || '{}'); | |
| } catch { | |
| return {}; | |
| } | |
| }; | |
| export const OptionsProvider = ({ children }) => { | |
| const [options, setOptions] = useState(getStoredOptions); | |
| useEffect(() => { | |
| try { | |
| localStorage.setItem('options', JSON.stringify(options)); | |
| } catch {} | |
| }, [options]); | |
| const updateOption = useCallback((obj, immediate = true) => { | |
| if (!obj || typeof obj !== 'object') return; | |
| const current = getStoredOptions(); | |
| const updated = { ...current, ...obj }; | |
| try { | |
| localStorage.setItem('options', JSON.stringify(updated)); | |
| } catch {} | |
| if (immediate) { | |
| setOptions((prev) => ({ ...prev, ...obj })); | |
| } | |
| }, []); | |
| const contextValue = useMemo(() => ({ options, updateOption }), [options, updateOption]); | |
| return <OptionsContext.Provider value={contextValue}>{children}</OptionsContext.Provider>; | |
| }; | |
| export const useOptions = () => { | |
| const context = useContext(OptionsContext); | |
| if (!context) { | |
| throw new Error('useOptions must be used within an OptionsProvider'); | |
| } | |
| return context; | |
| }; | |