@@ -3,69 +3,24 @@ import { Button, Dialog, Icon, Typography } from "@equinor/eds-core-react";
33import { ModelConfig } from "@/dto/FormConfig" ;
44import { useAvailableModels } from "@/contexts/ModelContext" ;
55import { help } from "@equinor/eds-icons" ;
6+ import { sortModelsByCategory } from "@/utils/modelUtils" ;
67
7- const makeLabel = ( modelConfig : ModelConfig ) =>
8- modelConfig . accessError ? `{modelConfig.displayName} (Access Error)` : modelConfig . displayName ;
9-
10- const handleToggle = (
11- isActive : boolean ,
12- setModel : ( modelConfig : ModelConfig | undefined ) => void ,
13- modelConfig : ModelConfig
14- ) => {
15- if ( isActive ) {
16- // If currently active, unselect it
17- setModel ( undefined ) ;
18- } else {
19- // If not active, select it
20- setModel ( modelConfig ) ;
21- }
22- } ;
23-
24- const PrimaryModelButton : React . FC < {
25- modelConfig : ModelConfig ;
26- active : boolean ;
27- setCurrentPrimaryModel : ( modelConfig : ModelConfig | undefined ) => void ;
28- } > = ( { modelConfig, active, setCurrentPrimaryModel } ) => {
29- const [ open , setOpen ] = useState < boolean > ( false ) ;
30-
31- return (
32- < Button . Group >
33- < Button
34- variant = { active ? "outlined" : "contained" }
35- onClick = { ( ) => {
36- handleToggle ( active , setCurrentPrimaryModel , modelConfig ) ;
37- } }
38- disabled = { ! ! modelConfig . accessError }
39- >
40- { makeLabel ( modelConfig ) }
41- </ Button >
42- < Button variant = { "contained" } onClick = { ( ) => setOpen ( true ) } >
43- < Icon data = { help } />
44- </ Button >
45- < Dialog open = { open } onClose = { ( ) => setOpen ( false ) } isDismissable = { true } style = { { width : "100%" } } >
46- < Dialog . Header >
47- < Dialog . Title > Model { modelConfig . displayName } </ Dialog . Title >
48- </ Dialog . Header >
49- < Dialog . Content style = { { whiteSpace : "pre-wrap" } } > { modelConfig . description } </ Dialog . Content >
50- </ Dialog >
51- </ Button . Group >
52- ) ;
8+ const makeLabel = ( modelConfig : ModelConfig ) => {
9+ return modelConfig . accessError ? `${ modelConfig . displayName } (Access Error)` : modelConfig . displayName ;
5310} ;
5411
55- const SecondaryModelButton : React . FC < {
12+ const ModelButton : React . FC < {
5613 modelConfig : ModelConfig ;
57- active : boolean ;
58- setCurrentSecondaryModel : ( modelConfig : ModelConfig | undefined ) => void ;
59- } > = ( { modelConfig, active , setCurrentSecondaryModel } ) => {
14+ isActive : boolean ;
15+ onToggle : ( ) => void ;
16+ } > = ( { modelConfig, isActive , onToggle } ) => {
6017 const [ open , setOpen ] = useState < boolean > ( false ) ;
6118
6219 return (
6320 < Button . Group >
6421 < Button
65- variant = { active ? "outlined" : "contained" }
66- onClick = { ( ) => {
67- handleToggle ( active , setCurrentSecondaryModel , modelConfig ) ;
68- } }
22+ variant = { isActive ? "outlined" : "contained" }
23+ onClick = { onToggle }
6924 disabled = { ! ! modelConfig . accessError }
7025 >
7126 { makeLabel ( modelConfig ) }
@@ -84,13 +39,23 @@ const SecondaryModelButton: React.FC<{
8439} ;
8540
8641const ModelSelect : React . FC < {
87- currentPrimaryModel ?: ModelConfig ;
88- setCurrentPrimaryModel : ( model : ModelConfig | undefined ) => void ;
89- currentSecondaryModel ?: ModelConfig ;
90- setCurrentSecondaryModel : ( model : ModelConfig | undefined ) => void ;
91- } > = ( { currentPrimaryModel, currentSecondaryModel, setCurrentPrimaryModel, setCurrentSecondaryModel } ) => {
42+ selectedModels : ModelConfig [ ] ;
43+ setSelectedModels : ( models : ModelConfig [ ] ) => void ;
44+ } > = ( { selectedModels, setSelectedModels } ) => {
9245 const { models, error, isLoading } = useAvailableModels ( ) ;
9346
47+ const handleToggle = ( modelConfig : ModelConfig ) => {
48+ const index = selectedModels . findIndex ( ( m ) => m . modelId === modelConfig . modelId ) ;
49+ if ( index !== - 1 ) {
50+ // Deselect the model
51+ setSelectedModels ( selectedModels . filter ( ( _ , i ) => i !== index ) ) ;
52+ } else {
53+ // Replace any existing model of the same category and maintain correct order
54+ const filteredModels = selectedModels . filter ( ( m ) => m . category !== modelConfig . category ) ;
55+ setSelectedModels ( sortModelsByCategory ( [ ...filteredModels , modelConfig ] ) ) ;
56+ }
57+ } ;
58+
9459 if ( isLoading ) {
9560 return (
9661 < Typography variant = "body_short" bold >
@@ -117,29 +82,35 @@ const ModelSelect: React.FC<{
11782 < div style = { { display : "flex" , gap : "16px" , flexWrap : "wrap" } } >
11883 { models
11984 . filter ( ( model ) => model . category === "Primary" )
120- . map ( ( model , index ) => (
121- < PrimaryModelButton
122- modelConfig = { model }
123- key = { index }
124- active = { model . modelId === currentPrimaryModel ?. modelId }
125- setCurrentPrimaryModel = { setCurrentPrimaryModel }
126- />
127- ) ) }
85+ . map ( ( model , index ) => {
86+ const isActive = selectedModels . some ( ( m ) => m . modelId === model . modelId ) ;
87+ return (
88+ < ModelButton
89+ modelConfig = { model }
90+ key = { index }
91+ isActive = { isActive }
92+ onToggle = { ( ) => handleToggle ( model ) }
93+ />
94+ ) ;
95+ } ) }
12896 </ div >
12997 < Typography variant = "h6" style = { { margin : "24px 0 8px 0" } } >
13098 Secondary Models
13199 </ Typography >
132100 < div style = { { display : "flex" , gap : "16px" , flexWrap : "wrap" } } >
133101 { models
134102 . filter ( ( model ) => model . category === "Secondary" )
135- . map ( ( model , index ) => (
136- < SecondaryModelButton
137- modelConfig = { model }
138- key = { index }
139- active = { model . modelId === currentSecondaryModel ?. modelId }
140- setCurrentSecondaryModel = { setCurrentSecondaryModel }
141- />
142- ) ) }
103+ . map ( ( model , index ) => {
104+ const isActive = selectedModels . some ( ( m ) => m . modelId === model . modelId ) ;
105+ return (
106+ < ModelButton
107+ modelConfig = { model }
108+ key = { index }
109+ isActive = { isActive }
110+ onToggle = { ( ) => handleToggle ( model ) }
111+ />
112+ ) ;
113+ } ) }
143114 </ div >
144115 </ div >
145116 </ >
0 commit comments