Skip to content

Commit d9f7dd5

Browse files
committed
Add Environment Context
1 parent 8c11ef5 commit d9f7dd5

File tree

3 files changed

+114
-13
lines changed

3 files changed

+114
-13
lines changed

client/packages/lowcoder/src/pages/setting/environments/Environments.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1-
// environments/Environments.tsx
21
import React from "react";
3-
import { Switch, Route, useRouteMatch } from "react-router-dom";
4-
import EnvironmentsList from "./EnvironmentsList"; // Rename your current component
5-
import EnvironmentDetail from "./EnvironmentDetail";
6-
import WorkspaceDetail from "./WorkspaceDetail";
7-
import { ENVIRONMENT_WORKSPACE_DETAIL } from "@lowcoder-ee/constants/routesURL";
2+
import { Switch, Route } from "react-router-dom";
3+
import EnvironmentsList from "./EnvironmentsList";
4+
import EnvironmentScopedRoutes from "./components/EnvironmentScopedRoutes";
85

96
import {
107
ENVIRONMENT_SETTING,
11-
ENVIRONMENT_DETAIL,
8+
ENVIRONMENT_DETAIL
129
} from "@lowcoder-ee/constants/routesURL";
1310

1411
const Environments: React.FC = () => {
1512
return (
1613
<Switch>
17-
<Route path={ENVIRONMENT_WORKSPACE_DETAIL}>
18-
<WorkspaceDetail />
14+
{/* Route that shows the list of environments */}
15+
<Route exact path={ENVIRONMENT_SETTING}>
16+
<EnvironmentsList />
1917
</Route>
2018

19+
{/* All other routes under /environments/:envId */}
2120
<Route path={ENVIRONMENT_DETAIL}>
22-
<EnvironmentDetail />
23-
</Route>
24-
<Route exact path={ENVIRONMENT_SETTING}>
25-
<EnvironmentsList />
21+
<EnvironmentScopedRoutes />
2622
</Route>
2723
</Switch>
2824
);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from "react";
2+
import { Switch, Route, useParams } from "react-router-dom";
3+
import { EnvironmentProvider } from "../context/EnvironmentContext";
4+
5+
import EnvironmentDetail from "../EnvironmentDetail";
6+
import WorkspaceDetail from "../WorkspaceDetail";
7+
8+
import {
9+
ENVIRONMENT_DETAIL,
10+
ENVIRONMENT_WORKSPACE_DETAIL,
11+
} from "@lowcoder-ee/constants/routesURL";
12+
13+
const EnvironmentScopedRoutes: React.FC = () => {
14+
const { environmentId } = useParams<{ environmentId: string }>();
15+
16+
return (
17+
<EnvironmentProvider envId={environmentId}>
18+
<Switch>
19+
<Route exact path={ENVIRONMENT_DETAIL}>
20+
<EnvironmentDetail />
21+
</Route>
22+
23+
<Route exact path={ENVIRONMENT_WORKSPACE_DETAIL}>
24+
<WorkspaceDetail />
25+
</Route>
26+
</Switch>
27+
</EnvironmentProvider>
28+
);
29+
};
30+
31+
export default EnvironmentScopedRoutes;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// src/contexts/EnvironmentContext.tsx
2+
import React, {
3+
createContext,
4+
useContext,
5+
useEffect,
6+
useState,
7+
useCallback,
8+
ReactNode,
9+
} from "react";
10+
import { useHistory } from "react-router-dom";
11+
import { getEnvironmentById } from "../services/environments.service";
12+
import { Environment } from "../types/environment.types";
13+
14+
interface EnvironmentContextType {
15+
environment: Environment | null;
16+
loading: boolean;
17+
error: string | null;
18+
refresh: () => void;
19+
}
20+
21+
const EnvironmentContext = createContext<EnvironmentContextType | undefined>(undefined);
22+
23+
export const useEnvironmentContext = () => {
24+
const context = useContext(EnvironmentContext);
25+
if (!context) {
26+
throw new Error("useEnvironmentContext must be used within an EnvironmentProvider");
27+
}
28+
return context;
29+
};
30+
31+
interface ProviderProps {
32+
envId: string;
33+
children: ReactNode;
34+
}
35+
36+
export const EnvironmentProvider: React.FC<ProviderProps> = ({ envId, children }) => {
37+
const [environment, setEnvironment] = useState<Environment | null>(null);
38+
const [loading, setLoading] = useState<boolean>(true);
39+
const [error, setError] = useState<string | null>(null);
40+
const history = useHistory();
41+
42+
const fetchEnvironment = useCallback(async () => {
43+
setLoading(true);
44+
setError(null);
45+
try {
46+
const data = await getEnvironmentById(envId);
47+
console.log("Environment data:", data);
48+
setEnvironment(data);
49+
} catch (err) {
50+
setError("Environment not found or failed to load");
51+
history.push("/404"); // or a centralized error route
52+
} finally {
53+
setLoading(false);
54+
}
55+
}, [envId, history]);
56+
57+
useEffect(() => {
58+
fetchEnvironment();
59+
}, [fetchEnvironment]);
60+
61+
const value: EnvironmentContextType = {
62+
environment,
63+
loading,
64+
error,
65+
refresh: fetchEnvironment,
66+
};
67+
68+
return (
69+
<EnvironmentContext.Provider value={value}>
70+
{children}
71+
</EnvironmentContext.Provider>
72+
);
73+
};
74+

0 commit comments

Comments
 (0)