Newer
Older
PropsWithChildren,
createContext,
useContext,
useEffect,
useState,
} from "react";
ClientConfig,
IAppContext,
ICanvasPosition,
IPosition,
import Network from "../lib/network";
const appContext = createContext<IAppContext>({} as any);
export const useAppContext = () => useContext(appContext);
export const AppContext = ({ children }: PropsWithChildren) => {
const [config, setConfig] = useState<ClientConfig>(undefined as any);
const [auth, setAuth] = useState<AuthSession>();
const [canvasPosition, setCanvasPosition] = useState<ICanvasPosition>();
const [cursorPosition, setCursorPosition] = useState<IPosition>();
// --- settings ---
const [loadChat, _setLoadChat] = useState(false);
const [undo, setUndo] = useState<{ available: true; expireAt: number }>();
// overlays visible
const [settingsSidebar, setSettingsSidebar] = useState(false);
function loadSettings() {
setLoadChat(localStorage.getItem("matrix.enable") === "true");
}
function handleConfig(config: ClientConfig) {
console.info("Server sent config", config);
setConfig(config);
}
function handleUser(user: AuthSession) {
setAuth(user);
}
function handlePixels(pixels: { available: number }) {
setPixels(pixels);
}
function handleUndo(
data: { available: false } | { available: true; expireAt: number }
) {
if (data.available) {
setUndo({ available: true, expireAt: data.expireAt });
} else {
setUndo(undefined);
}
}
Network.on("user", handleUser);
Network.on("config", handleConfig);
Network.waitFor("pixels").then(([data]) => handlePixels(data));
Network.on("pixels", handlePixels);
return () => {
Network.off("user", handleUser);
Network.off("config", handleConfig);
const setLoadChat = (v: boolean) => {
_setLoadChat(v);
localStorage.setItem("matrix.enable", v ? "true" : "false");
};
value={{
config,
user: auth,
canvasPosition,
setCanvasPosition,
cursorPosition,
setCursorPosition,
settingsSidebar,
setSettingsSidebar,