openrag/frontend/app/api/mutations/useDeleteDocument.ts
Cole Goldsmith d47038e097
Reorganize folders within frontend (#407)
* reorganize folder structure

* move folders from merge

* fix import issue

* run format

* update configs
2025-11-17 08:23:23 -06:00

47 lines
1 KiB
TypeScript

"use client";
import { useMutation, useQueryClient } from "@tanstack/react-query";
interface DeleteDocumentRequest {
filename: string;
}
interface DeleteDocumentResponse {
success: boolean;
deleted_chunks: number;
filename: string;
message: string;
}
const deleteDocument = async (
data: DeleteDocumentRequest,
): Promise<DeleteDocumentResponse> => {
const response = await fetch("/api/documents/delete-by-filename", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || "Failed to delete document");
}
return response.json();
};
export const useDeleteDocument = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: deleteDocument,
onSettled: () => {
// Invalidate and refetch search queries to update the UI
setTimeout(() => {
queryClient.invalidateQueries({ queryKey: ["search"] });
}, 1000);
},
});
};