import React from "react"; import { useTranslation } from "react-i18next"; import { useSelector } from "react-redux"; import toast from "react-hot-toast"; import { RootState } from "#/store"; import { AgentState } from "#/types/agent-state"; import { AGENT_STATUS_MAP } from "../../agent-status-map.constant"; import { useWsClient, WsClientProviderStatus, } from "#/context/ws-client-provider"; export function AgentStatusBar() { const { t, i18n } = useTranslation(); const { curAgentState } = useSelector((state: RootState) => state.agent); const { curStatusMessage } = useSelector((state: RootState) => state.status); const { status } = useWsClient(); const [statusMessage, setStatusMessage] = React.useState(""); const updateStatusMessage = () => { let message = curStatusMessage.message || ""; if (curStatusMessage?.id) { const id = curStatusMessage.id.trim(); if (i18n.exists(id)) { message = t(curStatusMessage.id.trim()) || message; } } if (curStatusMessage?.type === "error") { toast.error(message); return; } if (curAgentState === AgentState.LOADING && message.trim()) { setStatusMessage(message); } else { setStatusMessage(AGENT_STATUS_MAP[curAgentState].message); } }; React.useEffect(() => { updateStatusMessage(); }, [curStatusMessage.id]); React.useEffect(() => { if (status === WsClientProviderStatus.DISCONNECTED) { setStatusMessage("Trying to reconnect..."); } else { setStatusMessage(AGENT_STATUS_MAP[curAgentState].message); } }, [curAgentState]); return (
{t(statusMessage)}
); }