import React, { useState, useEffect } from "react";
import {
Box,
Paper,
Typography,
CircularProgress,
Button,
Snackbar,
Alert,
Grid,
IconButton,
Tooltip,
Dialog,
DialogTitle,
DialogContent,
DialogActions,
TextField,
Divider,
} from "@mui/material";
import { alpha } from "@mui/material/styles";
import CloudUploadIcon from "@mui/icons-material/CloudUpload";
import AutoFixHighIcon from "@mui/icons-material/AutoFixHigh";
import InsertDriveFileIcon from "@mui/icons-material/InsertDriveFile";
import DescriptionIcon from "@mui/icons-material/Description";
import ArticleIcon from "@mui/icons-material/Article";
import MenuBookIcon from "@mui/icons-material/MenuBook";
import DownloadIcon from "@mui/icons-material/Download";
import VisibilityIcon from "@mui/icons-material/Visibility";
import CloseIcon from "@mui/icons-material/Close";
import PublicIcon from "@mui/icons-material/Public";
import { useThemeMode } from "../../hooks/useThemeMode";
import getTheme from "../../config/theme";
import { API_CONFIG, apiService } from "../../config/api";
import { useDocumentSelection } from "./hooks/useDocumentSelection";
/**
* Component for creating a new benchmark, including file upload and generation initiation
*
* @param {Object} props - Component props
* @param {Function} props.onStartGeneration - Callback when generation starts with sessionId
* @returns {JSX.Element} CreateForm component
*/
function CreateForm({ onStartGeneration }) {
const { mode } = useThemeMode();
const theme = getTheme(mode);
// États pour la visualisation du document
const [openSnackbar, setOpenSnackbar] = useState(false);
const [isDownloading, setIsDownloading] = useState(false);
const [documentContent, setDocumentContent] = useState("");
const [openContentModal, setOpenContentModal] = useState(false);
const [isLoadingContent, setIsLoadingContent] = useState(false);
const [modalDocument, setModalDocument] = useState(null);
// Utiliser le hook personnalisé pour la gestion des documents
const {
isDragging,
isLoading,
sessionId,
selectedDocument,
isDefaultDocument,
urlInput,
urlSelected,
uploadStatus,
fileInputRef,
handleDragOver,
handleDragLeave,
handleClick,
handleFileChange,
handleDrop,
handleDefaultDocClick,
handleGenerateClick,
handleUrlInputChange,
setUploadStatus,
} = useDocumentSelection(onStartGeneration);
// Afficher le snackbar quand uploadStatus change
useEffect(() => {
if (uploadStatus) {
setOpenSnackbar(true);
}
}, [uploadStatus]);
// Liste des documents par défaut
const defaultDocuments = [
{
id: "pokemon-guide",
name: "Pokemon Guide",
icon: ,
description: "A comprehensive guide for Pokemon enthusiasts",
},
{
id: "hurricane-faq",
name: "Hurricane FAQ",
icon: ,
description: "Frequently asked questions about hurricanes",
},
{
id: "the-bitter-lesson",
name: "The Bitter Lesson",
icon: ,
description: "A seminal paper on AI development by Rich Sutton",
},
];
const handleCloseSnackbar = () => {
setOpenSnackbar(false);
};
const handleViewDocument = async (doc) => {
setIsLoadingContent(true);
try {
let extension = "";
if (doc.id === "the-bitter-lesson") {
extension = "html";
} else if (doc.id === "hurricane-faq") {
extension = "md";
} else {
extension = "txt";
}
// Mettre à jour l'état du document pour la modale
setModalDocument(doc);
// Utiliser apiService au lieu de fetch
const text = await apiService.getDocumentContent(doc.id, extension);
setDocumentContent(text);
setOpenContentModal(true);
} catch (error) {
console.error("Error loading document content:", error);
setUploadStatus({
success: false,
message: "Error loading document content",
});
} finally {
setIsLoadingContent(false);
}
};
const handleCloseContentModal = () => {
setOpenContentModal(false);
// Réinitialiser après la fermeture de la modale
setTimeout(() => {
setDocumentContent("");
setModalDocument(null);
}, 300);
};
const handleDownloadDocument = async (doc) => {
setIsDownloading(true);
try {
let extension = "";
if (doc.id === "the-bitter-lesson") {
extension = "html";
} else if (doc.id === "hurricane-faq") {
extension = "md";
} else {
extension = "txt";
}
// Utiliser le service API pour télécharger le document
await apiService.downloadDocument(doc.id, extension, doc.name);
} catch (error) {
console.error("Error downloading document:", error);
setUploadStatus({
success: false,
message: "Error downloading document",
});
} finally {
setIsDownloading(false);
}
};
return (
To create a benchmark, choose a sample document or{" "}
upload your own file/URL.
ideally a knowledge base, a FAQ, a news article, etc.
{defaultDocuments.map((doc) => (
handleDefaultDocClick(doc)}
>
{
e.stopPropagation();
handleViewDocument(doc);
}}
sx={{
position: "absolute",
top: 4,
right: 4,
color: "text.secondary",
opacity: 0.4,
"&:hover": {
opacity: 0.8,
backgroundColor: alpha(theme.palette.primary.main, 0.05),
},
padding: 0.3,
"& .MuiSvgIcon-root": {
fontSize: 16,
},
}}
disabled={isLoadingContent}
>
{isLoadingContent && selectedDocument?.id === doc.id ? (
) : (
)}
{doc.icon}
{doc.name}
{doc.description}
))}
{/* Zone de glisser-déposer pour les fichiers */}
{selectedDocument?.name && !isDefaultDocument && !urlSelected ? (
<>
{selectedDocument.name}
Click to upload a different file
>
) : (
<>
{isLoading && !urlSelected ? (
) : (
)}
{isLoading && !urlSelected
? "Uploading your file..."
: "Drag and drop your file here or click to browse"}
Accepted formats: PDF, TXT, HTML, MD
>
)}
{/* Champ d'entrée URL */}
{selectedDocument?.name && urlSelected ? (
<>
{selectedDocument?.domain || "URL processed"}
>
) : (
<>
{isLoading && urlSelected ? (
) : (
)}
{isLoading && urlSelected
? "Processing URL..."
: "Enter website address"}
>
)}
}
disabled={!sessionId}
sx={{ mt: 2 }}
>
{!sessionId
? "Generate benchmark"
: isDefaultDocument
? `Generate Benchmark from "${selectedDocument?.name}"`
: urlSelected
? "Generate Benchmark from URL"
: "Generate Benchmark from File"}
{uploadStatus?.message}
);
}
export default CreateForm;