import { Button } from "@/components/ui/button"; import { FileUp } from "lucide-react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import React, { useState } from "react"; import { Input } from "@/components/ui/input"; import { useImportMultipleFiles } from "@/api/mangamochi"; import { toast } from "sonner"; interface MangaManualImportDialogProps { fileImportDialogOpen: boolean; onFileImportDialogOpenChange: (open: boolean) => void; } export const MangaManualImportDialog = ({ fileImportDialogOpen, onFileImportDialogOpenChange, }: MangaManualImportDialogProps) => { const [malId, setMalId] = useState(""); const [dragActive, setDragActive] = useState(false); const [files, setFiles] = useState(null); const { mutate, isPending } = useImportMultipleFiles({ mutation: { onSuccess: () => { setFiles(null); setMalId(""); onFileImportDialogOpenChange(false); toast.success("Manga imported successfully!"); }, onError: () => toast.error("Failed to import manga."), }, }); const handleFileImport = () => { if (!files) { alert("Please select one or more files to upload"); return; } if (!malId.trim()) { alert("Please enter a MyAnimeList manga ID"); return; } let id = malId; if (!/^\d+$/.test(malId)) { const regex = /https?:\/\/(?:www\.)?myanimelist\.net\/(manga)\/(\d+)(?:\/|$)/i; const match = malId.match(regex); if (match) { id = match[2]; } else { alert("Invalid MyAnimeList URL or ID"); return; } } mutate({ data: { malId: id, files } }); }; const handleDrag = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); if (e.type === "dragenter" || e.type === "dragover") { setDragActive(true); } else if (e.type === "dragleave") { setDragActive(false); } }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragActive(false); if (e.dataTransfer.files) { setFiles(Array.from(e.dataTransfer.files)); } }; const handleFileSelect = (e: React.ChangeEvent) => { if (e.target.files) { setFiles(Array.from(e.target.files)); } }; return ( Import from File Upload one or more files and provide the MyAnimeList manga URL (or ID) to import manga data.
setMalId(e.target.value)} className="mt-2" />
); };