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, AllInputs, GetInputsFromSection, Sections } from "./settings"; export const actions = new ActionManager(); export const configs = new ConfigManager(); type EmptyListener = () => void; type BaseEvents = keyof Events; type Callback = K extends BaseEvents ? Events[K] | EmptyListener : EmptyListener; export function useAction(action: T, callback: ActionHandler, deps?: React.DependencyList) { useEffect(() => { actions.on(action, callback); return () => { actions.off(action, callback); }; }, deps ? [action, ...deps] : [action, callback]); return null; } export function useOnConfigChange(section: K, callback: ActionHandler<{ [L in keyof (K extends keyof Sections ? GetInputsFromSection : T)]?: (K extends keyof Sections ? GetInputsFromSection : T)[L] } | null>, deps?: React.DependencyList){ useEffect(() => { const onDataChanged = (data: any) => { callback(data?.[section] || null); }; configs.onChange(onDataChanged); onDataChanged(configs.data); return () => { configs.off(onDataChanged); } }, deps ? [section, ...deps] : [section, callback]) return null; } export function onGSI(event: T, callback: Callback, deps?: React.DependencyList){ useEffect(() => { GSI.on(event, callback); return () => { GSI.off(event, callback); } }, deps ? [event, ...deps] : [event, callback]) } const SettingsContext = createContext({} as AllInputs); export const SettingsProvider = ({ children }: { children: ReactNode }) => { const [ data, setData ] = useState(configs.data as AllInputs || null); 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]; }