import { useEffect, useRef } from "react";

const WebSocketViewer = () => {
    const ws = useRef<any>(null);

    useEffect(() => {
        // Connect to your Go backend WebSocket endpoint
        ws.current = new WebSocket(
            (window.location.protocol === "https:" ? "wss://" : "ws://") +
                window.location.host +
                "/lst/api/logger/logs"
        );

        ws.current.onopen = () => {
            console.log("[WebSocket] Connected");
        };

        ws.current.onmessage = (event: any) => {
            console.log("[WebSocket] Message received:", event.data);
        };

        ws.current.onerror = (error: any) => {
            console.error("[WebSocket] Error:", error);
        };

        ws.current.onclose = () => {
            console.log("[WebSocket] Disconnected");
        };

        // Cleanup on unmount
        return () => {
            if (ws.current) {
                ws.current.close();
            }
        };
    }, []);

    return <div>Check the console for WebSocket messages</div>;
};

export default WebSocketViewer;
