agenthub/frontend/components/LiveProvider.tsx

27 lines
812 B
TypeScript

"use client";
// One WebSocket for the whole console. Pages refetch when `revision` moves;
// the hub stays the source of truth.
import { createContext, useContext, type ReactNode } from "react";
import { useHubSocket, type SocketStatus } from "@/lib/useHubSocket";
import type { Frame } from "@/lib/types";
interface LiveValue {
status: SocketStatus;
revision: number;
lastFrame: Frame | null;
}
const LiveContext = createContext<LiveValue>({ status: "connecting", revision: 0, lastFrame: null });
export function LiveProvider({ children }: { children: ReactNode }) {
const { status, revision, lastFrame } = useHubSocket(["*.>"]);
return <LiveContext value={{ status, revision, lastFrame }}>{children}</LiveContext>;
}
export function useLive(): LiveValue {
return useContext(LiveContext);
}