2
2
ModelByProvider ,
3
3
MuxRule ,
4
4
V1ListAllModelsForAllProvidersResponse ,
5
- } from " @/api/generated" ;
5
+ } from ' @/api/generated'
6
6
import {
7
7
DialogTrigger ,
8
8
Button ,
@@ -12,71 +12,73 @@ import {
12
12
Input ,
13
13
OptionRenderer ,
14
14
OptionsSchema ,
15
- } from " @stacklok/ui-kit" ;
16
- import { ChevronDown , SearchMd } from " @untitled-ui/icons-react" ;
17
- import { useState } from " react" ;
15
+ } from ' @stacklok/ui-kit'
16
+ import { ChevronDown , SearchMd } from ' @untitled-ui/icons-react'
17
+ import { useState } from ' react'
18
18
19
19
type Props = {
20
- rule : MuxRule & { id : string } ;
21
- isArchived : boolean ;
22
- models : V1ListAllModelsForAllProvidersResponse ;
20
+ rule : MuxRule & { id : string }
21
+ isArchived : boolean
22
+ models : V1ListAllModelsForAllProvidersResponse
23
23
onChange : ( {
24
24
model,
25
25
provider_id,
26
26
} : {
27
- model : string ;
28
- provider_id : string ;
29
- } ) => void ;
30
- } ;
27
+ model : string
28
+ provider_id : string
29
+ } ) => void
30
+ }
31
31
32
32
function groupModelsByProviderName (
33
- models : ModelByProvider [ ] ,
34
- ) : OptionsSchema < " listbox" , string > [ ] {
35
- return models . reduce < OptionsSchema < " listbox" , string > [ ] > (
33
+ models : ModelByProvider [ ]
34
+ ) : OptionsSchema < ' listbox' , string > [ ] {
35
+ return models . reduce < OptionsSchema < ' listbox' , string > [ ] > (
36
36
( groupedProviders , item ) => {
37
37
const providerData = groupedProviders . find (
38
- ( group ) => group . id === item . provider_name ,
39
- ) ;
38
+ ( group ) => group . id === item . provider_name
39
+ )
40
40
if ( ! providerData ) {
41
41
groupedProviders . push ( {
42
42
id : item . provider_name ,
43
43
items : [ ] ,
44
44
textValue : item . provider_name ,
45
- } ) ;
45
+ } )
46
46
}
47
47
48
- ( providerData ?. items ?? [ ] ) . push ( {
49
- id : item . name ,
48
+ ; ( providerData ?. items ?? [ ] ) . push ( {
49
+ id : ` ${ item . provider_id } _ ${ item . name } ` ,
50
50
textValue : item . name ,
51
- } ) ;
51
+ } )
52
52
53
- return groupedProviders ;
53
+ return groupedProviders
54
54
} ,
55
- [ ] ,
56
- ) ;
55
+ [ ]
56
+ )
57
57
}
58
58
59
59
function filterModels ( {
60
60
groupedModels,
61
61
searchItem,
62
62
} : {
63
- searchItem : string ;
64
- groupedModels : OptionsSchema < " listbox" , string > [ ] ;
63
+ searchItem : string
64
+ groupedModels : OptionsSchema < ' listbox' , string > [ ]
65
65
} ) {
66
- return groupedModels
66
+ const test = groupedModels
67
67
. map ( ( modelData ) => {
68
- if ( ! searchItem ) return modelData ;
68
+ if ( ! searchItem ) return modelData
69
69
const filteredModels = modelData . items ?. filter ( ( item ) => {
70
- return item . textValue . includes ( searchItem ) ;
71
- } ) ;
70
+ return item . textValue . includes ( searchItem )
71
+ } )
72
72
73
73
const data = {
74
74
...modelData ,
75
75
items : filteredModels ,
76
- } ;
77
- return data ;
76
+ }
77
+ return data
78
78
} )
79
- . filter ( ( item ) => ( item . items ? item . items . length > 0 : false ) ) ;
79
+ . filter ( ( item ) => ( item . items ? item . items . length > 0 : false ) )
80
+
81
+ return test
80
82
}
81
83
82
84
export function WorkspaceModelsDropdown ( {
@@ -85,27 +87,28 @@ export function WorkspaceModelsDropdown({
85
87
models = [ ] ,
86
88
onChange,
87
89
} : Props ) {
88
- const [ isOpen , setIsOpen ] = useState ( false ) ;
89
- const [ searchItem , setSearchItem ] = useState ( "" ) ;
90
- const groupedModels = groupModelsByProviderName ( models ) ;
91
- const currentProvider = models . find (
92
- ( p ) => p . provider_id === rule . provider_id ,
93
- ) ;
90
+ const [ isOpen , setIsOpen ] = useState ( false )
91
+ const [ searchItem , setSearchItem ] = useState ( '' )
92
+ const groupedModels = groupModelsByProviderName ( models )
93
+ console . log ( { groupedModels, rule : rule } )
94
+ const currentProvider = models . find ( ( p ) => p . provider_id === rule . provider_id )
94
95
const currentModel =
95
96
currentProvider && rule . model
96
97
? `${ currentProvider ?. provider_name } /${ rule . model } `
97
- : "" ;
98
+ : ''
99
+ const selectedKey = `${ rule . provider_id } _${ rule . model } `
98
100
99
101
return (
100
- < div className = "w-full flex " >
102
+ < div className = "flex w-full" >
101
103
< DialogTrigger isOpen = { isOpen } onOpenChange = { ( test ) => setIsOpen ( test ) } >
102
104
< Button
103
105
variant = "secondary"
104
106
isDisabled = { isArchived }
105
107
data-testid = "workspace-models-dropdown"
106
- className = "flex justify-between cursor-pointer bg-gray-25 border-gray-400 shadow-none font-normal w-full"
108
+ className = "flex w-full cursor-pointer justify-between border-gray-400 bg-gray-25
109
+ font-normal shadow-none"
107
110
>
108
- < span > { currentModel || " Select a model" } </ span >
111
+ < span > { currentModel || ' Select a model' } </ span >
109
112
< ChevronDown className = "shrink-0" />
110
113
</ Button >
111
114
@@ -119,22 +122,21 @@ export function WorkspaceModelsDropdown({
119
122
items = { filterModels ( { searchItem, groupedModels } ) }
120
123
selectionMode = "single"
121
124
selectionBehavior = "replace"
122
- selectedKeys = { rule . model ? [ rule . model ] : [ ] }
125
+ selectedKeys = { selectedKey ? [ selectedKey ] : [ ] }
123
126
onSelectionChange = { ( v ) => {
124
- if ( v === " all" ) {
125
- return ;
127
+ if ( v === ' all' ) {
128
+ return
126
129
}
127
- const selectedValue = v . values ( ) . next ( ) . value ;
128
- const providerId = models . find (
129
- ( item ) => item . name === selectedValue ,
130
- ) ?. provider_id ;
131
- if ( typeof selectedValue === "string" && providerId ) {
130
+ const selectedValue = v . values ( ) . next ( ) . value
131
+ if ( ! selectedValue && typeof selectedValue !== 'string' ) return
132
+ if ( typeof selectedValue === 'string' ) {
133
+ const [ provider_id , modelName ] = selectedValue . split ( '_' )
134
+ if ( ! provider_id || ! modelName ) return
132
135
onChange ( {
133
- model : selectedValue ,
134
- provider_id : providerId ,
135
- } ) ;
136
-
137
- setIsOpen ( false ) ;
136
+ model : modelName ,
137
+ provider_id,
138
+ } )
139
+ setIsOpen ( false )
138
140
}
139
141
} }
140
142
className = "-mx-1 mt-2 max-h-72 overflow-auto"
@@ -154,5 +156,5 @@ export function WorkspaceModelsDropdown({
154
156
</ Popover >
155
157
</ DialogTrigger >
156
158
</ div >
157
- ) ;
159
+ )
158
160
}
0 commit comments