1
- import React from "react" ;
1
+ import React , { useState } from "react" ;
2
2
import { useParams } from "react-router-dom" ;
3
3
import {
4
4
Spin ,
@@ -13,6 +13,7 @@ import {
13
13
Button ,
14
14
Statistic ,
15
15
Divider ,
16
+ message
16
17
} from "antd" ;
17
18
import {
18
19
ReloadOutlined ,
@@ -29,6 +30,8 @@ import { useEnvironmentWorkspaces } from "./hooks/useEnvironmentWorkspaces";
29
30
import { useEnvironmentUserGroups } from "./hooks/useEnvironmentUserGroups" ;
30
31
import { useManagedWorkspaces } from "./hooks/enterprise/useManagedWorkspaces" ;
31
32
import { getMergedWorkspaces } from "./utils/getMergedWorkspaces" ;
33
+ import { Workspace } from "./types/workspace.types" ;
34
+ import { connectManagedWorkspace , unconnectManagedWorkspace } from "./services/enterprise.service" ;
32
35
33
36
34
37
const { Title, Text } = Typography ;
@@ -38,6 +41,12 @@ const { TabPane } = Tabs;
38
41
* Environment Detail Page Component
39
42
* Shows detailed information about a specific environment
40
43
*/
44
+
45
+ type WorkspaceStats = {
46
+ total : number ;
47
+ managed : number ;
48
+ unmanaged : number ;
49
+ } ;
41
50
const EnvironmentDetail : React . FC = ( ) => {
42
51
// Get environment ID from URL params
43
52
const {
@@ -72,6 +81,22 @@ const EnvironmentDetail: React.FC = () => {
72
81
73
82
// Use the custom hook to handle data fetching and state management
74
83
// Use the custom hook to handle data fetching and state management
84
+
85
+ const [ mergedWorkspaces , setMergedWorkspaces ] = useState < Workspace [ ] > ( [ ] ) ;
86
+ const [ workspaceStats , setWorkspaceStats ] = useState < WorkspaceStats > ( {
87
+ total : 0 ,
88
+ managed : 0 ,
89
+ unmanaged : 0 ,
90
+ } ) ;
91
+
92
+
93
+ React . useEffect ( ( ) => {
94
+ if ( workspaces && managedWorkspaces ) {
95
+ const { merged, stats } = getMergedWorkspaces ( workspaces , managedWorkspaces ) ;
96
+ setMergedWorkspaces ( merged ) ;
97
+ setWorkspaceStats ( stats ) ;
98
+ }
99
+ } , [ workspaces , managedWorkspaces ] ) ;
75
100
76
101
// If loading, show spinner
77
102
if ( envLoading ) {
@@ -121,7 +146,39 @@ const EnvironmentDetail: React.FC = () => {
121
146
) ;
122
147
}
123
148
124
- const { merged, stats : workspaceStats } = getMergedWorkspaces ( workspaces , managedWorkspaces ) ;
149
+ const { merged, stats : initialStats } = getMergedWorkspaces ( workspaces , managedWorkspaces ) ;
150
+
151
+
152
+
153
+ const handleToggleManaged = async ( workspace : Workspace , checked : boolean ) => {
154
+ try {
155
+ console . log ( "WORKSPACE" , workspace ) ;
156
+ if ( checked ) {
157
+ await connectManagedWorkspace ( environment . environmentId , workspace . name , workspace . gid ! ) ;
158
+ } else {
159
+ await unconnectManagedWorkspace ( workspace . gid ! ) ;
160
+ }
161
+
162
+ // Optimistically update the local state
163
+ const updatedList = mergedWorkspaces . map ( ( w ) =>
164
+ w . id === workspace . id ? { ...w , managed : checked } : w
165
+ ) ;
166
+
167
+ const updatedManagedCount = updatedList . filter ( ( w ) => w . managed ) . length ;
168
+
169
+ setMergedWorkspaces ( updatedList ) ;
170
+ setWorkspaceStats ( {
171
+ total : updatedList . length ,
172
+ managed : updatedManagedCount ,
173
+ unmanaged : updatedList . length - updatedManagedCount ,
174
+ } ) ;
175
+
176
+ message . success ( `${ workspace . name } is now ${ checked ? 'Managed' : 'Unmanaged' } ` ) ;
177
+ } catch ( err ) {
178
+ message . error ( `Failed to toggle managed state for ${ workspace . name } ` ) ;
179
+ }
180
+ } ;
181
+
125
182
126
183
return (
127
184
< div className = "environment-detail-container" style = { { padding : "24px" } } >
@@ -297,10 +354,11 @@ const EnvironmentDetail: React.FC = () => {
297
354
298
355
{ /* Workspaces List */ }
299
356
< WorkspacesList
300
- workspaces = { merged }
357
+ workspaces = { mergedWorkspaces } // ⬅️ Use local state!
301
358
loading = { workspacesLoading && ! workspacesError }
302
359
error = { workspacesError }
303
360
environmentId = { environment . environmentId }
361
+ onToggleManaged = { handleToggleManaged } // ⬅️ Add this to enable toggles
304
362
/>
305
363
</ Card >
306
364
</ TabPane >
0 commit comments