Created the LinkContext.jsx

This commit is contained in:
2022-12-28 03:36:42 +01:00
parent c9e25d8248
commit 0fb5960eb8

View File

@ -0,0 +1,26 @@
import React, {createContext, useEffect, useState} from "react";
import {jsonRequest} from "@/common/utils/RequestUtil";
export const LinkContext = createContext({});
export const LinkProvider = (props) => {
const [domain, setDomain] = useState(localStorage.getItem("domain") || "localhost"); // TODO
const [links, setLinks] = useState([]);
const [query, setQuery] = useState(""); // TODO
const updateLinks = () => jsonRequest(`/link/${domain}/list`)
.then(json => setLinks(json));
useEffect(() => {
updateLinks();
const interval = setInterval(() => updateLinks(), 15000);
return () => clearInterval(interval);
}, []);
return (
<LinkContext.Provider value={[links, updateLinks, setQuery, setDomain]}>
{props.children}
</LinkContext.Provider>
)
}