UNPKG

@nwebui/react-niagara-core

Version:
129 lines 4.44 kB
import React, { createContext, useContext, useEffect, useState, } from "react"; import * as baja from "@nwebui/bajascript"; import { NiagaraClient } from "./client"; import DefaultLogin from "./login"; const SessionContext = createContext({}); export const SessionProvider = function ({ loginComponent, noSession, children, }) { const [loading, setLoading] = useState(false); const [username, setUsername] = useState(); const [session, setSession] = useState(); const [error, setError] = useState(""); const [closed, setClosed] = useState(false); const [user, setUser] = useState(); const Login = loginComponent || DefaultLogin; async function login(username, password) { const success = await NiagaraClient.INSTANCE.login(username, password); if (success) { setTimeout(() => setUsername(username), 0); } return success; } async function getUsername() { try { setLoading(true); const username = await NiagaraClient.INSTANCE.username(); setUsername(username); } catch (e) { setError(e.toString()); } finally { setLoading(false); } } useEffect(() => { getUsername(); }, []); async function setupSession() { try { setLoading(true); setClosed(false); setUser(undefined); setSession(undefined); const session = baja.localhost.session; await session.open(); const user = (await session.resolve(`station:|slot:/Services/UserService/${username}`)); await user.loadSlots(); setUser(user); setSession(session); session.addListener("close", () => setClosed(true)); // globalThis.niagaraSession = session; } catch (e) { console.log(e); setError(e.toString()); } finally { setLoading(false); } } useEffect(() => { if (!username) { return; } if (!noSession) { setupSession(); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [username]); if (loading) { return (React.createElement("div", { style: { width: "100%", height: "100%", display: "flex", alignItems: "center", justifyContent: "center", } }, React.createElement("span", null, "\u6B63\u5728\u52A0\u8F7D..."))); } if (error) { return (React.createElement("div", { style: { width: "100%", height: "100%", display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center", } }, error, React.createElement("button", { onClick: () => setupSession() }, "\u91CD\u65B0\u8FDE\u63A5"))); } if (username === undefined) { return null; } if (!username) { return React.createElement(Login, { login: login }); } if (!noSession && (!user || !session)) { return null; } return (React.createElement(SessionContext.Provider, { value: { username, session, user } }, closed && (React.createElement("div", { style: { position: "absolute", width: "100%", height: "100%", top: 0, left: 0, display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center", zIndex: 10000, background: "rgba(0,0,0,0.7)", } }, "\u8FDE\u63A5\u5DF2\u65AD\u5F00", React.createElement("button", { onClick: () => setupSession() }, "\u91CD\u65B0\u8FDE\u63A5"))), children)); }; export function useUsername() { const ctx = useContext(SessionContext); return ctx.username; } export function useSession() { const ctx = useContext(SessionContext); return ctx.session; } export function useUser() { return useContext(SessionContext).user; } //# sourceMappingURL=session.js.map