fix: restore keyboard focus after voice connection retry

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-10-09 16:16:48 -05:00
1 parent 38057142bb
commit 4ca0c3cf28
2 files changed
+5 -3

No files matched your search

@@ -1,4 +1,4 @@
import { useState } from "react";
import { useRef, useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { chatEndpointsApi } from "@/api/chatEndpoints";
import { useChatConnectorsEnabled } from "@/hooks/useChatConnectorsEnabled";
@@ -6,6 +6,7 @@ import { Button } from "@/components/ui/button";
import { NativeVoiceConversation } from "./NativeVoiceConversation";
export function TaskVoiceLauncher({ companyId, issueId, agentId, boundEndpointId }: { companyId: string; issueId: string; agentId?: string | null; boundEndpointId?: string }) {
const launcher = useRef<HTMLButtonElement>(null);
const { enabled } = useChatConnectorsEnabled();
const [open, setOpen] = useState(false), [selected, setSelected] = useState("");
const connections = useQuery({ queryKey: ["task-voice-endpoints", companyId], queryFn: () => chatEndpointsApi.list(companyId), enabled: enabled && Boolean(agentId || boundEndpointId) });
@@ -14,7 +15,7 @@ export function TaskVoiceLauncher({ companyId, issueId, agentId, boundEndpointId
const endpoint = candidates.find((entry) => entry.id === selected) ?? candidates[0];
const connectionError = connections.isError ? <div role="alert" className="space-y-2 text-sm">
<p>Voice connections could not be loaded.</p>
<Button variant="outline" disabled={connections.isFetching} onClick={() => void connections.refetch()}>Retry voice connections</Button>
<Button variant="outline" disabled={connections.isFetching} onClick={() => { void connections.refetch().then(result => { if (!result.isError) requestAnimationFrame(() => launcher.current?.focus()); }); }}>Retry voice connections</Button>
</div> : null;
if (!endpoint && connectionError) return <section aria-label="Task voice conversation" className="space-y-3 rounded-lg border border-border p-4">{connectionError}</section>;
if (!endpoint) return boundEndpointId ? <p className="text-sm text-muted-foreground">{connections.isPending ? "Loading voice connection…" : "Voice is unavailable. Check the Speko connection in Apps."}</p> : null;
@@ -22,7 +23,7 @@ export function TaskVoiceLauncher({ companyId, issueId, agentId, boundEndpointId
{connectionError}
{!open ? <div className="flex flex-wrap items-center gap-3">
{candidates.length > 1 && <label className="flex min-w-0 max-w-full flex-col gap-2 text-sm">Voice persona<select aria-label="Voice persona" className="w-full min-w-0 max-w-full rounded-md border border-input bg-background p-2 text-sm" value={endpoint.id} onChange={(event) => setSelected(event.target.value)}>{candidates.map((entry) => <option key={entry.id} value={entry.id}>{entry.botLabel ?? entry.assignedAgentName}</option>)}</select></label>}
<Button variant="outline" onClick={() => setOpen(true)}>Talk to {endpoint.assignedAgentName}</Button>
<Button ref={launcher} variant="outline" onClick={() => setOpen(true)}>Talk to {endpoint.assignedAgentName}</Button>
</div> : <NativeVoiceConversation key={`${companyId}:${issueId}:${endpoint.id}`} companyId={companyId} endpointId={endpoint.id} issueId={issueId} agentName={endpoint.assignedAgentName} />}
</section>;
}
@@ -60,5 +60,6 @@ export const BoundConnectionRecovery: Story = {
await userEvent.keyboard("{Enter}");
await expect(canvas.getByRole("button", { name: `Talk to ${spekoAgent.name}` })).toBeVisible();
await expect(canvas.queryByRole("alert")).not.toBeInTheDocument();
await expect(canvas.getByRole("button", { name: `Talk to ${spekoAgent.name}` })).toHaveFocus();
},
};