import { useEffect, useRef, useState } from "react"; import { AppContext, SystemInfoContext } from "../scripts/contexts"; import { systemApi } from "../scripts/clientApi"; import { AppInfoContext, SystemInfoType } from '@simeonradivoev/gameflow-sdk/shared'; import LoadingScreen from "./LoadingScreen"; import { GamepadKeyboard } from "./GamepadKeyboard"; export default function AppCommunication (data: { children: any; }) { const [systemInfo, setSystemInfo] = useState(); const [appContext, setAppContext] = useState({} as AppInfoContext); const [loadingInfo, setLoadingInfo] = useState(undefined); const [loading, setLoading] = useState(true); const loadingProgressBarRef = useRef(null); useEffect(() => { const sub = systemApi.api.system.info.system.subscribe(); sub.subscribe(({ data }) => { switch (data.type) { case "info": setSystemInfo(data.data); break; case "focus": window.focus(); break; case "activeTask": setAppContext(c => ({ ...c, activeTaskProgress: data.progress })); break; case "loading": setLoadingInfo(data.state); if (loadingProgressBarRef.current) loadingProgressBarRef.current.value = data.progress; setLoading(true); break; case "loaded": setLoading(false); break; } }); document.documentElement.dataset.loaded = "true"; return () => { sub.close(); }; }, []); return {loading ?
{loadingInfo}
: data.children}
; }