- Added a delete confirmation dialog to the KnowledgeActionsDropdown component. - Integrated useDeleteDocument mutation for handling document deletion. - Updated the dropdown to pass the filename prop for targeted deletions. - Enhanced user feedback with success and error messages upon deletion.
45 lines
1 KiB
TypeScript
45 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,
|
|
onSuccess: () => {
|
|
// Invalidate and refetch search queries to update the UI
|
|
queryClient.invalidateQueries({ queryKey: ["search"] });
|
|
},
|
|
});
|
|
};
|