Skip to content

Commit 2377f4f

Browse files
authored
Fix use infinite tasks hook (#14)
Removes the useTasks hook (was not in use) and fixes the useCreateTask hook to respect the InfiniteData type that is returned from the useInfiniteQuery hook This caused a bug on the frontend where trying to create a task after fetching all tasks would cause an error
1 parent 15e5d60 commit 2377f4f

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

agentex-ui/hooks/use-create-task.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
'use client';
22

3-
import { useMutation, useQueryClient } from '@tanstack/react-query';
3+
import {
4+
InfiniteData,
5+
useMutation,
6+
useQueryClient,
7+
} from '@tanstack/react-query';
48
import { agentRPCNonStreaming } from 'agentex/lib';
59

610
import { toast } from '@/components/agentex/toast';
711

812
import { tasksKeys } from './use-tasks';
913

1014
import type AgentexSDK from 'agentex';
11-
import type { Task } from 'agentex/resources';
15+
import type { Task, TaskListResponse } from 'agentex/resources';
1216

1317
type CreateTaskParams = {
1418
agentName: string;
@@ -51,16 +55,37 @@ export function useCreateTask({
5155

5256
return response.result;
5357
},
54-
onSuccess: newTask => {
55-
// Add new task to the unfiltered cache
56-
queryClient.setQueryData<Task[]>(tasksKeys.all, old => {
57-
if (!old) return [newTask];
58-
// Avoid duplicates
59-
if (old.some(t => t.id === newTask.id)) {
60-
return old;
61-
}
62-
return [...old, newTask]; // Add to end (will be reversed in UI to show first)
63-
});
58+
onSuccess: (newTask, variables) => {
59+
// Helper function to update infinite query cache
60+
const updateInfiniteCache = (queryKey: readonly unknown[]) => {
61+
queryClient.setQueryData<InfiniteData<TaskListResponse>>(
62+
queryKey,
63+
old => {
64+
if (!old) {
65+
// If no cache exists, create initial structure
66+
return {
67+
pages: [[newTask]],
68+
pageParams: [1],
69+
};
70+
}
71+
72+
// Add new task to the first page (prepend to show at top)
73+
const firstPage = old.pages[0] ?? [];
74+
if (firstPage.some(t => t.id === newTask.id)) {
75+
return old; // Avoid duplicates
76+
}
77+
78+
return {
79+
...old,
80+
pages: [[newTask, ...firstPage], ...old.pages.slice(1)],
81+
};
82+
}
83+
);
84+
};
85+
86+
// Update both the agent-specific cache and the generic "all tasks" cache
87+
updateInfiniteCache(tasksKeys.byAgentName(variables.agentName));
88+
updateInfiniteCache(tasksKeys.all);
6489

6590
// Invalidate all task queries to ensure consistency
6691
queryClient.invalidateQueries({ queryKey: tasksKeys.all });

agentex-ui/hooks/use-tasks.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,6 @@ export const tasksKeys = {
1919
byId: (taskId: string) => [...tasksKeys.all, taskId] as const,
2020
};
2121

22-
/**
23-
* Fetches the list of tasks, optionally filtered by agent name
24-
*/
25-
export function useTasks(
26-
agentexClient: AgentexSDK,
27-
options?: { agentName?: string }
28-
) {
29-
const { agentName } = options || {};
30-
31-
return useQuery({
32-
queryKey: tasksKeys.byAgentName(agentName),
33-
queryFn: async (): Promise<TaskListResponse> => {
34-
const params: TaskListParams = {
35-
relationships: ['agents'],
36-
...(agentName ? { agent_name: agentName } : {}),
37-
};
38-
return agentexClient.tasks.list(params);
39-
},
40-
staleTime: 30 * 1000, // 30 seconds
41-
refetchOnWindowFocus: true,
42-
});
43-
}
44-
4522
/**
4623
* Fetches a single task by ID
4724
*/

0 commit comments

Comments
 (0)