新增项目选择弹窗(预设选项),支持变更环境变量
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
"use client";
|
||||
import React, { createContext, useContext, useEffect, useState } from "react";
|
||||
import { useSession } from "next-auth/react";
|
||||
import { config, NETWORK_NAME, setMapWorkspace, setNetworkName } from "@/config/config";
|
||||
import { ProjectSelector } from "@/components/project/ProjectSelector";
|
||||
|
||||
interface ProjectContextType {
|
||||
workspace: string;
|
||||
networkName: string;
|
||||
}
|
||||
|
||||
const ProjectContext = createContext<ProjectContextType | undefined>(undefined);
|
||||
|
||||
export const ProjectProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||
children,
|
||||
}) => {
|
||||
const { status } = useSession();
|
||||
const [isConfigured, setIsConfigured] = useState(false);
|
||||
const [currentProject, setCurrentProject] = useState({
|
||||
workspace: config.MAP_WORKSPACE,
|
||||
networkName: NETWORK_NAME || "tjwater",
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
// Check localStorage
|
||||
const savedWorkspace = localStorage.getItem("NEXT_PUBLIC_MAP_WORKSPACE");
|
||||
const savedNetwork = localStorage.getItem("NEXT_PUBLIC_NETWORK_NAME");
|
||||
|
||||
// If we have saved config, use it.
|
||||
if (savedWorkspace && savedNetwork) {
|
||||
applyConfig(savedWorkspace, savedNetwork);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const applyConfig = (ws: string, net: string) => {
|
||||
setMapWorkspace(ws);
|
||||
setNetworkName(net);
|
||||
setCurrentProject({ workspace: ws, networkName: net });
|
||||
|
||||
// Save to localStorage
|
||||
localStorage.setItem("NEXT_PUBLIC_MAP_WORKSPACE", ws);
|
||||
localStorage.setItem("NEXT_PUBLIC_NETWORK_NAME", net);
|
||||
|
||||
setIsConfigured(true);
|
||||
};
|
||||
|
||||
// Only show selector if authenticated and not configured
|
||||
if (status === "authenticated" && !isConfigured) {
|
||||
return (
|
||||
<ProjectSelector
|
||||
open={true}
|
||||
onSelect={(ws, net) => applyConfig(ws, net)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<ProjectContext.Provider value={currentProject}>
|
||||
{children}
|
||||
</ProjectContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useProject = () => useContext(ProjectContext);
|
||||
Reference in New Issue
Block a user