"use client"; import * as React from "react"; import { LoadingButton } from "@/components/Button/LoadingButton"; import { setAgentConnected, setMobileActiveTab } from "@/store/reducers/global"; import { useAppDispatch, useAppSelector, apiPing, apiStartService, apiStopService, MOBILE_ACTIVE_TAB_MAP, EMobileActiveTab, isEditModeOn, } from "@/common"; import { toast } from "sonner"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { cn } from "@/lib/utils"; import { RemotePropertyCfgSheet } from "@/components/Chat/ChatCfgPropertySelect"; import { RemoteGraphSelect } from "@/components/Chat/ChatCfgGraphSelect"; import { RemoteModuleCfgSheet } from "@/components/Chat/ChatCfgModuleSelect"; import { TrulienceCfgSheet } from "../Chat/ChatCfgTrulienceSetting"; let intervalId: NodeJS.Timeout | null = null; export default function Action(props: { className?: string }) { const { className } = props; const dispatch = useAppDispatch(); const agentConnected = useAppSelector((state) => state.global.agentConnected); const channel = useAppSelector((state) => state.global.options.channel); const userId = useAppSelector((state) => state.global.options.userId); const language = useAppSelector((state) => state.global.language); const voiceType = useAppSelector((state) => state.global.voiceType); const graphName = useAppSelector((state) => state.global.selectedGraphId); const mobileActiveTab = useAppSelector( (state) => state.global.mobileActiveTab ); const [loading, setLoading] = React.useState(false); React.useEffect(() => { if (channel) { checkAgentConnected(); } }, [channel]); const checkAgentConnected = async () => { const res: any = await apiPing(channel); if (res?.code == 0) { dispatch(setAgentConnected(true)); } }; const onClickConnect = async () => { if (loading) { return; } setLoading(true); if (agentConnected) { await apiStopService(channel); dispatch(setAgentConnected(false)); toast.success("Agent disconnected"); stopPing(); } else { const res = await apiStartService({ channel, userId, graphName, language, voiceType, }); const { code, msg } = res || {}; if (code != 0) { if (code == "10001") { toast.error( "The number of users experiencing the program simultaneously has exceeded the limit. Please try again later." ); } else { toast.error(`code:${code},msg:${msg}`); } setLoading(false); throw new Error(msg); } dispatch(setAgentConnected(true)); toast.success("Agent connected"); startPing(); } setLoading(false); }; const startPing = () => { if (intervalId) { stopPing(); } intervalId = setInterval(() => { apiPing(channel); }, 3000); }; const stopPing = () => { if (intervalId) { clearInterval(intervalId); intervalId = null; } }; const onChangeMobileActiveTab = (tab: string) => { dispatch(setMobileActiveTab(tab as EMobileActiveTab)); }; return ( <> {/* Action Bar */}
{/* -- Description Part */}
{/* -- Tabs Section */} {Object.values(EMobileActiveTab).map((tab) => ( {MOBILE_ACTIVE_TAB_MAP[tab]} ))} {/* -- Graph Select Part */}
{isEditModeOn && ( <> )} {/* -- Action Button */}
{loading ? "Connecting" : !agentConnected ? "Connect" : "Disconnect"}
); }