mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-01 18:07:00 +01:00
fix: restore task selection focus after call inventory retry
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
3aa4781288
commit
43be85d01b
3 files changed
+12
-2
No files matched your search
@@ -22,9 +22,11 @@ describe("incoming call task selection", () => {
|
||||
mocks.list.mockRejectedValueOnce(new Error("Offline")).mockResolvedValue(tasks); await render();
|
||||
await vi.waitFor(() => expect(container.textContent).toContain("Task list could not be loaded"));
|
||||
const retry = [...container.querySelectorAll("button")].find(button => button.textContent === "Retry tasks")!;
|
||||
retry.focus();
|
||||
await act(async () => retry.click());
|
||||
await vi.waitFor(() => expect(container.querySelector('option[value="task"]')).not.toBeNull());
|
||||
expect(container.querySelector('[role="alert"]')).toBeNull();
|
||||
expect(document.activeElement).toBe(container.querySelector("select"));
|
||||
});
|
||||
it("preserves previously loaded tasks when a background task request fails", async () => {
|
||||
mocks.list.mockResolvedValue(tasks); await render();
|
||||
|
||||
@@ -34,10 +34,17 @@ export function SpekoIncomingCallCard({call, tasks = [], busy, error, onDecide}:
|
||||
}
|
||||
export function SpekoIncomingCalls({companyId, endpointId, agentId}: {companyId: string; endpointId: string; agentId: string}) {
|
||||
const [decision, setDecision] = useState<VoiceInboundCall>();
|
||||
useEffect(() => { setDecision(undefined); }, [companyId, endpointId]);
|
||||
const host = useRef<HTMLDivElement>(null), restoreTaskFocus = useRef(false);
|
||||
useEffect(() => { setDecision(undefined); restoreTaskFocus.current = false; }, [companyId, endpointId]);
|
||||
const query = useQuery({queryKey: ["speko-incoming", companyId, endpointId], queryFn: () => voicePhoneApi.incoming(companyId, endpointId), refetchInterval: 2000, retry: false});
|
||||
const needsTaskSelection = Boolean(query.data?.some(call => ["awaiting_approval", "approving"].includes(call.state)));
|
||||
const tasks = useQuery({queryKey: ["speko-approval-tasks", companyId, agentId], queryFn: () => issuesApi.list(companyId, {assigneeAgentId: agentId}), enabled: needsTaskSelection, retry: false});
|
||||
useEffect(() => {
|
||||
if (restoreTaskFocus.current && !tasks.isFetching && !tasks.isError) {
|
||||
restoreTaskFocus.current = false;
|
||||
host.current?.querySelector<HTMLSelectElement>('select[aria-label="Conversation task"]')?.focus();
|
||||
}
|
||||
}, [tasks.data, tasks.isFetching, tasks.isError]);
|
||||
const mutation = useMutation({mutationFn: ({id, ...input}: {id: string; approve: boolean; approvalCode: string; issueId?: string}) => voicePhoneApi.decide(companyId, endpointId, id, input), onSuccess: result => { setDecision(result); return query.refetch(); }});
|
||||
return <div className="space-y-3">{needsTaskSelection && tasks.isError && <div role="alert" className="space-y-2 text-sm"><p>Task list could not be loaded. Retry to choose an existing task.</p><Button variant="outline" disabled={tasks.isFetching} onClick={() => void tasks.refetch()}>Retry tasks</Button></div>}{query.error && <p role="alert" className="text-sm text-destructive">Incoming call approval is unavailable. {query.error.message}</p>}{!query.error && [...(query.data ?? []).filter(call => call.id !== decision?.id), ...(decision ? [decision] : [])].map(call => <SpekoIncomingCallCard key={call.id} call={call} tasks={tasks.data?.filter(task => !["done", "cancelled"].includes(task.status)).map(task => ({id: task.id, label: `${task.identifier ?? "Task"} · ${task.title}`}))} busy={mutation.isPending && mutation.variables?.id === call.id} error={mutation.variables?.id === call.id ? mutation.error?.message : undefined} onDecide={input => mutation.mutate({id: call.id, ...input})} />)}</div>;
|
||||
return <div ref={host} className="space-y-3">{needsTaskSelection && tasks.isError && <div role="alert" className="space-y-2 text-sm"><p>Task list could not be loaded. Retry to choose an existing task.</p><Button variant="outline" disabled={tasks.isFetching} onClick={() => { restoreTaskFocus.current = true; void tasks.refetch().then(result => { if (result.isError) restoreTaskFocus.current = false; }); }}>Retry tasks</Button></div>}{query.error && <p role="alert" className="text-sm text-destructive">Incoming call approval is unavailable. {query.error.message}</p>}{!query.error && [...(query.data ?? []).filter(call => call.id !== decision?.id), ...(decision ? [decision] : [])].map(call => <SpekoIncomingCallCard key={call.id} call={call} tasks={tasks.data?.filter(task => !["done", "cancelled"].includes(task.status)).map(task => ({id: task.id, label: `${task.identifier ?? "Task"} · ${task.title}`}))} busy={mutation.isPending && mutation.variables?.id === call.id} error={mutation.variables?.id === call.id ? mutation.error?.message : undefined} onDecide={input => mutation.mutate({id: call.id, ...input})} />)}</div>;
|
||||
}
|
||||
@@ -52,5 +52,6 @@ export const TaskListFailureAndRecovery: Story = {
|
||||
await userEvent.keyboard("{Enter}");
|
||||
await expect(await canvas.findByRole("option", {name: "PAP-42 · Ready existing conversation"})).toBeVisible();
|
||||
await waitFor(() => expect(canvas.queryByRole("alert")).not.toBeInTheDocument());
|
||||
await waitFor(() => expect(canvas.getByRole("combobox", {name: "Conversation task"})).toHaveFocus());
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user