Created the ServerContext.jsx
This commit is contained in:
parent
80b9786d0f
commit
2871cfd9c8
27
webui/src/common/contexts/Server/ServerContext.jsx
Normal file
27
webui/src/common/contexts/Server/ServerContext.jsx
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import {createContext, useEffect, useState} from "react";
|
||||||
|
import {jsonRequest} from "@/common/utils/RequestUtil.js";
|
||||||
|
|
||||||
|
export const ServerContext = createContext({});
|
||||||
|
|
||||||
|
export const ServerProvider = ({children}) => {
|
||||||
|
|
||||||
|
const [server, setServer] = useState(null);
|
||||||
|
|
||||||
|
const updateServer = () => {
|
||||||
|
jsonRequest("server/").then((data) => setServer(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
updateServer();
|
||||||
|
|
||||||
|
const interval = setInterval(updateServer, 5000);
|
||||||
|
|
||||||
|
return () => clearInterval(interval);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ServerContext.Provider value={{server, updateServer}}>
|
||||||
|
{children}
|
||||||
|
</ServerContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
Reference in New Issue
Block a user