1
- import React , { useState } from 'react' ;
1
+ import React , { useState , useEffect } from 'react' ;
2
2
import { useStore } from '@nanostores/react' ;
3
3
import { providerStore , setProvider , type Provider } from '~/lib/stores/provider' ;
4
4
import '~/styles/index.scss' ;
5
5
6
6
export function ProviderSelector ( ) {
7
7
const currentProvider = useStore ( providerStore ) ;
8
8
const [ inputValue , setInputValue ] = useState ( currentProvider . model ) ;
9
+ const [ providers , setProviders ] = useState < string [ ] > ( [ ] ) ;
10
+ const [ apiKey , setApiKey ] = useState ( '' ) ; // New state for API key
11
+
12
+ useEffect ( ( ) => {
13
+ const fetchProviders = async ( ) => {
14
+ try {
15
+ const response = await fetch ( 'https://openrouter.ai/api/v1/models' ) ;
16
+ const data = await response . json ( ) ;
17
+ setProviders ( data . data . map ( a => a . id ) ) ;
18
+ } catch ( error ) {
19
+ console . error ( 'Error fetching providers:' , error ) ;
20
+ }
21
+ } ;
22
+
23
+ fetchProviders ( ) ;
24
+ } , [ ] ) ;
9
25
10
26
const handleProviderChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
11
27
const value = event . target . value ;
12
28
setInputValue ( value ) ;
13
29
} ;
14
30
15
- const providers = [
16
- 'Anthropic (Claude)' ,
17
- 'Together AI (meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo)' ,
18
- 'Together AI (meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo)' ,
19
- 'Together AI (mistralai/Mixtral-8x7B-Instruct-v0.1)' ,
20
- 'OpenRouter (openai/gpt-3.5-turbo)' ,
21
- 'OpenRouter (anthropic/claude-2)' ,
22
- 'OpenRouter (google/palm-2-chat-bison)' ,
23
- ] ;
31
+ const handleApiKeyButtonClick = ( ) => {
32
+ const value = prompt ( "Enter OpenRouter API Key" ) ; // Prompt user for API key
33
+ if ( value ) {
34
+ setApiKey ( value ) ; // Update API key state if a value is provided
35
+ }
36
+ } ;
24
37
25
38
return (
26
- < div className = "relative w-[250px]" >
39
+ < div className = "flex space-x-2" > { /* Changed to flex for horizontal layout */ }
40
+ { /* Button to set API key */ }
41
+ < button
42
+ onClick = { handleApiKeyButtonClick }
43
+ className = "w-[120px] px-3 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 focus:outline-none"
44
+ >
45
+ Set API Key
46
+ </ button >
27
47
< input
28
48
list = "providers"
29
49
value = { inputValue }
30
50
onChange = { handleProviderChange }
31
51
onMouseDown = { ( ) => {
32
52
setInputValue ( '' ) ; // Clear input on click
33
53
} }
34
- className = "w-full px-3 py-2 bg-transparent border border-white/20 rounded text-white hover:border-white/40 focus:border-white/60 outline-none"
54
+ className = "w-[300px] px-3 py-2 bg-transparent border border-white/20 rounded text-white hover:border-white/40 focus:border-white/60 outline-none" // Increased width for larger input
35
55
/>
36
56
< datalist id = "providers" >
37
57
{ providers . map ( ( provider ) => (
@@ -40,4 +60,4 @@ export function ProviderSelector() {
40
60
</ datalist >
41
61
</ div >
42
62
) ;
43
- }
63
+ }
0 commit comments