SignalMod / frontend /src /context /AppContext.tsx
Mirae Kang
feat: implement new models and improve UI, #23
46cc63a
raw
history blame
1.15 kB
import {
createContext,
useCallback,
useContext,
useMemo,
useState,
type ReactNode,
} from "react";
export type HubEntry = {
user: string;
snippet: string;
score: number;
action: string;
};
type AppContextValue = {
threshold: number;
setThreshold: (v: number) => void;
hubHistory: HubEntry[];
addHubEntry: (entry: HubEntry) => void;
};
const AppContext = createContext<AppContextValue | null>(null);
export function AppProvider({ children }: { children: ReactNode }) {
const [threshold, setThreshold] = useState(0.381);
const [hubHistory, setHubHistory] = useState<HubEntry[]>([]);
const addHubEntry = useCallback((entry: HubEntry) => {
setHubHistory((prev) => [entry, ...prev].slice(0, 100));
}, []);
const value = useMemo(
() => ({
threshold,
setThreshold,
hubHistory,
addHubEntry,
}),
[threshold, hubHistory, addHubEntry]
);
return <AppContext.Provider value={value}>{children}</AppContext.Provider>;
}
export function useApp() {
const ctx = useContext(AppContext);
if (!ctx) throw new Error("useApp must be used within AppProvider");
return ctx;
}