Spaces:
Build error
Build error
File size: 1,711 Bytes
fab9847 | 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 53 54 55 56 57 58 59 60 | import { createContext, useContext, useState, useEffect } from 'react';
import { fetchBenchmarks, fetchResults, fetchEval, fetchNlg } from '../api';
const AppContext = createContext();
export function AppProvider({ children }) {
const [benchmarks, setBenchmarks] = useState([]);
const [currentBM, setCurrentBM] = useState('WTI');
const [months, setMonths] = useState([]);
const [currentMonth, setCurrentMonth] = useState(0);
const [evalData, setEvalData] = useState({});
const [nlgData, setNlgData] = useState({});
const [loading, setLoading] = useState(true);
// Load benchmarks on mount
useEffect(() => {
fetchBenchmarks().then(bms => {
setBenchmarks(bms);
if (bms.length > 0) setCurrentBM(bms[0]);
}).catch(() => setBenchmarks(['WTI']));
}, []);
// Load data when benchmark changes
useEffect(() => {
if (!currentBM) return;
setLoading(true);
Promise.all([
fetchResults(currentBM),
fetchEval(currentBM),
fetchNlg(currentBM),
]).then(([results, ev, nlg]) => {
setMonths(results);
setCurrentMonth(results.length - 1);
setEvalData(ev);
setNlgData(nlg);
setLoading(false);
}).catch(err => {
console.error('Failed to load data:', err);
setLoading(false);
});
}, [currentBM]);
const current = months[currentMonth] || {};
const prev = months[currentMonth - 1] || current;
return (
<AppContext.Provider value={{
benchmarks, currentBM, setCurrentBM,
months, currentMonth, setCurrentMonth,
current, prev, evalData, nlgData, loading,
}}>
{children}
</AppContext.Provider>
);
}
export function useApp() {
return useContext(AppContext);
}
|