1+ 'use client' ;
2+
3+ import { createContext , useContext , useEffect , useState } from 'react' ;
4+ import { getItem , indexedDBService , setItem } from '@/services/indexedDB' ;
5+
6+ interface ConfigContextType {
7+ apiKey : string ;
8+ baseUrl : string ;
9+ updateConfig : ( newConfig : Partial < { apiKey : string ; baseUrl : string } > ) => Promise < void > ;
10+ isLoading : boolean ;
11+ isDBReady : boolean ;
12+ }
13+
14+ const ConfigContext = createContext < ConfigContextType | undefined > ( undefined ) ;
15+
16+ export function ConfigProvider ( { children } : { children : React . ReactNode } ) {
17+ const [ apiKey , setApiKey ] = useState < string > ( '' ) ;
18+ const [ baseUrl , setBaseUrl ] = useState < string > ( '' ) ;
19+ const [ isLoading , setIsLoading ] = useState ( true ) ;
20+ const [ isDBReady , setIsDBReady ] = useState ( false ) ;
21+
22+ useEffect ( ( ) => {
23+ const initializeDB = async ( ) => {
24+ try {
25+ setIsLoading ( true ) ;
26+ await indexedDBService . init ( ) ;
27+ setIsDBReady ( true ) ;
28+
29+ // Now load config
30+ const cachedApiKey = await getItem ( 'apiKey' ) ;
31+ const cachedBaseUrl = await getItem ( 'baseUrl' ) ;
32+
33+ if ( cachedApiKey ) {
34+ console . log ( 'Cached API key found:' , cachedApiKey ) ;
35+ }
36+ if ( cachedBaseUrl ) {
37+ console . log ( 'Cached base URL found:' , cachedBaseUrl ) ;
38+ }
39+
40+ // If not in cache, use env variables
41+ const defaultApiKey = process . env . NEXT_PUBLIC_OPENAI_API_KEY || '' ;
42+ const defaultBaseUrl = process . env . NEXT_PUBLIC_OPENAI_API_BASE || '' ;
43+
44+ // Set the values
45+ setApiKey ( cachedApiKey || defaultApiKey ) ;
46+ setBaseUrl ( cachedBaseUrl || defaultBaseUrl ) ;
47+
48+ // If not in cache, save to cache
49+ if ( ! cachedApiKey ) {
50+ await setItem ( 'apiKey' , defaultApiKey ) ;
51+ }
52+ if ( ! cachedBaseUrl ) {
53+ await setItem ( 'baseUrl' , defaultBaseUrl ) ;
54+ }
55+
56+ } catch ( error ) {
57+ console . error ( 'Error initializing:' , error ) ;
58+ } finally {
59+ setIsLoading ( false ) ;
60+ }
61+ } ;
62+
63+ initializeDB ( ) ;
64+ } , [ ] ) ;
65+
66+ const updateConfig = async ( newConfig : Partial < { apiKey : string ; baseUrl : string } > ) => {
67+ try {
68+ if ( newConfig . apiKey !== undefined ) {
69+ await setItem ( 'apiKey' , newConfig . apiKey ) ;
70+ setApiKey ( newConfig . apiKey ) ;
71+ }
72+ if ( newConfig . baseUrl !== undefined ) {
73+ await setItem ( 'baseUrl' , newConfig . baseUrl ) ;
74+ setBaseUrl ( newConfig . baseUrl ) ;
75+ }
76+ } catch ( error ) {
77+ console . error ( 'Error updating config:' , error ) ;
78+ throw error ;
79+ }
80+ } ;
81+
82+ return (
83+ < ConfigContext . Provider value = { { apiKey, baseUrl, updateConfig, isLoading, isDBReady } } >
84+ { children }
85+ </ ConfigContext . Provider >
86+ ) ;
87+ }
88+
89+ export function useConfig ( ) {
90+ const context = useContext ( ConfigContext ) ;
91+ if ( context === undefined ) {
92+ throw new Error ( 'useConfig must be used within a ConfigProvider' ) ;
93+ }
94+ return context ;
95+ }
0 commit comments