File size: 1,148 Bytes
f0b240d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46cc63a
f0b240d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;
}