diff --git a/src/API/contexts/actions.tsx b/src/API/contexts/actions.tsx index 85e6143..f936a3f 100644 --- a/src/API/contexts/actions.tsx +++ b/src/API/contexts/actions.tsx @@ -1,8 +1,8 @@ -import { useCallback, useEffect, useState } from "react"; +import { useEffect, useState, createContext, ReactNode, useContext } from "react"; import ActionManager, { ActionHandler, ConfigManager } from "./managers"; import { Events } from "csgogsi"; import { GSI } from "../HUD"; -import type { AllActions, GetInputsFromSection, Sections } from "./settings"; +import type { AllActions, AllInputs, GetInputsFromSection, Sections } from "./settings"; export const actions = new ActionManager(); export const configs = new ConfigManager(); @@ -50,13 +50,38 @@ export function onGSI(event: T, callback: Callback, dep }, deps ? [event, ...deps] : [event, callback]) } -export function useConfig(section: K){ - const [ data, setData ] = useState<{ [L in keyof (K extends keyof Sections ? GetInputsFromSection : T)]?: (K extends keyof Sections ? GetInputsFromSection : T)[L] } | null>(configs.data?.[section] || null); +const SettingsContext = createContext({} as AllInputs); +export const SettingsProvider = ({ children }: { children: ReactNode }) => { + const [ data, setData ] = useState(configs.data as AllInputs || null); - const onDataChanged = useCallback((sectionData: any) => { - setData(sectionData || null); - }, [section]); - useOnConfigChange(section, onDataChanged); - return data; + + useEffect(() => { + const onDataChanged = (data: any) => { + setData(data); + }; + + configs.onChange(onDataChanged); + onDataChanged(configs.data); + + return () => { + configs.off(onDataChanged); + } + }, []) + + return ( + + {children} + + ); +}; + +export function useConfig(section: K){ + const context = useContext(SettingsContext); + if (context === undefined) { + throw new Error('generic Hook must be used within a Generic Provider'); + } + return context?.[section]; } diff --git a/src/API/contexts/settings.ts b/src/API/contexts/settings.ts index 4f73be0..819423e 100644 --- a/src/API/contexts/settings.ts +++ b/src/API/contexts/settings.ts @@ -42,6 +42,8 @@ type ValueMapper = { [K in T[number] as K[ export type GetInputsFromSection = { [K in NonNeverKeys>]: ValueMapper[K]}; export type Sections = { [K in Settings[number] as K["name"]]: K["inputs"]} +export type AllInputs = { [K in keyof Sections]: GetInputsFromSection }; + type ActionValueMapper = { [K in T[number] as K["name"]]: K extends { type: "action" } ? K["values"][number]["name"] : never } type GetActionsFromSection = { [K in NonNeverKeys>]: ActionValueMapper[K]}; diff --git a/src/App.tsx b/src/App.tsx index 20ff282..e348380 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,7 @@ import { useEffect, useState } from 'react' import './App.css' import { CSGO } from 'csgogsi' -import { onGSI } from './API/contexts/actions' +import { SettingsProvider, onGSI } from './API/contexts/actions' import Layout from './HUD/Layout/Layout'; import './API/socket'; import { Match } from './API/types'; @@ -74,7 +74,9 @@ function App() { if (!game) return null; return ( - + + + ); }