File size: 1,850 Bytes
3d700dd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import { useMutation, useQueryClient } from "@tanstack/react-query";
import AgentServerConversationService from "#/api/conversation-service/agent-server-conversation-service.api";
import { clearConversationLocalStorage } from "#/utils/conversation-local-storage";
export const useDeleteConversation = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (variables: { conversationId: string }) =>
AgentServerConversationService.deleteConversation(
variables.conversationId,
),
onMutate: async (variables) => {
await queryClient.cancelQueries({ queryKey: ["user", "conversations"] });
const previousConversations = queryClient.getQueryData([
"user",
"conversations",
]);
queryClient.setQueryData(
["user", "conversations"],
(old: { conversation_id: string }[] | undefined) =>
old?.filter(
(conv) => conv.conversation_id !== variables.conversationId,
),
);
return { previousConversations };
},
onSuccess: (_, variables) => {
clearConversationLocalStorage(variables.conversationId);
},
onError: (err, variables, context) => {
if (context?.previousConversations) {
queryClient.setQueryData(
["user", "conversations"],
context.previousConversations,
);
}
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: ["user", "conversations"] });
// Mirror useCreateConversation: cloud surfaces in-flight
// conversations via useStartTasks, so invalidate that key too so the
// panel refreshes regardless of whether the deleted item was a ready
// conversation or a still-provisioning start task.
queryClient.invalidateQueries({ queryKey: ["start-tasks"] });
},
});
};
|