@@ -4,78 +4,32 @@ import { Card, Button, Row, Col, Statistic, Divider, Alert, message } from 'antd
4
4
import { ClusterOutlined , SyncOutlined } from '@ant-design/icons' ;
5
5
import Title from 'antd/lib/typography/Title' ;
6
6
import { Environment } from '../types/environment.types' ;
7
+ import { useWorkspaces } from '../hooks/useWorkspaces' ;
8
+ import WorkspacesList from './WorkspacesList' ;
7
9
import { Workspace } from '../types/workspace.types' ;
8
- import { useEnvironmentWorkspaces } from '../hooks/useEnvironmentWorkspaces' ;
9
- import { useManagedWorkspaces } from '../hooks/enterprise/useManagedWorkspaces' ;
10
- import WorkspacesList from './WorkspacesList' ;
11
- import { connectManagedWorkspace , unconnectManagedWorkspace } from '../services/enterprise.service' ;
12
- import { getMergedWorkspaces } from '../utils/getMergedWorkspaces' ;
13
-
14
- interface WorkspaceStats {
15
- total : number ;
16
- managed : number ;
17
- unmanaged : number ;
18
- }
19
10
20
11
interface WorkspacesTabProps {
21
12
environment : Environment ;
22
13
}
23
14
24
15
const WorkspacesTab : React . FC < WorkspacesTabProps > = ( { environment } ) => {
25
- // Keep the existing hooks for now - we'll optimize these later
16
+ // Use the new hook that handles both regular and managed workspaces
26
17
const {
27
18
workspaces,
28
- loading : workspacesLoading ,
29
- error : workspacesError ,
30
- } = useEnvironmentWorkspaces ( environment ) ;
31
-
32
- const {
33
- managedWorkspaces,
34
- managedLoading,
35
- managedError,
36
- } = useManagedWorkspaces ( environment ) ;
37
-
38
- // Keep the merging logic for now - we'll optimize this later
39
- const [ mergedWorkspaces , setMergedWorkspaces ] = React . useState < Workspace [ ] > ( [ ] ) ;
40
- const [ workspaceStats , setWorkspaceStats ] = React . useState < WorkspaceStats > ( {
41
- total : 0 ,
42
- managed : 0 ,
43
- unmanaged : 0 ,
44
- } ) ;
45
-
46
- React . useEffect ( ( ) => {
47
- if ( workspaces && managedWorkspaces ) {
48
- const { merged, stats } = getMergedWorkspaces ( workspaces , managedWorkspaces ) ;
49
- setMergedWorkspaces ( merged ) ;
50
- setWorkspaceStats ( stats ) ;
51
- }
52
- } , [ workspaces , managedWorkspaces ] ) ;
19
+ stats,
20
+ loading,
21
+ error,
22
+ toggleManagedStatus
23
+ } = useWorkspaces ( environment ) ;
53
24
54
- const handleToggleManaged = async ( workspace : Workspace , checked : boolean ) => {
55
- try {
56
- if ( checked ) {
57
- await connectManagedWorkspace ( environment . environmentId , workspace . name , workspace . gid ! ) ;
58
- } else {
59
- await unconnectManagedWorkspace ( workspace . gid ! ) ;
60
- }
61
-
62
- // Optimistically update the local state
63
- const updatedList = mergedWorkspaces . map ( ( w ) =>
64
- w . id === workspace . id ? { ...w , managed : checked } : w
65
- ) ;
66
-
67
- const updatedManagedCount = updatedList . filter ( ( w ) => w . managed ) . length ;
68
-
69
- setMergedWorkspaces ( updatedList ) ;
70
- setWorkspaceStats ( {
71
- total : updatedList . length ,
72
- managed : updatedManagedCount ,
73
- unmanaged : updatedList . length - updatedManagedCount ,
74
- } ) ;
75
-
25
+ const handleToggleManaged = async ( workspace : Workspace , checked :boolean ) => {
26
+ const success = await toggleManagedStatus ( workspace , checked ) ;
27
+
28
+ if ( success ) {
76
29
message . success ( `${ workspace . name } is now ${ checked ? 'Managed' : 'Unmanaged' } ` ) ;
77
- } catch ( err ) {
30
+ } else {
78
31
message . error ( `Failed to toggle managed state for ${ workspace . name } ` ) ;
32
+ // Optionally refresh to ensure UI is in sync with backend
79
33
}
80
34
} ;
81
35
@@ -98,21 +52,21 @@ const WorkspacesTab: React.FC<WorkspacesTabProps> = ({ environment }) => {
98
52
< Col span = { 8 } >
99
53
< Statistic
100
54
title = "Total Workspaces"
101
- value = { workspaceStats . total }
55
+ value = { stats . total }
102
56
prefix = { < ClusterOutlined /> }
103
57
/>
104
58
</ Col >
105
59
< Col span = { 8 } >
106
60
< Statistic
107
61
title = "Managed Workspaces"
108
- value = { workspaceStats . managed }
62
+ value = { stats . managed }
109
63
prefix = { < ClusterOutlined /> }
110
64
/>
111
65
</ Col >
112
66
< Col span = { 8 } >
113
67
< Statistic
114
68
title = "Unmanaged Workspaces"
115
- value = { workspaceStats . unmanaged }
69
+ value = { stats . unmanaged }
116
70
prefix = { < ClusterOutlined /> }
117
71
/>
118
72
</ Col >
@@ -121,10 +75,10 @@ const WorkspacesTab: React.FC<WorkspacesTabProps> = ({ environment }) => {
121
75
< Divider style = { { margin : "16px 0" } } />
122
76
123
77
{ /* Show error if workspace loading failed */ }
124
- { workspacesError && (
78
+ { error && (
125
79
< Alert
126
80
message = "Error loading workspaces"
127
- description = { workspacesError }
81
+ description = { error }
128
82
type = "error"
129
83
showIcon
130
84
style = { { marginBottom : "16px" } }
@@ -133,7 +87,7 @@ const WorkspacesTab: React.FC<WorkspacesTabProps> = ({ environment }) => {
133
87
134
88
{ ( ! environment . environmentApikey ||
135
89
! environment . environmentApiServiceUrl ) &&
136
- ! workspacesError && (
90
+ ! error && (
137
91
< Alert
138
92
message = "Configuration Issue"
139
93
description = {
@@ -149,9 +103,9 @@ const WorkspacesTab: React.FC<WorkspacesTabProps> = ({ environment }) => {
149
103
150
104
{ /* Workspaces List */ }
151
105
< WorkspacesList
152
- workspaces = { mergedWorkspaces }
153
- loading = { workspacesLoading && ! workspacesError }
154
- error = { workspacesError }
106
+ workspaces = { workspaces }
107
+ loading = { loading && ! error }
108
+ error = { error }
155
109
environmentId = { environment . environmentId }
156
110
onToggleManaged = { handleToggleManaged }
157
111
/>
0 commit comments