@@ -88,13 +88,65 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
8888 ref ,
8989 ) => {
9090 const TEXTAREA_MAX_HEIGHT = chatStarted ? 400 : 200 ;
91- const [ apiKeys , setApiKeys ] = useState < Record < string , string > > ( { } ) ;
91+ const [ apiKeys , setApiKeys ] = useState < Record < string , string > > ( ( ) => {
92+ const savedKeys = Cookies . get ( 'apiKeys' ) ;
93+
94+ if ( savedKeys ) {
95+ try {
96+ return JSON . parse ( savedKeys ) ;
97+ } catch ( error ) {
98+ console . error ( 'Failed to parse API keys from cookies:' , error ) ;
99+ return { } ;
100+ }
101+ }
102+
103+ return { } ;
104+ } ) ;
92105 const [ modelList , setModelList ] = useState ( MODEL_LIST ) ;
93106 const [ isModelSettingsCollapsed , setIsModelSettingsCollapsed ] = useState ( false ) ;
94107 const [ isListening , setIsListening ] = useState ( false ) ;
95108 const [ recognition , setRecognition ] = useState < SpeechRecognition | null > ( null ) ;
96109 const [ transcript , setTranscript ] = useState ( '' ) ;
97110
111+ // Load enabled providers from cookies
112+ const [ enabledProviders , setEnabledProviders ] = useState ( ( ) => {
113+ const savedProviders = Cookies . get ( 'providers' ) ;
114+
115+ if ( savedProviders ) {
116+ try {
117+ const parsedProviders = JSON . parse ( savedProviders ) ;
118+ return PROVIDER_LIST . filter ( ( p ) => parsedProviders [ p . name ] ) ;
119+ } catch ( error ) {
120+ console . error ( 'Failed to parse providers from cookies:' , error ) ;
121+ return PROVIDER_LIST ;
122+ }
123+ }
124+
125+ return PROVIDER_LIST ;
126+ } ) ;
127+
128+ // Update enabled providers when cookies change
129+ useEffect ( ( ) => {
130+ const updateProvidersFromCookies = ( ) => {
131+ const savedProviders = Cookies . get ( 'providers' ) ;
132+
133+ if ( savedProviders ) {
134+ try {
135+ const parsedProviders = JSON . parse ( savedProviders ) ;
136+ setEnabledProviders ( PROVIDER_LIST . filter ( ( p ) => parsedProviders [ p . name ] ) ) ;
137+ } catch ( error ) {
138+ console . error ( 'Failed to parse providers from cookies:' , error ) ;
139+ }
140+ }
141+ } ;
142+
143+ updateProvidersFromCookies ( ) ;
144+
145+ const interval = setInterval ( updateProvidersFromCookies , 1000 ) ;
146+
147+ return ( ) => clearInterval ( interval ) ;
148+ } , [ PROVIDER_LIST ] ) ;
149+
98150 console . log ( transcript ) ;
99151 useEffect ( ( ) => {
100152 // Load API keys from cookies on component mount
@@ -184,23 +236,6 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
184236 }
185237 } ;
186238
187- const updateApiKey = ( provider : string , key : string ) => {
188- try {
189- const updatedApiKeys = { ...apiKeys , [ provider ] : key } ;
190- setApiKeys ( updatedApiKeys ) ;
191-
192- // Save updated API keys to cookies with 30 day expiry and secure settings
193- Cookies . set ( 'apiKeys' , JSON . stringify ( updatedApiKeys ) , {
194- expires : 30 , // 30 days
195- secure : true , // Only send over HTTPS
196- sameSite : 'strict' , // Protect against CSRF
197- path : '/' , // Accessible across the site
198- } ) ;
199- } catch ( error ) {
200- console . error ( 'Error saving API keys to cookies:' , error ) ;
201- }
202- } ;
203-
204239 const handleFileUpload = ( ) => {
205240 const input = document . createElement ( 'input' ) ;
206241 input . type = 'file' ;
@@ -360,11 +395,15 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
360395 providerList = { PROVIDER_LIST }
361396 apiKeys = { apiKeys }
362397 />
363- { provider && (
398+ { enabledProviders . length > 0 && provider && (
364399 < APIKeyManager
365400 provider = { provider }
366401 apiKey = { apiKeys [ provider . name ] || '' }
367- setApiKey = { ( key ) => updateApiKey ( provider . name , key ) }
402+ setApiKey = { ( key ) => {
403+ const newApiKeys = { ...apiKeys , [ provider . name ] : key } ;
404+ setApiKeys ( newApiKeys ) ;
405+ Cookies . set ( 'apiKeys' , JSON . stringify ( newApiKeys ) ) ;
406+ } }
368407 />
369408 ) }
370409 </ div >
0 commit comments