优化项目配置逻辑,增强错误处理和状态更新
This commit is contained in:
@@ -51,27 +51,54 @@ export const ProjectProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||
setIsConfigured(true);
|
||||
|
||||
try {
|
||||
const response = await apiFetch(
|
||||
// Open project backend (simulation model)
|
||||
const openResponse = await apiFetch(
|
||||
`${config.BACKEND_URL}/openproject/?network=${net}`,
|
||||
{
|
||||
method: "POST",
|
||||
},
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
if (!openResponse.ok) {
|
||||
throw new Error(`Failed to open project: HTTP ${openResponse.status}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
const bbox = Array.isArray(data?.map_extent?.bbox)
|
||||
? data.map_extent.bbox.map((value: number) => Number(value))
|
||||
: null;
|
||||
if (bbox && bbox.length === 4) {
|
||||
setMapExtent(bbox);
|
||||
localStorage.setItem("NEXT_PUBLIC_MAP_EXTENT", bbox.join(","));
|
||||
localStorage.removeItem(`${ws}_map_view`);
|
||||
setCurrentProject((prev) => ({ ...prev, extent: bbox }));
|
||||
|
||||
// Fetch project metadata
|
||||
const infoResponse = await apiFetch(
|
||||
`${config.BACKEND_URL}/project_info/?network=${net}`,
|
||||
);
|
||||
if (!infoResponse.ok) {
|
||||
console.warn(
|
||||
`Failed to fetch project info: HTTP ${infoResponse.status}`,
|
||||
);
|
||||
} else {
|
||||
const data = await infoResponse.json();
|
||||
|
||||
// Update workspace if different
|
||||
if (data?.gs_workspace && data.gs_workspace !== ws) {
|
||||
setMapWorkspace(data.gs_workspace);
|
||||
localStorage.setItem(
|
||||
"NEXT_PUBLIC_MAP_WORKSPACE",
|
||||
data.gs_workspace,
|
||||
);
|
||||
setCurrentProject((prev) => ({
|
||||
...prev,
|
||||
workspace: data.gs_workspace,
|
||||
}));
|
||||
}
|
||||
|
||||
// Update extent if available
|
||||
const bbox = Array.isArray(data?.map_extent?.bbox)
|
||||
? data.map_extent.bbox.map((value: number) => Number(value))
|
||||
: null;
|
||||
if (bbox && bbox.length === 4) {
|
||||
setMapExtent(bbox);
|
||||
localStorage.setItem("NEXT_PUBLIC_MAP_EXTENT", bbox.join(","));
|
||||
localStorage.removeItem(`${ws}_map_view`);
|
||||
setCurrentProject((prev) => ({ ...prev, extent: bbox }));
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to open project:", error);
|
||||
console.error("Failed to setup project:", error);
|
||||
}
|
||||
}, [setCurrentProjectId]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user