import { useContext, useEffect, useRef, useState } from "react"; import { useQuery } from "@tanstack/react-query"; import { queryKeys } from "@/lib/queryKeys"; import { chatEndpointsApi } from "@/api/chatEndpoints"; import { useChatConnectorsEnabled } from "@/hooks/useChatConnectorsEnabled"; import { Button } from "@/components/ui/button"; import { VoiceCallContext } from "./VoiceCallContext"; import { NativeVoiceConversation } from "./NativeVoiceConversation"; export function TaskVoiceLauncher({ companyId, issueId, agentId, boundEndpointId }: { companyId: string; issueId: string; agentId?: string | null; boundEndpointId?: string }) { const launcher = useRef(null), restoreFocus = useRef(false); const host = useContext(VoiceCallContext), previouslyActive = useRef(false); const { enabled } = useChatConnectorsEnabled(); const [open, setOpen] = useState(false), [selected, setSelected] = useState(""); const connections = useQuery({ queryKey: queryKeys.chatEndpoints.list(companyId), queryFn: () => chatEndpointsApi.list(companyId), enabled: enabled && Boolean(agentId || boundEndpointId) }); useEffect(() => { if (previouslyActive.current && !host?.active && open) { setOpen(false); restoreFocus.current = true; } previouslyActive.current = Boolean(host?.active); }, [host?.active, open]); useEffect(() => { if (restoreFocus.current && !connections.isFetching && !connections.isError && launcher.current) { restoreFocus.current = false; launcher.current.focus(); } }, [connections.data, connections.isFetching, connections.isError, open]); if (!enabled) return null; const candidates = (connections.data ?? []).filter((endpoint) => endpoint.provider === "speko" && endpoint.status === "active" && (boundEndpointId ? endpoint.id === boundEndpointId : endpoint.assignedAgentId === agentId)); const endpoint = candidates.find((entry) => entry.id === selected) ?? candidates[0]; const connectionError = connections.isError ?

Voice connections could not be loaded.

: null; if (!endpoint && connectionError) return
{connectionError}
; if (!endpoint) return boundEndpointId ?

{connections.isPending ? "Loading voice connection…" : "Voice is unavailable. Check the Speko connection in Apps."}

: null; return
{connectionError} {!open ?
{candidates.length > 1 && }
: }
; }