"use client"; import {DropZoneFile} from "@/components/wrappers/common/dropzone/dropzone-file"; import {useMutation, useQueryClient} from "@tanstack/react-query"; import {useState} from "react"; import {Loader2} from "lucide-react"; import {ButtonWithLoading} from "@/components/wrappers/common/button/button-with-loading"; import {toast} from "sonner"; import {uploadBackupAction} from "@/components/wrappers/dashboard/database/import/upload-backup.action"; import {Card, CardContent} from "@/components/ui/card"; type UploadRetentionZoneProps = { onSuccessAction?: () => void; databaseId: string; }; export const UploadBackupZone = ({onSuccessAction, databaseId}: UploadRetentionZoneProps) => { const queryClient = useQueryClient(); const [isProcessing, setIsProcessing] = useState(false); const [file, setFile] = useState(null); const mutationUpload = useMutation({ mutationFn: async (file: File) => { try { setIsProcessing(true); const formData = new FormData(); formData.append("file", file); formData.append("databaseId", databaseId); const result = await uploadBackupAction(formData) const inner = result?.data; if (inner?.success) { toast.success(inner.actionSuccess?.message); onSuccessAction?.() queryClient.invalidateQueries({queryKey: ["database-data", databaseId]}); } else { toast.error(inner?.actionError?.message); } } catch (err) { console.error(err); toast.error("An error occurred while upload in the backup"); } finally { queryClient.invalidateQueries({queryKey: ["database-data", databaseId]}); setIsProcessing(false); } }, }); const acceptDbImportFiles: Record = { "application/sql": [".sql"], "application/x-sql": [".sql"], "text/plain": [".sql"], "application/octet-stream": [".dump"], }; return ( <> {isProcessing ? ( ) : ( setFile(file)} /> )} {(!isProcessing && file) && (
mutationUpload.mutateAsync(file)} isPending={false}> Upload
)} ); }; const UploadLoader = ({label = "Processing uploading..."}: { label?: string }) => { return (

{label}

Do not close this window

); };