Skip to content

Commit 058db37

Browse files
committed
add enterprise managed workspaces hook
1 parent 204890a commit 058db37

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { useEffect, useState } from "react";
2+
import { ManagedOrg } from "../../types/enterprise.types";
3+
import {
4+
getManagedWorkspaces,
5+
} from "../../services/enterprise.service";
6+
import { Environment } from "../../types/environment.types";
7+
8+
export function useManagedWorkspaces(
9+
environment: Environment | null
10+
) {
11+
const [managed, setManaged] = useState<ManagedOrg[]>([]);
12+
const [loading, setLoading] = useState<boolean>(false);
13+
const [error, setError] = useState<string | null>(null);
14+
15+
const fetchManaged = async () => {
16+
if (!environment) return;
17+
setLoading(true);
18+
setError(null);
19+
20+
21+
try {
22+
const { environmentId, environmentApikey, environmentApiServiceUrl } = environment;
23+
24+
if (!environmentApikey || !environmentApiServiceUrl) {
25+
setError("Missing API key or service URL for this environment.");
26+
setLoading(false);
27+
return;
28+
}
29+
30+
const result = await getManagedWorkspaces(environmentId, environmentApiServiceUrl);
31+
setManaged(result);
32+
} catch (err: any) {
33+
setError(err.message ?? "Failed to load managed workspaces");
34+
} finally {
35+
setLoading(false);
36+
}
37+
};
38+
39+
useEffect(() => {
40+
fetchManaged();
41+
}, [environment, fetchManaged]);
42+
43+
return {
44+
managedWorkspaces: managed,
45+
managedLoading: loading,
46+
managedError: error,
47+
refreshManagedWorkspaces: fetchManaged,
48+
};
49+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import axios from "axios";
2+
import { message } from "antd";
3+
import { ManagedOrg } from "../types/enterprise.types";
4+
5+
/**
6+
* Fetch workspaces for a specific environment
7+
* @param apiServiceUrl - API service URL for the environment
8+
* @param environmentId - ID of the environment
9+
*
10+
*
11+
*/
12+
13+
export async function getManagedWorkspaces(
14+
environmentId: string,
15+
apiServiceUrl: string
16+
): Promise<ManagedOrg[]> {
17+
if (!environmentId || !apiServiceUrl) {
18+
throw new Error("Missing environmentId or apiServiceUrl");
19+
}
20+
21+
try {
22+
const res = await axios.get(`${apiServiceUrl}/api/plugins/enterprise/org`);
23+
const all: ManagedOrg[] = res.data;
24+
return all.filter(org => org.environmentId === environmentId);
25+
} catch (err) {
26+
const errorMsg = err instanceof Error ? err.message : "Failed to fetch managed workspaces";
27+
message.error(errorMsg);
28+
throw err;
29+
}
30+
}
31+
32+
33+
/**
34+
* Fetch workspaces for a specific environment
35+
* @param apiServiceUrl - API service URL for the environment
36+
* @param environmentId - ID of the environment
37+
* @param orgName - Name of the workspace
38+
* @param orgTags - Tags of the workspace
39+
*
40+
*/
41+
42+
export async function connectManagedWorkspace(
43+
environmentId: string,
44+
apiServiceUrl: string,
45+
orgName: string,
46+
orgTags: string[] = []
47+
) {
48+
if (!environmentId || !apiServiceUrl || !orgName) {
49+
throw new Error("Missing required params to connect org");
50+
}
51+
52+
try {
53+
const payload = {
54+
environment_id: environmentId,
55+
org_name: orgName,
56+
org_tags: orgTags,
57+
};
58+
59+
const res = await axios.post(`${apiServiceUrl}/api/plugins/enterprise/org`, payload);
60+
return res.data;
61+
} catch (err) {
62+
const errorMsg = err instanceof Error ? err.message : "Failed to connect org";
63+
message.error(errorMsg);
64+
throw err;
65+
}
66+
}
67+
68+
69+
70+
71+
72+
73+
/**
74+
* Fetch workspaces for a specific environment
75+
* @param apiServiceUrl - API service URL for the environment
76+
* @param orgId - ID of the workspace
77+
*
78+
*/
79+
export async function unconnectManagedWorkspace(
80+
apiServiceUrl: string,
81+
orgId: string
82+
) {
83+
if (!apiServiceUrl || !orgId) {
84+
throw new Error("Missing apiServiceUrl or orgId");
85+
}
86+
87+
try {
88+
await axios.delete(`${apiServiceUrl}/api/plugins/enterprise/org/${orgId}`);
89+
} catch (err) {
90+
const errorMsg = err instanceof Error ? err.message : "Failed to unconnect org";
91+
message.error(errorMsg);
92+
throw err;
93+
}
94+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export interface ManagedOrg {
2+
orgGid: string;
3+
environmentId: string;
4+
orgName: string;
5+
orgTags: string[];
6+
createdAt: string;
7+
updatedAt: string;
8+
}
9+

0 commit comments

Comments
 (0)