"use client"; import {useEffect, useMemo, useState} from "react"; import {fetchConfig} from "@/app/utils/fetchConfig"; import BusControlTabs from "@/app/components/BusControlTabs"; import MainMix from "@/app/components/MainMix"; import Settings from "@/app/components/Settings"; import {Config} from "@/app/types/config"; // initialize global websocket connection import {fetchValue, initConnection, isConnected} from "@/app/lib/x32CommunicationManager"; function withTimeout(promise: Promise, timeoutMs: number): Promise { return new Promise((resolve, reject) => { const timeout = setTimeout(() => { reject(new Error("Request timed out")); }, timeoutMs); promise .then((result) => { clearTimeout(timeout); resolve(result); }) .catch((error) => { clearTimeout(timeout); reject(error); }); }); } const Page = () => { const [appConfig, setAppConfig] = useState(null); const [activeTab, setActiveTab] = useState("mainMix"); const [webSocketConnected, setWebSocketConnected] = useState(false); const [mixerConnected, setMixerConnected] = useState(false); useEffect(() => { fetchConfig().then((config: Config) => { setAppConfig(config); initConnection(`ws://${window.location.hostname}:${config.webserver.port}`); }); }, []); // periodically check if the websocket connection is still alive useEffect(() => { const interval = setInterval(() => { // wait for appConfig to be fetched if (!appConfig) { console.log("AppConfig not fetched yet"); return; } setWebSocketConnected(isConnected); if (!isConnected) { console.log("Reconnecting to WebSocket"); initConnection(`ws://${window.location.hostname}:${appConfig.webserver.port}`); } else { // check if mixer is connected withTimeout(fetchValue("/info"), 1500) .then((response) => { // response is a string starting with "V", which is the API version, if the mixer is connected if (response.startsWith("V")) { setMixerConnected(true); } else { setMixerConnected(false); } }) .catch((error) => { console.log("Mixer connection check failed:", error.message); setMixerConnected(false); }); } }, 2000); return () => clearInterval(interval); }, [appConfig]); const tabComponents = useMemo(() => { return [ { id: "mainMix", label: "Main Mix", content: ( ), }, { id: "busControl", label: "Bus Control", content: ( ), }, { id: "recording", label: "Recording", content: (

Recording

Coming soon...

), }, { id: "settings", label: "Settings", content: ( ), } ] } , []); if (!appConfig) { return

Error fetching config

; } if (!webSocketConnected) { return

WebSocket not connected

; } if (!mixerConnected) { return

Mixer not connected

; } return (
{/* Tabs Header */}
{tabComponents.map((tab) => ( ))}
{/* Tabs Content */}
{tabComponents.map((tab) => (
{tab.content}
))}
); }; export default Page;