Skip to content

Commit 6750980

Browse files
committed
add managed workspace in the workspace context
1 parent 7440ba3 commit 6750980

File tree

2 files changed

+114
-128
lines changed

2 files changed

+114
-128
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import React, { useEffect, useState } from "react";
2-
import { useParams, useHistory } from "react-router-dom";
1+
import React, { useState } from "react";
32
import history from "@lowcoder-ee/util/history";
43
import {
54
Spin,
65
Typography,
76
Card,
8-
Row,
9-
Col,
107
Tabs,
11-
Alert,
128
Button,
139
Breadcrumb,
1410
Space,
@@ -26,101 +22,77 @@ import {
2622
ArrowLeftOutlined,
2723
CloudUploadOutlined
2824
} from "@ant-design/icons";
29-
import { useEnvironmentContext } from "./context/EnvironmentContext";
25+
26+
// Use the context hooks
27+
import { useSingleEnvironmentContext } from "./context/SingleEnvironmentContext";
28+
import { useWorkspaceContext } from "./context/WorkspaceContext";
29+
import { useDeployModal } from "./context/DeployModalContext";
30+
3031
import DeployableItemsTab from "./components/DeployableItemsTab";
32+
import { workspaceConfig } from "./config/workspace.config";
3133
import { appsConfig } from "./config/apps.config";
3234
import { dataSourcesConfig } from "./config/data-sources.config";
3335
import { queryConfig } from "./config/query.config";
34-
import { useDeployableItems } from "./hooks/useDeployableItems";
35-
import { workspaceConfig } from "./config/workspace.config";
36-
import { useDeployModal } from "./context/DeployModalContext";
37-
import { useSingleEnvironmentContext } from "./context/SingleEnvironmentContext";
36+
3837
const { Title, Text } = Typography;
3938
const { TabPane } = Tabs;
4039

41-
42-
4340
const WorkspaceDetail: React.FC = () => {
41+
// Use the context hooks
42+
const { environment } = useSingleEnvironmentContext();
43+
const { workspace, isLoading, error, toggleManagedStatus } = useWorkspaceContext();
44+
const { openDeployModal } = useDeployModal();
4445

45-
// Get parameters from URL
46-
const { environmentId,workspaceId } = useParams<{
47-
workspaceId: string;
48-
environmentId: string;
49-
}>();
50-
const {
51-
environment,
52-
isLoading: envLoading,
53-
error: envError,
54-
} = useSingleEnvironmentContext();
55-
56-
const {openDeployModal} = useDeployModal();
57-
58-
// Use our generic hook with the workspace config
59-
const {
60-
items: workspaces,
61-
stats: workspaceStats,
62-
loading: workspaceLoading,
63-
error : workspaceError,
64-
toggleManagedStatus,
65-
refreshItems
66-
} = useDeployableItems(
67-
workspaceConfig,
68-
environment,
69-
{ workspaceId } // Additional params if needed
70-
);
71-
72-
// Find the current workspace in the items array
73-
const workspace = workspaces.find(w => w.id === workspaceId);
46+
const [isToggling, setIsToggling] = useState(false);
7447

48+
// Handle toggle managed status
7549
const handleToggleManaged = async (checked: boolean) => {
7650
if (!workspace) return;
7751

78-
const success = await toggleManagedStatus(workspace, checked);
79-
if (success) {
80-
message.success(`Workspace is now ${checked ? 'Managed' : 'Unmanaged'}`);
81-
} else {
82-
message.error('Failed to change managed status');
52+
setIsToggling(true);
53+
try {
54+
const success = await toggleManagedStatus(checked);
55+
if (success) {
56+
message.success(`Workspace is now ${checked ? 'Managed' : 'Unmanaged'}`);
57+
} else {
58+
message.error('Failed to change managed status');
59+
}
60+
} finally {
61+
setIsToggling(false);
8362
}
8463
};
8564

86-
if (envLoading || workspaceLoading ) {
87-
return (
88-
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100%', padding: '50px' }}>
89-
<Spin size="large" tip="Loading workspace details..." />
90-
</div>
91-
);
92-
}
65+
if (isLoading) {
66+
return (
67+
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100%', padding: '50px' }}>
68+
<Spin size="large" tip="Loading workspace details..." />
69+
</div>
70+
);
71+
}
9372

94-
if (!environment || !workspace) {
95-
return (
96-
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100%', padding: '50px' }}>
97-
<Typography.Title level={3}>Workspace not found</Typography.Title>
98-
</div>
99-
)
100-
}
73+
if (error || !environment || !workspace) {
74+
return (
75+
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100%', padding: '50px' }}>
76+
<Typography.Title level={3}>
77+
{error || "Workspace not found"}
78+
</Typography.Title>
79+
</div>
80+
);
81+
}
10182

102-
10383
return (
104-
<div
105-
className="workspace-detail-container"
106-
style={{ padding: "24px", flex: 1 }}
107-
>
84+
<div className="workspace-detail-container" style={{ padding: "24px", flex: 1 }}>
10885
{/* Breadcrumb navigation */}
10986
<Breadcrumb style={{ marginBottom: "16px" }}>
11087
<Breadcrumb.Item>
111-
<span
112-
style={{ cursor: "pointer" }}
113-
onClick={() => history.push("/setting/environments")}
114-
>
88+
<span style={{ cursor: "pointer" }} onClick={() => history.push("/setting/environments")}>
11589
<HomeOutlined /> Environments
11690
</span>
11791
</Breadcrumb.Item>
11892
<Breadcrumb.Item>
11993
<span
12094
style={{ cursor: "pointer" }}
121-
onClick={() =>
122-
history.push(`/setting/environments/${environmentId}`)
123-
}
95+
onClick={() => history.push(`/setting/environments/${environment.environmentId}`)}
12496
>
12597
<TeamOutlined /> {environment.environmentName}
12698
</span>
@@ -129,29 +101,14 @@ const WorkspaceDetail: React.FC = () => {
129101
</Breadcrumb>
130102

131103
{/* Workspace header with details and actions */}
132-
<Card
133-
style={{ marginBottom: "24px" }}
134-
bodyStyle={{ padding: "16px 24px" }}
135-
>
136-
<div
137-
style={{
138-
display: "flex",
139-
justifyContent: "space-between",
140-
alignItems: "center",
141-
}}
142-
>
104+
<Card style={{ marginBottom: "24px" }} bodyStyle={{ padding: "16px 24px" }}>
105+
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
143106
{/* Left section - Workspace info */}
144107
<div>
145108
<Title level={3} style={{ margin: 0 }}>
146109
{workspace.name}
147110
</Title>
148-
<div
149-
style={{
150-
display: "flex",
151-
alignItems: "center",
152-
marginTop: "8px",
153-
}}
154-
>
111+
<div style={{ display: "flex", alignItems: "center", marginTop: "8px" }}>
155112
<Text type="secondary" style={{ marginRight: "16px" }}>
156113
ID: {workspace.id}
157114
</Text>
@@ -166,8 +123,9 @@ const WorkspaceDetail: React.FC = () => {
166123
<div style={{ display: "flex", alignItems: "center" }}>
167124
<Text style={{ marginRight: "8px" }}>Managed:</Text>
168125
<Switch
169-
checked={workspace.managed}
126+
checked={!!workspace.managed}
170127
onChange={handleToggleManaged}
128+
loading={isToggling}
171129
checkedChildren="Yes"
172130
unCheckedChildren="No"
173131
/>
@@ -192,9 +150,7 @@ const WorkspaceDetail: React.FC = () => {
192150
</Tooltip>
193151
<Button
194152
icon={<ArrowLeftOutlined />}
195-
onClick={() =>
196-
history.push(`/setting/environments/${environmentId}`)
197-
}
153+
onClick={() => history.push(`/setting/environments/${environment.environmentId}`)}
198154
>
199155
Back
200156
</Button>
@@ -204,58 +160,35 @@ const WorkspaceDetail: React.FC = () => {
204160

205161
{/* Tabs for Apps, Data Sources, and Queries */}
206162
<Tabs defaultActiveKey="apps">
207-
<TabPane
208-
tab={
209-
<span>
210-
<AppstoreOutlined /> Apps
211-
</span>
212-
}
213-
key="apps"
214-
>
163+
<TabPane tab={<span><AppstoreOutlined /> Apps</span>} key="apps">
215164
<DeployableItemsTab
216165
environment={environment}
217166
config={appsConfig}
218-
additionalParams={{ workspaceId }}
167+
additionalParams={{ workspaceId: workspace.id }}
219168
title="Apps in this Workspace"
220169
/>
221170
</TabPane>
222171

223-
{/* Update the TabPane in WorkspaceDetail.tsx */}
224-
<TabPane
225-
tab={
226-
<span>
227-
<DatabaseOutlined /> Data Sources
228-
</span>
229-
}
230-
key="dataSources"
231-
>
172+
<TabPane tab={<span><DatabaseOutlined /> Data Sources</span>} key="dataSources">
232173
<DeployableItemsTab
233174
environment={environment}
234175
config={dataSourcesConfig}
235-
additionalParams={{ workspaceId }}
176+
additionalParams={{ workspaceId: workspace.id }}
236177
title="Data Sources in this Workspace"
237178
/>
238179
</TabPane>
239180

240-
<TabPane
241-
tab={
242-
<span>
243-
<CodeOutlined /> Queries
244-
</span>
245-
}
246-
key="queries"
247-
>
181+
<TabPane tab={<span><CodeOutlined /> Queries</span>} key="queries">
248182
<DeployableItemsTab
249183
environment={environment}
250184
config={queryConfig}
251-
additionalParams={{ workspaceId }}
185+
additionalParams={{ workspaceId: workspace.id }}
252186
title="Queries in this Workspace"
253187
/>
254188
</TabPane>
255189
</Tabs>
256190
</div>
257191
);
258-
}
259-
192+
};
260193

261-
export default WorkspaceDetail
194+
export default WorkspaceDetail;

client/packages/lowcoder/src/pages/setting/environments/context/WorkspaceContext.tsx

+56-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import React, {
1010
import { message } from "antd";
1111
import { useParams } from "react-router-dom";
1212
import { useSingleEnvironmentContext } from "./SingleEnvironmentContext";
13-
import { fetchWorkspaceById} from "../services/environments.service";
13+
import { fetchWorkspaceById } from "../services/environments.service";
1414
import { Workspace } from "../types/workspace.types";
15+
import { getManagedWorkspaces, connectManagedWorkspace, unconnectManagedWorkspace } from "../services/enterprise.service";
1516

1617
interface WorkspaceContextState {
1718
// Workspace data
@@ -25,6 +26,7 @@ import React, {
2526

2627
// Functions
2728
refreshWorkspace: () => Promise<void>;
29+
toggleManagedStatus: (checked: boolean) => Promise<boolean>;
2830
}
2931

3032
const WorkspaceContext = createContext<WorkspaceContextState | undefined>(undefined);
@@ -71,8 +73,29 @@ import React, {
7173
setError(null);
7274

7375
try {
74-
const data = await fetchWorkspaceById(environment.environmentId, workspaceId, environment.environmentApikey, environment.environmentApiServiceUrl!);
75-
setWorkspace(data);
76+
// Fetch the workspace data
77+
const workspaceData = await fetchWorkspaceById(
78+
environment.environmentId,
79+
workspaceId,
80+
environment.environmentApikey,
81+
environment.environmentApiServiceUrl!
82+
);
83+
84+
if (!workspaceData) {
85+
throw new Error("Workspace not found");
86+
}
87+
88+
// Fetch managed workspaces to check if this one is managed
89+
const managedWorkspaces = await getManagedWorkspaces(environment.environmentId);
90+
91+
// Set the managed status
92+
const isManaged = managedWorkspaces.some(org => org.orgGid === workspaceData.gid);
93+
94+
// Update the workspace with managed status
95+
setWorkspace({
96+
...workspaceData,
97+
managed: isManaged
98+
});
7699
} catch (err) {
77100
const errorMessage = err instanceof Error ? err.message : "Workspace not found or failed to load";
78101
setError(errorMessage);
@@ -81,6 +104,35 @@ import React, {
81104
}
82105
}, [workspaceId, environment]);
83106

107+
// Function to toggle managed status
108+
const toggleManagedStatus = useCallback(async (checked: boolean): Promise<boolean> => {
109+
if (!workspace || !environment) {
110+
return false;
111+
}
112+
113+
try {
114+
if (checked) {
115+
// Connect the workspace as managed
116+
await connectManagedWorkspace(
117+
environment.environmentId,
118+
workspace.name,
119+
workspace.gid!
120+
);
121+
} else {
122+
// Disconnect the managed workspace
123+
await unconnectManagedWorkspace(workspace.gid!);
124+
}
125+
126+
// Update local state
127+
setWorkspace(prev => prev ? { ...prev, managed: checked } : null);
128+
129+
return true;
130+
} catch (err) {
131+
const errorMessage = err instanceof Error ? err.message : "Failed to update managed status";
132+
message.error(errorMessage);
133+
return false;
134+
}
135+
}, [workspace, environment]);
84136

85137
// Load workspace data when the component mounts or dependencies change
86138
useEffect(() => {
@@ -93,6 +145,7 @@ import React, {
93145
isLoading,
94146
error,
95147
refreshWorkspace: fetchWorkspace,
148+
toggleManagedStatus
96149
};
97150

98151
return (

0 commit comments

Comments
 (0)