import { useEffect, useMemo } from "react"; import { cx } from "@/cva.config"; import { HidState, MouseState, RTCState, SettingsState, useHidStore, useMouseStore, useRTCStore, useSettingsStore, useVideoStore, VideoState, } from "@/hooks/stores"; import { keys, modifiers } from "@/keyboardMappings"; export default function InfoBar() { const keysDownState = useHidStore((state: HidState) => state.keysDownState); const mouseX = useMouseStore((state: MouseState) => state.mouseX); const mouseY = useMouseStore((state: MouseState) => state.mouseY); const mouseMove = useMouseStore((state: MouseState) => state.mouseMove); const videoClientSize = useVideoStore( (state: VideoState) => `${Math.round(state.clientWidth)}x${Math.round(state.clientHeight)}`, ); const videoSize = useVideoStore( (state: VideoState) => `${Math.round(state.width)}x${Math.round(state.height)}`, ); const rpcDataChannel = useRTCStore((state: RTCState) => state.rpcDataChannel); const settings = useSettingsStore(); const showPressedKeys = useSettingsStore((state: SettingsState) => state.showPressedKeys); useEffect(() => { if (!rpcDataChannel) return; rpcDataChannel.onclose = () => console.log("rpcDataChannel has closed"); rpcDataChannel.onerror = (e: Event) => console.error(`Error on DataChannel '${rpcDataChannel.label}': ${e}`); }, [rpcDataChannel]); const keyboardLedState = useHidStore((state: HidState) => state.keyboardLedState); const isTurnServerInUse = useRTCStore((state: RTCState) => state.isTurnServerInUse); const usbState = useHidStore((state: HidState) => state.usbState); const hdmiState = useVideoStore((state: VideoState) => state.hdmiState); const displayKeys = useMemo(() => { if (!showPressedKeys) return ""; const activeModifierMask = keysDownState.modifier || 0; const keysDown = keysDownState.keys || []; const modifierNames = Object.entries(modifiers).filter(([_, mask]) => (activeModifierMask & mask) !== 0).map(([name, _]) => name); const keyNames = Object.entries(keys).filter(([_, value]) => keysDown.includes(value)).map(([name, _]) => name); return [...modifierNames,...keyNames].join(", "); }, [keysDownState, showPressedKeys]); return (
{settings.debugMode ? (
Resolution:{" "} {videoSize}
) : null} {settings.debugMode ? (
Video Size: {videoClientSize}
) : null} {(settings.debugMode && settings.mouseMode == "absolute") ? (
Pointer: {mouseX},{mouseY}
) : null} {(settings.debugMode && settings.mouseMode == "relative") ? (
Last Move: {mouseMove ? `${mouseMove.x},${mouseMove.y} ${mouseMove.buttons ? `(${mouseMove.buttons})` : ""}` : "N/A"}
) : null} {settings.debugMode && (
USB State: {usbState}
)} {settings.debugMode && (
HDMI State: {hdmiState}
)} {showPressedKeys && (
Keys:

{displayKeys}

)}
{isTurnServerInUse && (
Relayed by Cloudflare
)}
Caps Lock
Num Lock
Scroll Lock
{keyboardLedState.compose ? (
Compose
) : null} {keyboardLedState.kana ? (
Kana
) : null} {keyboardLedState.shift ? (
Shift
) : null}
); }